annotate th_file.c @ 457:85fa3d333556

Actually, revert the boolean changes .. meh.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Jan 2018 23:09:29 +0200
parents 347bfd3e017e
children 77d1af382e08
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
347bfd3e017e Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
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 //#include <fcntl.h>
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #ifdef TH_PLAT_WINDOWS
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 # include <shlwapi.h>
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 # include <shfolder.h>
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #else
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 //# include <sys/wait.h>
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 # include <sys/stat.h>
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 # include <sys/types.h>
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 #endif
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
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 char * th_get_home_dir()
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 #if defined(TH_PLAT_WINDOWS)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 char tmpPath[MAX_PATH];
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 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
27 return th_strdup(tmpPath);
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 #endif
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 return th_strdup(getenv("HOME"));
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
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 char * th_get_data_dir()
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 #if defined(TH_PLAT_WINDOWS)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 char tmpPath[MAX_PATH];
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 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
38 return th_strdup(tmpPath);
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 #endif
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 return th_strdup(getenv("HOME"));
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
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
207
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
44 char * th_get_config_dir(const char *name)
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 #if defined(TH_PLAT_WINDOWS)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 // 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
48 (void) name;
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 return th_get_data_dir();
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 #elif defined(USE_XDG)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 const char *xdgConfigDir = getenv("XDG_CONFIG_HOME");
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 // If XDG is enabled, try the environment variable first
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 if (xdgConfigDir != NULL && strcmp(xdgConfigDir, ""))
207
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
55 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
56 else
207
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
57 {
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 // Nope, try the obvious alternative
207
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
59 char *data = th_get_data_dir();
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
60 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
61 th_free(data);
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
62 return dir;
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
63 }
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 #else
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 // XDG not enabled
207
75dbac8f2f59 Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
66 (void) name;
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 return th_get_data_dir();
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 #endif
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
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
72 BOOL th_stat_path(const char *path, int *flags)
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 *flags = 0;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 #ifdef TH_PLAT_WINDOWS
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 DWORD attr = GetFileAttributes(path);
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 *flags |= (attr & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 *flags |= (attr & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 *flags |= TH_IS_READABLE;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 #else
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 uid_t id = geteuid();
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 struct stat sb;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 if (stat(path, &sb) < 0)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
86 return FALSE;
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 *flags |= S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 *flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 *flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 #endif
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
93 return TRUE;
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 }
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
97 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
98 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 char save, *path = th_strdup(cpath);
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 size_t start = 0, end;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
101 BOOL res = FALSE;
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 // If mode is 0, default to something sensible
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 if (mode == 0)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 mode = 0711;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 // Start creating the directory stucture
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 do
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 // Split foremost path element out
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 for (save = 0, end = start; path[end] != 0; end++)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 if (path[end] == TH_DIR_SEPARATOR)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 save = path[end];
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 path[end] = 0;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 break;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 }
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 // If the element is there, create it
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 if (path[start] != 0)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 int flags;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
123 BOOL exists = th_stat_path(path, &flags);
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 if (exists && (flags & TH_IS_DIR) == 0)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 goto error;
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 if (!exists)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 {
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 #ifdef TH_PLAT_WINDOWS
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 if (!CreateDirectory(path, NULL))
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 goto error;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 #else
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 if (mkdir(path, mode) < 0)
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 goto error;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 #endif
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
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 // Restore separator character and jump to next element
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 path[end] = save;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 start = end + 1;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 } while (save != 0);
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
144 res = TRUE;
184
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 error:
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 th_free(path);
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 return res;
b256db93cf25 Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 }