diff --git a/headers/os/storage/PathFinder.h b/headers/os/storage/PathFinder.h new file mode 100644 index 0000000000..a1819fb145 --- /dev/null +++ b/headers/os/storage/PathFinder.h @@ -0,0 +1,63 @@ +/* + * Copyright 2013, Haiku Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _PATH_FINDER_H +#define _PATH_FINDER_H + + +#include +#include + + +class BPath; +class BStringList; +struct entry_ref; + + +class BPathFinder { +public: + BPathFinder(const void* codePointer = NULL, + const char* dependency = NULL); + BPathFinder(const char* path, + const char* dependency = NULL); + BPathFinder(const entry_ref& ref, + const char* dependency = NULL); + + status_t SetTo(const void* codePointer = NULL, + const char* dependency = NULL); + status_t SetTo(const char* path, + const char* dependency = NULL); + status_t SetTo(const entry_ref& ref, + const char* dependency = NULL); + + status_t FindPath(path_base_directory baseDirectory, + const char* subPath, uint32 flags, + BPath& path); + status_t FindPath(path_base_directory baseDirectory, + const char* subPath, BPath& path); + status_t FindPath(path_base_directory baseDirectory, + BPath& path); + + static status_t FindPaths(path_base_directory baseDirectory, + const char* subPath, uint32 flags, + BStringList& paths); + static status_t FindPaths(path_base_directory baseDirectory, + const char* subPath, BStringList& paths); + static status_t FindPaths(path_base_directory baseDirectory, + BStringList& paths); + +private: + status_t _SetTo(const void* codePointer, + const char* path, const char* dependency); + +private: + const void* fCodePointer; + BString fPath; + BString fDependency; + status_t fInitStatus; + uint32 fReserved[4]; +}; + + +#endif // _PATH_FINDER_H diff --git a/src/kits/storage/Jamfile b/src/kits/storage/Jamfile index c57db1a203..f7703a39f0 100644 --- a/src/kits/storage/Jamfile +++ b/src/kits/storage/Jamfile @@ -39,6 +39,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { NodeMonitor.cpp OffsetFile.cpp Path.cpp + PathFinder.cpp PathMonitor.cpp Query.cpp QueryPredicate.cpp diff --git a/src/kits/storage/PathFinder.cpp b/src/kits/storage/PathFinder.cpp new file mode 100644 index 0000000000..579418999c --- /dev/null +++ b/src/kits/storage/PathFinder.cpp @@ -0,0 +1,161 @@ +/* + * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include + +#include +#include +#include + + +BPathFinder::BPathFinder(const void* codePointer, const char* dependency) +{ + _SetTo(codePointer, NULL, dependency); +} + + +BPathFinder::BPathFinder(const char* path, const char* dependency) +{ + _SetTo(NULL, path, dependency); +} + + +BPathFinder::BPathFinder(const entry_ref& ref, const char* dependency) +{ + SetTo(ref, dependency); +} + + +status_t +BPathFinder::SetTo(const void* codePointer, const char* dependency) +{ + return _SetTo(codePointer, NULL, dependency); +} + + +status_t +BPathFinder::SetTo(const char* path, const char* dependency) +{ + return _SetTo(NULL, path, dependency); +} + + +status_t +BPathFinder::SetTo(const entry_ref& ref, const char* dependency) +{ + BPath path; + fInitStatus = path.SetTo(&ref); + if (fInitStatus != B_OK) + return fInitStatus; + + return _SetTo(NULL, path.Path(), dependency); +} + + +status_t +BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath, + uint32 flags, BPath& path) +{ + path.Unset(); + + if (fInitStatus != B_OK) + return fInitStatus; + + const char* dependency = fDependency.IsEmpty() + ? NULL : fDependency.String(); + + char pathBuffer[B_PATH_NAME_LENGTH]; + status_t error; + + if (!fPath.IsEmpty()) { + error = find_path_for_path(fPath, dependency, baseDirectory, subPath, + flags, pathBuffer, sizeof(pathBuffer)); + } else { + error = find_path(fCodePointer, dependency, baseDirectory, subPath, + flags, pathBuffer, sizeof(pathBuffer)); + } + + if (error != B_OK) + return error; + + return path.SetTo(pathBuffer); +} + + +status_t +BPathFinder::FindPath(path_base_directory baseDirectory, const char* subPath, + BPath& path) +{ + return FindPath(baseDirectory, subPath, 0, path); +} + + +status_t +BPathFinder::FindPath(path_base_directory baseDirectory, BPath& path) +{ + return FindPath(baseDirectory, NULL, 0, path); +} + + +/*static*/ status_t +BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath, + uint32 flags, BStringList& paths) +{ + paths.MakeEmpty(); + + // get the paths + char** pathArray; + size_t pathCount; + status_t error = find_paths(baseDirectory, subPath, flags, &pathArray, + &pathCount); + if (error != B_OK) + return error; + + // add them to BStringList + for (size_t i = 0; i < pathCount; i++) { + BString path(pathArray[i]); + if (path.IsEmpty() || !paths.Add(path)) { + paths.MakeEmpty(); + return B_NO_MEMORY; + } + } + + return B_OK; +} + + +/*static*/ status_t +BPathFinder::FindPaths(path_base_directory baseDirectory, const char* subPath, + BStringList& paths) +{ + return FindPaths(baseDirectory, subPath, 0, paths); +} + + +/*static*/ status_t +BPathFinder::FindPaths(path_base_directory baseDirectory, BStringList& paths) +{ + return FindPaths(baseDirectory, NULL, 0, paths); +} + + +status_t +BPathFinder::_SetTo(const void* codePointer, const char* path, + const char* dependency) +{ + fCodePointer = codePointer; + fPath = path; + fDependency = dependency; + + if ((path != NULL && path[0] != '\0' && fPath.IsEmpty()) + || (dependency != NULL && dependency[0] != '\0' + && fDependency.IsEmpty())) { + fInitStatus = B_NO_MEMORY; + } else + fInitStatus = B_OK; + + return fInitStatus; +}