2 * Copyright 2013-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * John Scipione, jscipione@gmail.com
7 * Ingo Weinhold, ingo_weinhold@gmx.de
10 * headers/os/storage/PathFinder.h hrev47402
11 * src/kits/storage/PathFinder.cpp hrev47402
19 \brief Provides the BPathFinder class interface.
27 \brief Helper class to retrieve paths in the file system layout.
29 The BPathFinder provides two sets of methods for retrieving paths:
30 FindPath() for getting a single path in an installation location specified
31 via a constructor or a SetTo() invocation, and the static FindPaths() for
32 getting a list of paths for all installation locations.
39 \fn BPathFinder::BPathFinder(const void* codePointer,
40 const char* dependency)
41 \brief Creates an object referring to an installation location based on a
44 When initialized with this constructor a FindPath() method called afterward
45 determines the path of the image (i.e. executable, library, or add-on) file
46 associated with \a codePointer, a pointer to a location in the code or
47 static data of an image loaded in the caller's team. Based on that path the
48 path constant passed to FindPath() will be evaluated. In most cases that
49 means first determining the path of the installation location from the path.
51 If \a dependency is specified, instead of determining the installation
52 location path from the image path, the installation location path of the
53 dependency \a dependency of the package containing the image file is used.
55 If the initialization fails, e.g. due to insufficient memory or invalid
56 arguments, subsequent calls to FindPath() will return an error.
58 \param codePointer A pointer to code or static data belonging to the image
59 based on which FindPath() shall compute the path. The special value
60 \c B_APP_IMAGE_SYMBOL (the default) can be used to refer to the
61 program image, and \c B_CURRENT_IMAGE_SYMBOL for the caller's image.
62 \param dependency The name of the package's "requires" entry to be used for
63 resolving the installation location. Can be \c NULL (the default).
70 \fn BPathFinder::BPathFinder(const char* path, const char* dependency)
71 \brief Creates an object referring to an installation location based on a
74 When initialized with this constructor a FindPath() method called afterward
75 evaluates the path constant passed to it based on \a path. In most cases
76 that means first determining the path of the installation location from the
79 If \a dependency is specified, instead of determining the installation
80 location path from the given path, the installation location path of the
81 dependency \a dependency of the package containing the file referred to by
84 If the initialization fails, e.g. due to insufficient memory or invalid
85 arguments, subsequent calls to FindPath() will return an error.
87 \param path A path based on which FindPath() shall compute the path.
88 \param dependency The name of the package's "requires" entry to be used
89 for resolving the installation location. Can be \c NULL.
96 \fn BPathFinder::BPathFinder(const entry_ref& ref, const char* dependency)
97 \brief Creates an object referring to an installation location based on a
100 The constructor converts the given entry_ref \a ref to a path and then
101 initializes the object like
102 BPathFinder::BPathFinder(const char*, const char*).
104 \param ref A reference to be resolved to a path based on which FindPath()
105 shall compute the path.
106 \param dependency The name of the package's "requires" entry to be used
107 for resolving the installation location. Can be \c NULL.
109 \see BPathFinder::BPathFinder(const char*, const char*)
116 \fn BPathFinder::BPathFinder(const BResolvableExpression& expression,
117 const char* dependency)
118 \brief Creates an object referring to an installation location based on
119 the path of a package satisfying the given resolvable expression.
121 The constructor finds the latest package that satisfies the resolvable
122 expression \a expression and then uses its path to initialize the object
123 like BPathFinder::BPathFinder(const char*, const char*).
125 \note When using this constructor linking against libpackage.so is
128 \param expression A resolvable expression to be resolved to the path of
129 the latest package satisfying it, based on which FindPath() shall
131 \param dependency The name of the package's "requires" entry to be used
132 for resolving the installation location. Can be \c NULL.
134 \see BPathFinder::BPathFinder(const char*, const char*)
141 \fn status_t BPathFinder::SetTo(const void* codePointer,
142 const char* dependency)
143 \brief Reinitializes the object to refer to an installation location based
144 on a loaded image file.
146 When reinitialized with this method a FindPath() method called afterward
147 determines the path of the image (i.e. executable, library, or add-on)
148 file associated with \a codePointer, a pointer to a location in the code
149 or static data of an image loaded in the caller's team. Based on that path
150 the path constant passed to FindPath() will be evaluated. In most cases
151 that means first determining the path of the installation location from
154 If \a dependency is specified, instead of determining the installation
155 location path from the image path, the installation location path of the
156 dependency \a dependency of the package containing the image file is used.
158 If the initialization fails, e.g. due to insufficient memory or invalid
159 arguments, this method and subsequent calls to FindPath() will return an
162 \param codePointer A pointer to code or static data belonging to the image
163 based on which FindPath() shall compute the path. The special value
164 \c B_APP_IMAGE_SYMBOL (the default) can be used to refer to the
165 program image, and \c B_CURRENT_IMAGE_SYMBOL for the caller's image.
166 \param dependency The name of the package's "requires" entry to be used for
167 resolving the installation location. Can be \c NULL (the default).
169 \return A status code.
170 \retval B_OK Everything went fine.
177 \fn status_t BPathFinder::SetTo(const char* path, const char* dependency)
178 \brief Reinitializes the object to refer to an installation location based
181 When reinitialized with this method a FindPath() method called afterward
182 evaluates the path constant passed to it based on \a path. In most cases
183 that means first determining the path of the installation location from
186 If \a dependency is specified, instead of determining the installation
187 location path from the given path, the installation location path of the
188 dependency \a dependency of the package containing the file referred to by
191 If the initialization fails, e.g. due to insufficient memory or invalid
192 arguments, this method and subsequent calls to FindPath() will return an
195 \param path A path based on which FindPath() shall compute the path.
196 \param dependency The name of the package's "requires" entry to be used for
197 resolving the installation location. Can be \c NULL.
199 \return \c B_OK Everything if went fine or an error code otherwise.
206 \fn status_t BPathFinder::SetTo(const entry_ref& ref,
207 const char* dependency)
208 \brief Reinitializes the object to refer to an installation location based
209 on a given entry_ref.
211 This method converts the given entry_ref \a ref to a path and then calls
212 calls BPathFinder::SetTo(const char*, const char*).
214 \param ref A reference to be resolved to a path based on which FindPath()
215 shall compute the path.
216 \param dependency The name of the package's "requires" entry to be used
217 for resolving the installation location. Can be \c NULL.
219 \see status_t BPathFinder::SetTo(const char*, const char*)
221 \return \c B_OK Everything if went fine or an error code otherwise.
228 \fn status_t BPathFinder::SetTo(const BResolvableExpression& expression,
229 const char* dependency)
230 \brief Reinitializes the object to refer to an installation location based
231 on a given resolvable expression.
233 This method finds the latest package that satisfies the resolvable
234 expression \a expression and then passes its path to
235 BPathFinder::SetTo(const char*, const char*).
237 \note When using this method linking against libpackage.so is required.
239 \param expression A resolvable expression to be resolved to the path of the
240 latest package satisfying it, based on which FindPath() shall compute
242 \param dependency The name of the package's "requires" entry to be used for
243 resolving the installation location. Can be \c NULL.
245 \see status_t BPathFinder::SetTo(const char*, const char*)
247 \return \c B_OK Everything if went fine or an error code otherwise.
254 \fn status_t BPathFinder::FindPath(const char* architecture,
255 path_base_directory baseDirectory, const char* subPath, uint32 flags,
257 \brief Retrieves a path in the file system layout based.
259 Depending on how the object was initialized this method starts with a path
260 (from an image file or as given) and based on it evaluates \a baseDirectory.
261 In most cases that means first determining the path of the installation
262 location from the path, then appending the relative path corresponding to
263 the given \a baseDirectory constant, and finally appending \a subPath, if
266 If a dependency string was passed to the previous constructor or SetTo()
267 method, instead of determining the installation location path from the
268 initial path, the installation location path of the dependency of the
269 package containing the file the initial path refers to is used.
271 If \a baseDirectory specifies a path that is architecture dependent,
272 \a architecture is used for constructing the path. If \a architecture is
273 \c NULL, the architecture associated with the initial path (as returned by
274 guess_architecture_for_path()) is used. Note that if an image was specified,
275 this is the same as the caller's architecture (as returned by
278 If \c B_FIND_PATH_IMAGE_PATH or \c B_FIND_PATH_PACKAGE_PATH are
279 specified, \a subPath is ignored. In the former case, which is only valid,
280 if an image was specified for initialization, \a dependency is ignored as
281 well and the path of the image file is returned. In the latter case the path
282 of the package containing the file the initial path refers to, respectively,
283 if \a dependency was specified, that of the package \a dependency was
284 resolved to is returned.
286 \param architecture The name of the architecture to be used for resolving
287 \a architecture dependent paths. Can be \c NULL, in which case the
288 \a architecture associated with the initial path is used.
289 \param baseDirectory Constant indicating which path to retrieve.
290 \param subPath Relative subpath that shall be appended. Can be \c NULL.
291 \param flags Bitwise OR of any of the following flags:
292 - \c B_FIND_PATH_CREATE_DIRECTORY: If the resulting path doesn't
293 exist, create it as a directory (including all missing ancestors).
294 - \c B_FIND_PATH_CREATE_PARENT_DIRECTORY: If the resulting path's
295 parent doesn't exist, create the parent directory (including all
297 - \c B_FIND_PATH_EXISTING_ONLY: If the resulting path doesn't exist,
298 fail with \c B_ENTRY_NOT_FOUND.
299 \param _path The variable to be set to the resulting path on success.
301 \return A status code.
302 \retval B_OK Everything went fine.
303 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
304 path didn't exist, e.g. \c B_FIND_PATH_PACKAGE_PATH was specified
305 and the image file didn't belong to a package, or \c dependency
306 was specified, but wasn't a "requires" entry of the package, or
307 \c B_FIND_PATH_EXISTING_ONLY was specified and the resulting path
315 \fn status_t BPathFinder::FindPath(path_base_directory baseDirectory,
316 const char* subPath, uint32 flags, BPath& _path)
317 \brief Retrieves a path in the file system layout based.
319 Equivalent to a call to BPathFinder::FindPath(const char*,
320 path_base_directory, const char*, uint32, BPath&) with a \c NULL
323 \param baseDirectory Constant indicating which path to retrieve.
324 \param subPath Relative subpath that shall be appended. Can be \c NULL.
325 \param flags Bitwise OR of any of the following flags:
326 - \c B_FIND_PATH_CREATE_DIRECTORY: If the resulting path doesn't
327 exist, create it as a directory (including all missing ancestors).
328 - \c B_FIND_PATH_CREATE_PARENT_DIRECTORY: If the resulting path's
329 parent doesn't exist, create the parent directory (including all
331 - \c B_FIND_PATH_EXISTING_ONLY: If the resulting path doesn't exist,
332 fail with \c B_ENTRY_NOT_FOUND.
333 \param _path The variable to be set to the resulting path on success.
335 \return A status code.
336 \retval B_OK Everything went fine.
337 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
338 path didn't exist, e.g. \c B_FIND_PATH_PACKAGE_PATH was specified
339 and the image file didn't belong to a package, or \c dependency
340 was specified, but wasn't a "requires" entry of the package, or
341 \c B_FIND_PATH_EXISTING_ONLY was specified and the resulting path
349 \fn status_t BPathFinder::FindPath(path_base_directory baseDirectory,
350 const char* subPath, BPath& _path)
351 \brief Retrieves a path in the file system layout based.
353 Equivalent to a call to BPathFinder::FindPath(const char*,
354 path_base_directory, const char*, uint32, BPath&) with a \c NULL
355 architecture and 0 flags.
357 \param baseDirectory Constant indicating which path to retrieve.
358 \param subPath Relative subpath that shall be appended. Can be \c NULL.
359 \param _path The variable to be set to the resulting path on success.
361 \return A status code.
362 \retval B_OK Everything went fine.
363 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
364 path didn't exist, e.g. \c B_FIND_PATH_PACKAGE_PATH was specified
365 and the image file didn't belong to a package, or \c dependency was
366 specified, but wasn't a "requires" entry of the package.
371 \fn status_t BPathFinder::FindPath(path_base_directory baseDirectory,
373 \brief Retrieves a path in the file system layout based.
375 Equivalent to a call to BPathFinder::FindPath(const char*,
376 path_base_directory, const char*, uint32, BPath&) with a \c NULL
377 architecture, 0 flags, and \c NULL subpath.
379 \param baseDirectory Constant indicating which path to retrieve.
380 \param _path The variable to be set to the resulting path on success.
381 \return A status code.
382 \retval B_OK Everything went fine.
383 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
384 path didn't exist, e.g. \c B_FIND_PATH_PACKAGE_PATH was specified
385 and the image file didn't belong to a package, or \c dependency
386 was specified, but wasn't a "requires" entry of the package.
393 \fn status_t BPathFinder::FindPaths(const char* architecture,
394 path_base_directory baseDirectory, const char* subPath, uint32 flags,
396 \brief Retrieves a list of paths in the file system layout.
398 For each installation location -- in the order most specific to most
399 generic, non-packaged before packaged -- the function evaluates
400 \a baseDirectory to a path and appends \a subPath, if given.
402 If \a baseDirectory specifies a path that is architecture dependent,
403 \a architecture is used for constructing each path. If \a architecture is
404 \c NULL, the caller's architecture (as returned by get_architecture()) is
407 \c B_FIND_PATH_PACKAGE_PATH and \c B_FIND_PATH_IMAGE_PATH are not
408 valid arguments for this function.
410 \param architecture The name of the architecture to be used for resolving
411 \a architecture dependent paths. Can be \c NULL, in which case the
412 caller's \a architecture is used.
413 \param baseDirectory Constant indicating which paths to retrieve.
414 \param subPath Relative subpath that shall be appended. Can be \c NULL.
415 \param flags Bitwise OR of any of the following flags:
416 - \c B_FIND_PATH_CREATE_DIRECTORY: If a resulting path doesn't
417 exist, create it as a directory (including all missing ancestors).
418 - \c B_FIND_PATH_CREATE_PARENT_DIRECTORY: If a resulting path's
419 parent doesn't exist, create the parent directory (including all
421 - \c B_FIND_PATH_EXISTING_ONLY: If a resulting path doesn't exist,
422 skip it. If none of the paths exist, fail with \c B_ENTRY_NOT_FOUND.
423 \param _paths The BStringList variable where the retrieved paths shall be
424 stored. The list is emptied before adding the paths. It is also
427 \return A status code.
428 \retval B_OK Everything went fine.
429 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
430 paths didn't exist, e.g. \c B_FIND_PATH_EXISTING_ONLY was
431 specified and none of the resulting paths existed.
438 \fn status_t BPathFinder::FindPaths(path_base_directory baseDirectory,
439 const char* subPath, uint32 flags, BStringList& _paths)
440 \brief Retrieves a list of paths in the file system layout.
442 Equivalent to a call to BPathFinder::FindPaths(const char*,
443 path_base_directory, const char*, uint32, BStringList&) with a \c NULL
446 \param baseDirectory Constant indicating which paths to retrieve.
447 \param subPath Relative subpath that shall be appended. Can be \c NULL.
448 \param flags Bitwise OR of any of the following flags:
449 - \c B_FIND_PATH_CREATE_DIRECTORY: If a resulting path doesn't
450 exist, create it as a directory (including all missing ancestors).
451 - \c B_FIND_PATH_CREATE_PARENT_DIRECTORY: If a resulting path's
452 parent doesn't exist, create the parent directory (including all
454 - \c B_FIND_PATH_EXISTING_ONLY: If a resulting path doesn't exist,
455 skip it. If none of the paths exist, fail with
456 \c B_ENTRY_NOT_FOUND.
457 \param _paths The BStringList variable where the retrieved paths shall be
458 stored. The list is emptied before adding the paths. It is also
461 \return A status code.
462 \retval B_OK Everything went fine.
463 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
464 paths didn't exist, e.g. \c B_FIND_PATH_EXISTING_ONLY was specified
465 and none of the resulting paths existed.
472 \fn status_t BPathFinder::FindPaths(path_base_directory baseDirectory,
473 const char* subPath, BStringList& _paths)
474 \brief Retrieves a list of paths in the file system layout.
476 Equivalent to a call to BPathFinder::FindPaths(const char*,
477 path_base_directory, const char*, uint32, BStringList&) with a \c NULL
478 architecture and 0 flags.
480 \param baseDirectory Constant indicating which paths to retrieve.
481 \param subPath Relative subpath that shall be appended. Can be \c NULL.
482 \param _paths The BStringList variable where the retrieved paths shall be
483 stored. The list is emptied before adding the paths. It is also
486 \return A status code.
487 \retval B_OK Everything went fine.
488 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
489 paths didn't exist, e.g. \c B_FIND_PATH_EXISTING_ONLY was specified
490 and none of the resulting paths existed.
497 \fn status_t BPathFinder::FindPaths(path_base_directory baseDirectory,
499 \brief Retrieves a list of paths in the file system layout.
501 Equivalent to a call to BPathFinder::FindPaths(const char*,
502 path_base_directory, const char*, uint32, BStringList&) with a \c NULL
503 architecture, 0 flags, and \c NULL subpath.
505 \param baseDirectory Constant indicating which paths to retrieve.
506 \param _paths The BStringList variable where the retrieved paths shall be
507 stored. The list is emptied before adding the paths. It is also
510 \return A status code.
511 \retval B_OK Everything went fine.
512 \retval B_ENTRY_NOT_FOUND A file system entry required for retrieving the
513 paths didn't exist, e.g. \c B_FIND_PATH_EXISTING_ONLY was specified
514 and none of the resulting paths existed.