17#define _POSIX_C_SOURCE 200809L
19#define _DEFAULT_SOURCE 1
22# define _DARWIN_C_SOURCE 1
26#include "lilv_config.h"
34# define mkdir(path, flags) _mkdir(path)
35# define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
41#if USE_FLOCK && USE_FILENO
54# define PAGE_SIZE 4096
67 return (isalpha(path[0]) && (path[1] ==
':' || path[1] ==
'|') &&
68 (path[2] ==
'/' || path[2] ==
'\\'));
77 char* buf = (
char*)calloc(len, 1);
85 const char*
const tmpdir =
getenv(
"TMPDIR");
111 const size_t path_len = strlen(path);
112 const size_t dir_len = strlen(dir);
113 return dir && path_len >= dir_len && !strncmp(path, dir, dir_len);
121 return getcwd(
NULL, 0);
131 char* cwd = getcwd(
NULL, 0);
150 const size_t path_len = strlen(path);
151 const size_t base_len = strlen(base);
152 const size_t min_len = (path_len < base_len) ? path_len : base_len;
155 size_t last_shared_sep = 0;
156 for (
size_t i = 0;
i < min_len && path[
i] == base[
i]; ++
i) {
162 if (last_shared_sep == 0) {
169 for (
size_t i = last_shared_sep + 1;
i < base_len; ++
i) {
176 const bool use_slash = strchr(path,
'/');
178 static const bool use_slash =
true;
182 const size_t suffix_len = path_len - last_shared_sep;
183 char* rel = (
char*)calloc(1, suffix_len + (up * 3) + 1);
184 for (
size_t i = 0;
i < up; ++
i) {
186 memcpy(rel + (
i * 3),
"../", 3);
188 memcpy(rel + (
i * 3),
"..\\", 3);
193 memcpy(rel + (up * 3), path + last_shared_sep + 1, suffix_len);
200 const char*
s = path + strlen(path) - 1;
219 char* dirname = (
char*)
malloc(
s - path + 2);
220 memcpy(dirname, path,
s - path + 1);
221 dirname[
s - path + 1] =
'\0';
228 const size_t path_len = strlen(path);
229 size_t last_sep = path_len;
230 for (
size_t i = 0;
i < path_len; ++
i) {
236 if (last_sep >= path_len) {
240 const size_t ret_len = path_len - last_sep;
241 char*
const ret = (
char*)calloc(ret_len + 1, 1);
243 strncpy(ret, path + last_sep + 1, ret_len);
254 const size_t a_len = strlen(
a);
255 const size_t b_len =
b ? strlen(
b) : 0;
257 const size_t pre_len = a_len - (a_end_is_sep ? 1 : 0);
258 char* path = (
char*)calloc(1, a_len + b_len + 2);
263 const bool a_has_slash = strchr(
a,
'/');
264 const bool b_has_slash =
b && strchr(
b,
'/');
265 if (a_has_slash || b_has_slash) {
268 path[pre_len] =
'\\';
275 memcpy(path + pre_len + 1,
294 char* real_path = realpath(path,
NULL);
304 return !lstat(path, &st);
306 return !access(path, F_OK);
314 return !stat(path, &st) &&
S_ISDIR(st.st_mode);
320 FILE*
in = fopen(src,
"r");
325 FILE*
out = fopen(dst,
"w");
335 if (fwrite(
page, 1, n_read,
out) != n_read) {
341 if (!st && fflush(
out)) {
345 if (!st && (ferror(
in) || ferror(
out))) {
360 if (strcmp(oldpath, newpath)) {
362 ret = !CreateHardLink(newpath, oldpath, 0);
366 ret = symlink(target, newpath);
379 OVERLAPPED overlapped = {0};
383 (LOCKFILE_EXCLUSIVE_LOCK | (block ? 0 : LOCKFILE_FAIL_IMMEDIATELY));
385 return !LockFileEx(handle, flags, 0, UINT32_MAX, UINT32_MAX, &overlapped);
387 return !UnlockFileEx(handle, 0, UINT32_MAX, UINT32_MAX, &overlapped);
389#elif USE_FLOCK && USE_FILENO
390 return flock(fileno(
file),
391 (lock ? LOCK_EX : LOCK_UN) | (block ? 0 : LOCK_NB));
400 void (*
f)(
const char* path,
const char*
name,
void*
data))
405 HANDLE fh = FindFirstFile(pat, &fd);
406 if (fh != INVALID_HANDLE_VALUE) {
408 if (strcmp(fd.cFileName,
".") && strcmp(fd.cFileName,
"..")) {
409 f(path, fd.cFileName,
data);
411 }
while (FindNextFile(fh, &fd));
416 DIR* dir = opendir(path);
418 for (
struct dirent* entry =
NULL; (entry = readdir(dir));) {
419 if (strcmp(entry->d_name,
".") && strcmp(entry->d_name,
"..")) {
420 f(path, entry->d_name,
data);
432 static const char chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
433 static const int n_chars =
sizeof(chars) - 1;
435 const size_t pattern_len = strlen(
pattern);
436 if (pattern_len < 7 || strcmp(
pattern + pattern_len - 6,
"XXXXXX")) {
442 const size_t path_pattern_len = strlen(path_pattern);
443 char*
const suffix = path_pattern + path_pattern_len - 6;
445 for (
unsigned attempt = 0; attempt < 128; ++attempt) {
446 for (
unsigned i = 0;
i < 6; ++
i) {
447 suffix[
i] = chars[rand() % n_chars];
450 if (!mkdir(path_pattern, 0700)) {
459 return mkdtemp(path_pattern);
478 const size_t path_len = strlen(path);
487 for (;
i <= path_len; ++
i) {
488 const char c = path[
i];
507 if (stat(path, &buf)) {
518 return !RemoveDirectory(path);
528 if (!strcmp(a_path, b_path)) {
537 if (!strcmp(a_real, b_real)) {
541 }
else if (!(a_file = fopen(a_real,
"rb")) ||
542 !(b_file = fopen(b_real,
"rb"))) {
547 while (!feof(a_file) && !feof(b_file)) {
548 if (fgetc(a_file) != fgetc(b_file)) {
uint8_t a
Definition Spc_Cpu.h:141
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned f
Definition inflate.c:1572
char * lilv_path_canonical(const char *path)
Definition filesystem.c:283
char * lilv_path_current(void)
Return the current working directory.
Definition filesystem.c:119
static bool lilv_is_dir_sep(const char c)
Definition filesystem.c:58
char * lilv_create_temporary_directory(const char *pattern)
Definition filesystem.c:464
char * lilv_path_absolute_child(const char *path, const char *parent)
Definition filesystem.c:138
char * lilv_path_parent(const char *path)
Definition filesystem.c:198
char * lilv_temp_directory_path(void)
Return the path to a directory suitable for making temporary files.
Definition filesystem.c:73
bool lilv_path_is_absolute(const char *path)
Return true iff path is an absolute path.
Definition filesystem.c:92
int lilv_create_directories(const char *dir_path)
Definition filesystem.c:475
char * lilv_path_relative_to(const char *path, const char *base)
Definition filesystem.c:148
int lilv_flock(FILE *file, bool lock, bool block)
Definition filesystem.c:375
bool lilv_is_directory(const char *path)
Return true iff path points to an existing directory.
Definition filesystem.c:311
#define PAGE_SIZE
Definition filesystem.c:54
char * lilv_path_absolute(const char *path)
Definition filesystem.c:125
void lilv_dir_for_each(const char *path, void *data, void(*f)(const char *path, const char *name, void *data))
Definition filesystem.c:398
int lilv_copy_file(const char *src, const char *dst)
Definition filesystem.c:318
int lilv_remove(const char *path)
Remove the file or empty directory at path.
Definition filesystem.c:514
static off_t lilv_file_size(const char *path)
Definition filesystem.c:504
bool lilv_path_is_child(const char *path, const char *dir)
Return true iff path is a child of dir.
Definition filesystem.c:108
char * lilv_path_join(const char *a, const char *b)
Join path a and path b with a single directory separator between them.
Definition filesystem.c:248
char * lilv_create_temporary_directory_in(const char *pattern, const char *parent)
Definition filesystem.c:429
bool lilv_file_equals(const char *a_path, const char *b_path)
Return true iff the given paths point to files with identical contents.
Definition filesystem.c:526
bool lilv_path_exists(const char *path)
Return true iff path points to an existing file system entry.
Definition filesystem.c:300
int lilv_symlink(const char *oldpath, const char *newpath)
Definition filesystem.c:357
char * lilv_path_filename(const char *path)
Definition filesystem.c:226
static const char * name
Definition pugl.h:1582
static uintptr_t parent
Definition pugl.h:1644
JSAMPIMAGE data
Definition jpeglib.h:945
#define LILV_DIR_SEP
Definition juce_lv2_config.h:54
static SerdStatus page(SerdReader *reader)
Definition reader.c:112
float in
Definition lilv_test.c:1460
float out
Definition lilv_test.c:1461
unsigned int DWORD
Definition swell-types.h:164
void * HANDLE
Definition swell-types.h:212
void GetTempPath(int bufsz, char *buf)
Definition swell.cpp:1063
#define off_t
Definition thread-link.cpp:7
return c
Definition crypt.c:175
memcpy(hh, h, RAND_HEAD_LEN)
int * pattern
Definition match.c:126
int match(string, pattern, ignore_case __WDL) ZCONST char *string
int result
Definition process.c:1455
#define dirent
Definition unix.c:70
closedir((DIR *) G.wild_dir)
#define S_ISDIR(m)
Definition unzpriv.h:1322
struct zdirent * file
Definition win32.c:1500