comparison th_file.c @ 741:0852bd106034

Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 14 Dec 2022 01:32:40 +0200
parents caeb0a44ebdf
children 64af66cae541
comparison
equal deleted inserted replaced
740:caeb0a44ebdf 741:0852bd106034
148 148
149 return true; 149 return true;
150 } 150 }
151 151
152 152
153 bool th_mkdir_path(const char *cpath, const int mode) 153 int th_mkdir_path(const char *cpath, const int mode)
154 { 154 {
155 char save, *path = th_strdup(cpath); 155 char save, *path = th_strdup(cpath);
156 size_t start = 0, end; 156 size_t start = 0, end;
157 bool res = false; 157 int res = THERR_OK;
158
159 if (path == NULL)
160 return THERR_MALLOC;
158 161
159 #if defined(TH_PLAT_WINDOWS) 162 #if defined(TH_PLAT_WINDOWS)
160 (void) mode; 163 (void) mode;
161 #endif 164 #endif
162 165
176 if (path[start] != 0) 179 if (path[start] != 0)
177 { 180 {
178 th_stat_data sdata; 181 th_stat_data sdata;
179 bool exists = th_stat_path(path, &sdata); 182 bool exists = th_stat_path(path, &sdata);
180 if (exists && (sdata.flags & TH_IS_DIR) == 0) 183 if (exists && (sdata.flags & TH_IS_DIR) == 0)
181 goto error; 184 {
185 res = THERR_FWRITE;
186 goto out;
187 }
182 188
183 if (!exists) 189 if (!exists)
184 { 190 {
185 #if defined(TH_PLAT_WINDOWS) 191 #if defined(TH_PLAT_WINDOWS)
186 if (!CreateDirectory(path, NULL)) 192 if (!CreateDirectory(path, NULL))
187 goto error;
188 #else 193 #else
189 if (mkdir(path, mode != 0 ? mode : 0711) < 0) 194 if (mkdir(path, mode != 0 ? mode : 0711) < 0)
190 goto error; 195 #endif
191 #endif 196 {
197 res = th_get_error();
198 goto out;
199 }
192 } 200 }
193 } 201 }
194 202
195 // Restore separator character and jump to next element 203 // Restore separator character and jump to next element
196 path[end] = save; 204 path[end] = save;
197 start = end + 1; 205 start = end + 1;
198 } while (save != 0); 206 } while (save != 0);
199 207
200 res = true; 208 out:
201
202 error:
203 th_free(path); 209 th_free(path);
204 return res; 210 return res;
205 } 211 }