fat: Greatly simplify and clean up dosfs_get_file_map().
[haiku.git] / src / build / libroot / find_directory.cpp
blob0fce36a072f0952563dfc71c291966f398401ea4
1 /*
2 * Copyright 2004, François Revol.
3 * Copyright 2007-2010, Axel Dörfler, axeld@pinc-software.de.
4 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
5 * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
6 * Distributed under the terms of the MIT License.
7 */
10 #include <FindDirectory.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <stdlib.h>
16 #include <unistd.h>
18 #include <StorageDefs.h>
20 #include <find_directory_private.h>
23 #ifndef HAIKU_BUILD_GENERATED_DIRECTORY
24 # error HAIKU_BUILD_GENERATED_DIRECTORY not defined!
25 #endif
28 /*! make dir and its parents if needed */
29 static int
30 create_path(const char *path, mode_t mode)
32 char buffer[B_PATH_NAME_LENGTH + 1];
33 int pathLength;
34 int i = 0;
36 if (path == NULL || ((pathLength = strlen(path)) > B_PATH_NAME_LENGTH))
37 return EINVAL;
39 while (++i < pathLength) {
40 const char *slash = strchr(&path[i], '/');
41 struct stat st;
43 if (slash == NULL)
44 i = pathLength;
45 else if (i != slash - path)
46 i = slash - path;
47 else
48 continue;
50 strlcpy(buffer, path, i + 1);
51 if (stat(buffer, &st) < 0) {
52 errno = 0;
53 if (mkdir(buffer, mode) < 0)
54 return errno;
58 return 0;
62 status_t
63 find_directory(directory_which which, dev_t device, bool createIt,
64 char *returnedPath, int32 pathLength)
66 // we support only the handful of paths we need
67 const char* path;
68 switch (which) {
69 case B_SYSTEM_TEMP_DIRECTORY:
70 path = HAIKU_BUILD_GENERATED_DIRECTORY "/tmp";
71 break;
72 case B_SYSTEM_SETTINGS_DIRECTORY:
73 path = HAIKU_BUILD_GENERATED_DIRECTORY "/system/settings";
74 break;
75 case B_SYSTEM_CACHE_DIRECTORY:
76 path = HAIKU_BUILD_GENERATED_DIRECTORY "/system/cache";
77 break;
78 case B_USER_SETTINGS_DIRECTORY:
79 path = HAIKU_BUILD_GENERATED_DIRECTORY "/user/settings";
80 break;
81 case B_USER_CACHE_DIRECTORY:
82 path = HAIKU_BUILD_GENERATED_DIRECTORY "/user/cache";
83 break;
84 default:
85 return B_BAD_VALUE;
88 // create, if necessary
89 status_t error = B_OK;
90 struct stat st;
91 if (createIt && stat(path, &st) < 0)
92 error = create_path(path, 0755);
94 if (error == B_OK)
95 strlcpy(returnedPath, path, pathLength);
97 return error;
101 status_t
102 __find_directory(directory_which which, dev_t device, bool createIt,
103 char *returnedPath, int32 pathLength)
105 return find_directory(which, device, createIt, returnedPath, pathLength);