Repository navigation
Expand file tree
/
Copy pathFiles.c
More file actions
148 lines (130 loc) · 4.37 KB
/
Copy pathFiles.c
File metadata and controls
148 lines (130 loc) · 4.37 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include "Prototypes.h"
/*{
typedef bool(*Method_Files_fileHandler)(void*, char*);
}*/
FILE* Files_open(const char* mode, const char* picture, const char* value) {
char fileName[4097];
char* dataDir = PKGDATADIR;
char* sysconfDir = SYSCONFDIR;
FILE* fd = Files_openHome(mode, picture, value);
if (fd) return fd;
snprintf(fileName, 4096, "%s/dit/", sysconfDir);
int len = strlen(fileName);
snprintf(fileName + len, 4096 - len, picture, value);
fd = fopen(fileName, mode);
if (fd) return fd;
snprintf(fileName, 4096, "%s/", dataDir);
len = strlen(fileName);
snprintf(fileName + len, 4096 - len, picture, value);
fd = fopen(fileName, mode);
return fd;
}
static void Files_nameHome(char* fileName, const char* picture, const char* value) {
char* homeDir = getenv("HOME");
snprintf(fileName, 4096, "%s/.dit/", homeDir);
int len = strlen(fileName);
snprintf(fileName + len, 4096 - len, picture, value);
}
static char* tryToFind(const char* dir, const char* pattern, const char* picture, const char* value, int* dirEndsAt) {
char fileName[4097];
snprintf(fileName, 4096, pattern, dir);
int len = strlen(fileName);
if (dirEndsAt) *dirEndsAt = len;
snprintf(fileName + len, 4096 - len, picture, value);
if (access(fileName, R_OK) == 0) return strdup(fileName);
return NULL;
}
char* Files_findFile(const char* picture, const char* value, int* dirEndsAt) {
char* found = tryToFind(getenv("HOME"), "%s/.dit/", picture, value, dirEndsAt);
if (found) return found;
found = tryToFind(SYSCONFDIR, "%s/dit/", picture, value, dirEndsAt);
if (found) return found;
found = tryToFind(PKGDATADIR, "%s/", picture, value, dirEndsAt);
if (found) return found;
return NULL;
}
bool Files_existsHome(const char* picture, const char* value) {
char fileName[4097];
Files_nameHome(fileName, picture, value);
return (access(fileName, F_OK) == 0);
}
FILE* Files_openHome(const char* mode, const char* picture, const char* value) {
char fileName[4097];
Files_nameHome(fileName, picture, value);
return fopen(fileName, mode);
}
int Files_deleteHome(const char* picture, const char* value) {
char fileName[4097];
Files_nameHome(fileName, picture, value);
return unlink(fileName);
}
char* Files_encodePathAsFileName(char* fileName) {
char* rpath;
if (fileName[0] == '/')
rpath = strdup(fileName);
else
rpath = realpath(fileName, NULL);
for(char *c = rpath; *c; c++)
if (*c == '/')
*c = ':';
return rpath;
}
void Files_makeHome() {
static const char* dirs[] = {
"%s/.dit",
"%s/.dit/undo",
"%s/.dit/lock",
NULL
};
char fileName[4097];
const char* home = getenv("HOME");
for (int i = 0; dirs[i]; i++) {
snprintf(fileName, 4096, dirs[i], home);
fileName[4095] = '\0';
mkdir(fileName, 0700);
}
}
void Files_forEachInDir(char* dirName, Method_Files_fileHandler fileHandler, void* data) {
char homeName[2049];
char* homeDir = getenv("HOME");
snprintf(homeName, sizeof(homeName), "%s/.dit/", homeDir);
int homeLen = strlen(homeName);
snprintf(homeName + homeLen, sizeof(homeName) - homeLen, "%s", dirName);
DIR* dir = opendir(homeName);
while (dir) {
struct dirent* entry = readdir(dir);
if (!entry) break;
if (entry->d_name[0] == '.') continue;
snprintf(homeName + homeLen, sizeof(homeName) - homeLen, "%s/%s", dirName, entry->d_name);
if (fileHandler(data, homeName)) {
if (dir) closedir(dir);
return;
}
}
if (dir) closedir(dir);
char dataName[2049];
char* dataDir = PKGDATADIR;
snprintf(dataName, sizeof(dataName), "%s/", dataDir);
int dataLen = strlen(dataName);
snprintf(dataName + dataLen, sizeof(dataName) - homeLen, "%s", dirName);
dir = opendir(dataName);
while (dir) {
struct dirent* entry = readdir(dir);
if (!entry) break;
if (entry->d_name[0] == '.') continue;
snprintf(homeName + homeLen, sizeof(dataName) - homeLen, "%s/%s", dirName, entry->d_name);
if (access(homeName, R_OK) == 0) {
continue;
}
snprintf(dataName + dataLen, sizeof(dataName) - dataLen, "%s/%s", dirName, entry->d_name);
if (fileHandler(data, dataName)) {
if (dir) closedir(dir);
return;
}
}
if (dir) closedir(dir);
}