Mercurial > hg > th-libs
annotate th_file.c @ 485:4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
directory in th_get_home_dir() and th_get_data_dir().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 04 May 2019 03:00:23 +0300 |
parents | 85039ab7e6ab |
children | 3a852e9f70a6 |
rev | line source |
---|---|
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * File, directory etc helper functions |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
481 | 4 * (C) Copyright 2016-2019 Tecnic Software productions (TNSP) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_file.h" |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_string.h" |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #include <unistd.h> |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
11 #if defined(TH_PLAT_WINDOWS) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 # include <shlwapi.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 # include <shfolder.h> |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
14 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
15 # include <sys/stat.h> |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
16 # include <sys/types.h> |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 //# include <sys/wait.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 # include <sys/stat.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 # include <sys/types.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 char * th_get_home_dir() |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 char tmpPath[MAX_PATH]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 return th_strdup(tmpPath); |
485
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
30 else |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
31 return th_strdup(""); |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
32 |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
33 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
34 return th_strdup("C:\\"); |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
35 #else |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
36 return th_strdup(getenv("HOME")); |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 char * th_get_data_dir() |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 char tmpPath[MAX_PATH]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 return th_strdup(tmpPath); |
485
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
47 else |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
48 return th_strdup(""); |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
49 |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
50 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
51 return th_strdup("C:\\"); |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
52 #else |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
53 return th_strdup(getenv("HOME")); |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
58 char * th_get_config_dir(const char *name) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 // For Windows, we just use the appdata directory |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
62 (void) name; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 return th_get_data_dir(); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 #elif defined(USE_XDG) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 const char *xdgConfigDir = getenv("XDG_CONFIG_HOME"); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 // If XDG is enabled, try the environment variable first |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 if (xdgConfigDir != NULL && strcmp(xdgConfigDir, "")) |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
69 return th_strdup_printf("%s%c%s%c", xdgConfigDir, TH_DIR_SEPARATOR, name, TH_DIR_SEPARATOR); |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 else |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
71 { |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 // Nope, try the obvious alternative |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
73 char *data = th_get_data_dir(); |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
74 char *dir = th_strdup_printf("%s%c%s%c%s%c", data, TH_DIR_SEPARATOR, ".config", TH_DIR_SEPARATOR, name, TH_DIR_SEPARATOR); |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
75 th_free(data); |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
76 return dir; |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
77 } |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 // XDG not enabled |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
80 (void) name; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 return th_get_data_dir(); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
86 #ifdef TH_PLAT_WINDOWS |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
87 static uint64_t th_convert_windows_time(FILETIME ftime) |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
88 { |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
89 uint64_t value = (((uint64_t) ftime.dwHighDateTime) << 32ULL) | ((uint64_t) ftime.dwLowDateTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
90 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
91 // Naive conversion (1000 ns / 100) * ns - difference_between_win_to_unix_epoch |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
92 return (value / ((1000 / 100) * 1000 * 1000)) - 11644473600ULL;; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
93 } |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
94 #endif |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
95 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
96 |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
97 BOOL th_stat_path(const char *path, th_stat_data *data) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 { |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
99 #if defined(TH_PLAT_WINDOWS) |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
100 WIN32_FILE_ATTRIBUTE_DATA fdata; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
101 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fdata)) |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
102 return FALSE; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
103 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
104 data->size = (((uint64_t) fdata.nFileSizeHigh) << 32ULL) | ((uint64_t) fdata.nFileSizeLow); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
105 data->ctime = th_convert_windows_time(fdata.ftCreationTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
106 data->atime = th_convert_windows_time(fdata.ftLastAccessTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
107 data->mtime = th_convert_windows_time(fdata.ftLastWriteTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
108 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
109 data->flags = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
110 data->flags |= (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
111 data->flags |= TH_IS_READABLE; |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
112 |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
113 #else |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
114 #if defined(TH_PLAT_UNIX) |
480
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
115 uid_t uid = geteuid(); |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
116 gid_t gid = getegid(); |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
117 #endif |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
118 |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 struct stat sb; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 if (stat(path, &sb) < 0) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
121 return FALSE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
123 data->size = sb.st_size; |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
124 data->ctime = sb.st_ctime; |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
125 data->atime = sb.st_atime; |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
126 data->mtime = sb.st_mtime; |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
127 |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
128 data->flags = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0; |
480
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
129 |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
130 #if defined(TH_PLAT_UNIX) |
480
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
131 if ((uid == sb.st_uid && (sb.st_mode & S_IWUSR)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
132 (gid == sb.st_gid && (sb.st_mode & S_IWGRP)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
133 (sb.st_mode & S_IWOTH)) |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
134 data->flags |= TH_IS_WRITABLE; |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
135 |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
136 if ((uid == sb.st_uid && (sb.st_mode & S_IRUSR)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
137 (gid == sb.st_gid && (sb.st_mode & S_IRGRP)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
138 (sb.st_mode & S_IROTH)) |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
139 data->flags |= TH_IS_READABLE; |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
140 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
141 data->flags |= TH_IS_READABLE | TH_IS_WRITABLE; |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
142 #endif |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
143 |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
144 #endif |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
146 return TRUE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
150 BOOL th_mkdir_path(const char *cpath, int mode) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 char save, *path = th_strdup(cpath); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 size_t start = 0, end; |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
154 BOOL res = FALSE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 // If mode is 0, default to something sensible |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 if (mode == 0) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 mode = 0711; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 // Start creating the directory stucture |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 do |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 // Split foremost path element out |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 for (save = 0, end = start; path[end] != 0; end++) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 if (path[end] == TH_DIR_SEPARATOR) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 save = path[end]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 path[end] = 0; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 break; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 // If the element is there, create it |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 if (path[start] != 0) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 { |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
175 th_stat_data sdata; |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
176 BOOL exists = th_stat_path(path, &sdata); |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
177 if (exists && (sdata.flags & TH_IS_DIR) == 0) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 goto error; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 if (!exists) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 { |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
182 #if defined(TH_PLAT_WINDOWS) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 if (!CreateDirectory(path, NULL)) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 goto error; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 if (mkdir(path, mode) < 0) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 goto error; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
189 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 // Restore separator character and jump to next element |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 path[end] = save; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 start = end + 1; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 } while (save != 0); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
197 res = TRUE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 error: |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 th_free(path); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 return res; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 } |