Mercurial > hg > th-libs
annotate th_file.c @ 478:b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Jul 2018 09:25:51 +0300 |
parents | 77d1af382e08 |
children | 2aa2052cb17d |
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 |
454 | 4 * (C) Copyright 2016-2018 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> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 #ifdef TH_PLAT_WINDOWS |
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> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 //# include <sys/wait.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 # include <sys/stat.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 # include <sys/types.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 char * th_get_home_dir() |
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 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 char tmpPath[MAX_PATH]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 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
|
26 return th_strdup(tmpPath); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 return th_strdup(getenv("HOME")); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 char * th_get_data_dir() |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 char tmpPath[MAX_PATH]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 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
|
37 return th_strdup(tmpPath); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 return th_strdup(getenv("HOME")); |
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 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
43 char * th_get_config_dir(const char *name) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 // 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
|
47 (void) name; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 return th_get_data_dir(); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 #elif defined(USE_XDG) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 const char *xdgConfigDir = getenv("XDG_CONFIG_HOME"); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 // If XDG is enabled, try the environment variable first |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 if (xdgConfigDir != NULL && strcmp(xdgConfigDir, "")) |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
54 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
|
55 else |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
56 { |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 // Nope, try the obvious alternative |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
58 char *data = th_get_data_dir(); |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
59 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
|
60 th_free(data); |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
61 return dir; |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
62 } |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 // XDG not enabled |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
65 (void) name; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 return th_get_data_dir(); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
71 #ifdef TH_PLAT_WINDOWS |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
72 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
|
73 { |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
74 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
|
75 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
76 // 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
|
77 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
|
78 } |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
79 #endif |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
80 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
81 |
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
|
82 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
|
83 { |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
84 #ifdef TH_PLAT_WINDOWS |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
85 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
|
86 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
|
87 return FALSE; |
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 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
|
90 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
|
91 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
|
92 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
|
93 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
94 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
|
95 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
|
96 data->flags |= TH_IS_READABLE; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
97 #else |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 uid_t id = geteuid(); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 struct stat sb; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 if (stat(path, &sb) < 0) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
101 return FALSE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 |
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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
108 data->flags = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0; |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
109 data->flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0; |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
110 data->flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0; |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
111 #endif |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
113 return TRUE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
117 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
|
118 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 char save, *path = th_strdup(cpath); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 size_t start = 0, end; |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
121 BOOL res = FALSE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 // If mode is 0, default to something sensible |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 if (mode == 0) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 mode = 0711; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 // Start creating the directory stucture |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 do |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 // Split foremost path element out |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 for (save = 0, end = start; path[end] != 0; end++) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 if (path[end] == TH_DIR_SEPARATOR) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 save = path[end]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 path[end] = 0; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 break; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 // If the element is there, create it |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 if (path[start] != 0) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 { |
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
|
142 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
|
143 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
|
144 if (exists && (sdata.flags & TH_IS_DIR) == 0) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 goto error; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 if (!exists) |
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 #ifdef TH_PLAT_WINDOWS |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 if (!CreateDirectory(path, NULL)) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 goto error; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 if (mkdir(path, mode) < 0) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 goto error; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 // Restore separator character and jump to next element |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 path[end] = save; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 start = end + 1; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 } while (save != 0); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
164 res = TRUE; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 error: |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 th_free(path); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 return res; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 } |