Hash :
0ca99830
Author :
Thomas de Grivel
Date :
2024-08-06T19:59:21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "assert.h"
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include "buf.h"
#include "buf_save.h"
#include "env.h"
#include "file.h"
#include "io.h"
#include "list.h"
#include "str.h"
#include "sym.h"
#include "time.h"
#include "config.h"
#ifndef O_BINARY
# define O_BINARY 0
#endif
bool file_access (const s_str *path, const s_sym *mode)
{
sw m;
if (mode == &g_sym_r)
m = R_OK;
else if (mode == &g_sym_rw)
m = R_OK | W_OK;
else if (mode == &g_sym_rwx)
m = R_OK | W_OK | X_OK;
else if (mode == &g_sym_rx)
m = R_OK | X_OK;
else if (mode == &g_sym_w)
m = W_OK;
else if (mode == &g_sym_wx)
m = W_OK | X_OK;
else if (mode == &g_sym_x)
m = X_OK;
else
m = F_OK;
return ! access(path->ptr.pchar, m);
}
sw file_copy (const char *from, const char *to)
{
char buf[4096];
s32 e;
sw fd_from = -1;
sw fd_to = -1;
char *out;
sw r;
sw saved_errno;
sw w;
if ((fd_from = open(from, O_RDONLY | O_BINARY)) < 0) {
e = errno;
err_write_1("file_copy: ");
err_write_1(strerror(e));
err_write_1(": ");
err_puts(from);
assert(! "file_copy: failed to open file for reading");
return -1;
}
if ((fd_to = open(to, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) < 0) {
e = errno;
err_write_1("file_copy: ");
err_write_1(strerror(e));
err_write_1(": ");
err_puts(to);
assert(! "file_copy: failed to open file for writing");
goto error;
}
while ((r = read(fd_from, buf, sizeof buf)) > 0) {
out = buf;
do {
if ((w = write(fd_to, out, r)) >= 0) {
r -= w;
out += w;
}
else if (errno != EINTR)
goto error;
} while (r > 0);
}
if (r == 0) {
if (close(fd_to) < 0) {
fd_to = -1;
goto error;
}
close(fd_from);
return 0;
}
error:
saved_errno = errno;
close(fd_from);
if (fd_to >= 0)
close(fd_to);
errno = saved_errno;
return -1;
}
s_str * file_dirname (const s_str *path, s_str *dest)
{
uw dirsep_pos;
assert(path);
assert(dest);
if (! str_rindex_character(path, '/', &dirsep_pos))
return str_init(dest, NULL, 2, "./");
if (! dirsep_pos)
return str_init(dest, NULL, 1, "/");
return str_init_slice(dest, path, 0, dirsep_pos + 1);
}
bool * file_exists (const s_str *path, bool *dest)
{
struct stat sb;
assert(path);
assert(dest);
*dest = ! stat(path->ptr.pchar, &sb);
return dest;
}
s_list ** file_list (const s_str *path, s_list **dest)
{
DIR *dir;
struct dirent *dirent;
s32 e;
s_list **tail;
s_list *tmp = NULL;
dir = opendir(path->ptr.pchar);
if (! dir) {
e = errno;
err_write_1("file_list: opendir: ");
err_write_1(strerror(e));
err_write_1(": ");
err_write_str(path);
return NULL;
}
tail = &tmp;
while ((dirent = readdir(dir))) {
*tail = list_new_str_alloc_copy(dirent->d_namlen,
dirent->d_name, NULL);
if (! *tail) {
list_delete_all(tmp);
closedir(dir);
return NULL;
}
tail = &(*tail)->next.data.list;
}
closedir(dir);
*dest = tmp;
return dest;
}
s_tag * file_mtime (const s_str *path, s_tag *dest)
{
s32 e;
struct stat sb;
assert(path);
assert(dest);
if (stat(path->ptr.pchar, &sb)) {
e = errno;
err_write_1("file_mtime: ");
err_write_1(strerror(e));
err_write_1(": ");
err_puts(path->ptr.pchar);
return NULL;
}
#if HAVE_STAT_MTIM
return time_to_tag(&sb.st_mtim, dest);
#else
s_timespec tmp = {0};
tmp.tv_sec = sb.st_mtime;
tmp.tv_nsec = 0;
return time_to_tag(&tmp, dest);
#endif
}
FILE * file_open (const char *path, const char *mode)
{
s32 e;
FILE *fp;
fp = fopen(path, mode);
if (! fp) {
e = errno;
err_write_1("file_open: ");
err_write_1(strerror(e));
err_write_1(": ");
err_puts(path);
assert(! "facts_open_file: fopen");
return NULL;
}
return fp;
}
s_str * file_pwd (s_str *dest)
{
char buf[PATH_MAX];
char *pchar;
uw size;
if (! getcwd(buf, sizeof(buf)))
return NULL;
size = strlen(buf);
pchar = calloc(1, size + 1);
memcpy(pchar, buf, size);
str_init(dest, pchar, size, pchar);
return dest;
}
s_str * file_search (const s_str *suffix, const s_sym *mode,
s_str *dest)
{
char buf_s[PATH_MAX];
s_buf buf;
const s_list *path;
sw r;
s_buf_save save;
const s_str *str;
s_str tmp = {0};
buf_init(&buf, false, PATH_MAX, buf_s);
if ((r = buf_write_str(&buf, &g_kc3_env.argv0_dir)) < 0)
return NULL;
buf_save_init(&buf, &save);
path = g_kc3_env.path;
while (path) {
if (path->tag.type == TAG_STR) {
buf_save_restore_rpos(&buf, &save);
buf_save_restore_wpos(&buf, &save);
str = &path->tag.data.str;
if ((r = buf_write_str(&buf, str)) < 0 ||
(str->ptr.pchar[str->size - 1] != '/' &&
(r = buf_write_1(&buf, "/")) < 0) ||
(r = buf_write_str(&buf, suffix)) < 0)
return NULL;
buf_read_to_str(&buf, &tmp);
//io_inspect_str(&tmp);
if (file_access(&tmp, mode)) {
*dest = tmp;
return dest;
}
str_clean(&tmp);
}
path = list_next(path);
}
return NULL;
}
s_file_stat * file_stat (const s_str *path, s_file_stat *dest)
{
s32 e;
struct stat sb;
s_file_stat tmp = {0};
assert(path);
assert(dest);
if (stat(path->ptr.pchar, &sb)) {
e = errno;
err_write_1("file_stat: ");
err_write_1(strerror(e));
err_write_1(": ");
err_puts(path->ptr.pchar);
return NULL;
}
tmp.st_dev = sb.st_dev;
tmp.st_ino = sb.st_ino;
tmp.st_mode = NULL;
if (sb.st_mode & S_IFREG)
tmp.st_mode = list_new_sym(&g_sym_file, tmp.st_mode);
else if (sb.st_mode & S_IFDIR)
tmp.st_mode = list_new_sym(&g_sym_directory, tmp.st_mode);
tmp.st_nlink = sb.st_nlink;
tmp.st_uid = sb.st_uid;
tmp.st_gid = sb.st_gid;
tmp.st_rdev = sb.st_rdev;
tmp.st_size = sb.st_size;
tmp.st_blksize = sb.st_blksize;
tmp.st_blocks = sb.st_blocks;
tmp.st_atim.tv_sec = sb.st_atim.tv_sec;
tmp.st_atim.tv_nsec = sb.st_atim.tv_nsec;
tmp.st_mtim.tv_sec = sb.st_mtim.tv_sec;
tmp.st_mtim.tv_nsec = sb.st_mtim.tv_nsec;
tmp.st_ctim.tv_sec = sb.st_ctim.tv_sec;
tmp.st_ctim.tv_nsec = sb.st_ctim.tv_nsec;
*dest = tmp;
return dest;
}