aboutsummaryrefslogtreecommitdiff
path: root/src/fs.c
blob: 58bb43d90d689b24ed850b7a756461899dc11a6f (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "fs.h"

#include "log.h"
#include "config.h"
#include "vector.h"

#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <dirent.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#ifdef __linux__
#include <sys/sendfile.h>
#endif

#define BUFSIZE 8192

#define CONTENTDIR "content"
#define DEFAULTALBUM "unorganized"

/*
 * File extensions based on which we add files to the list of images to be
 * processed.
 */
static const char *img_extensions[] = {
	"jpg",
	"jpeg",
	"png",
	"tiff",
	NULL,
};

static void
hm_destroy_callback(const void *k, void *v)
{
	free(v);
}

const char *
rbasename(const char *path)
{
	char *delim = strrchr(path, '/');
	if (delim == NULL) {
		return path;
	}
	return delim + 1;
}

enum nmkdir_res
nmkdir(const char *path, struct stat *dstat, bool dry)
{
	if (dry) {
		if (stat(path, dstat)) {
			if (errno == ENOENT) {
				log_printl(LOG_DETAIL, "Created directory %s", path);
				return NMKDIR_CREATED;
			}
			log_printl_errno(LOG_FATAL, "Can't read %s", path);
			return NMKDIR_ERROR;
		}
		if (!S_ISDIR(dstat->st_mode)) {
			log_printl(LOG_FATAL, "%s is not a directory", path);
			return NMKDIR_ERROR;
		}
		return NMKDIR_NOOP;
	}

	if(mkdir(path, 0755) < 0) {
		if (errno == EEXIST) {
			if (stat(path, dstat)) {
				log_printl_errno(LOG_FATAL, "Can't read %s", path);
				return NMKDIR_ERROR;
			}
			if (!S_ISDIR(dstat->st_mode)) {
				log_printl(LOG_FATAL, "%s is not a directory", path);
				return NMKDIR_ERROR;
			}
			return NMKDIR_NOOP;
		} else {
			log_printl_errno(LOG_FATAL, "Can't make directory %s", path);
			return NMKDIR_ERROR;
		}
	}
	log_printl(LOG_DETAIL, "Created directory %s", path);
	return NMKDIR_CREATED;
}

char *
joinpath(const char *restrict a, const char *restrict b)
{
	char *fpath = malloc(strlen(a) + strlen(b) + 2);
	joinpathb(fpath, a, b);

	return fpath;
}

bool
isimage(const char *fname)
{
	char *ext = strrchr(fname, '.');
	if (!ext || *(ext + 1) == '\0') return false;
	
	ext++;
	for (size_t i = 0; img_extensions[i] != NULL; i++) {
		if (!strcasecmp(ext, img_extensions[i])) return true;
	}

	return false;
}

const char *
delext(const char *restrict basename, char *restrict buf, size_t n)
{
	const char *ext = strrchr(basename, '.');
	size_t i = ext - basename;
	if (i + 1 > n) return NULL;
	memcpy(buf, basename, i);
	buf[i] = '\0';

	return ext;
}

int
file_is_uptodate(const char *path, const struct timespec *srcmtim)
{
	struct stat dststat;
	if (stat(path, &dststat)) {
		if (errno != ENOENT) {
			log_printl_errno(LOG_FATAL, "Can't read file %s", path);
			return -1;
		}
		return 0;
	} else if (dststat.st_mtim.tv_sec != srcmtim->tv_sec
			|| dststat.st_mtim.tv_nsec != srcmtim->tv_nsec) {
		return 0;
	}

	return 1;
}

void
setdatetime(const char *path, const struct timespec *mtim)
{
	struct timespec tms[] = {
		{ .tv_sec = mtim->tv_sec, .tv_nsec = mtim->tv_nsec },
		{ .tv_sec = mtim->tv_sec, .tv_nsec = mtim->tv_nsec },
	};
	if (utimensat(AT_FDCWD, path, tms, 0) == -1) {
		log_printl_errno(LOG_ERROR, "Warning: couldn't set times of %s", path);
	}
}

bool
rmentry(const char *path)
{
	struct stat st;
	if (stat(path, &st)) {
		log_printl_errno(LOG_ERROR, "Can't stat file %s", path);
		return false;
	}

	if (S_ISDIR(st.st_mode)) {
		DIR *dir = opendir(path);
		struct dirent *ent;
		while ((ent = readdir(dir))) {
			if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
				continue;
			}

			char target[PATH_MAX];
			sprintf(target, "%s/%s", path, ent->d_name);
			if (!rmentry(target)) {
				closedir(dir);
				return false;
			}
		}

		closedir(dir);
		if (rmdir(path)) goto error;
		goto success;
	}

	if (unlink(path)) goto error;

success:
	log_printl(LOG_DETAIL, "Deleted %s", path);
	return true;
error:
	log_printl_errno(LOG_ERROR, "Can't delete %s", path);
	return false;
}

ssize_t
rmextra(const char *path, struct hashmap *preserved)
{
	ssize_t removed = 0;
	DIR *dir = opendir(path);
	if (dir == NULL) return -1;

	struct dirent *ent;
	while ((ent = readdir(dir))) {
		if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
			continue;
		}

		if (hashmap_get(preserved, ent->d_name) != NULL) continue;

		char target[PATH_MAX];
		sprintf(target, "%s/%s", path, ent->d_name);
		if (!rmentry(target)) {
			closedir(dir);
			return -1;
		}
		removed++;
	}

	closedir(dir);
	return removed;
}

bool
filesync(const char *restrict srcpath, const char *restrict dstpath,
		struct hashmap *preserved)
{
	int fdsrc;
	struct stat stsrc;
	struct vector *own = NULL;
	bool cleanup = false;

	fdsrc = open(srcpath, O_RDONLY);
	if (fdsrc < 0) {
		log_printl_errno(LOG_ERROR, "Failed to open %s", srcpath);
		return false;
	}
	if (fstat(fdsrc, &stsrc)) {
		log_printl_errno(LOG_ERROR, "Failed to stat %s", srcpath);
		goto dir_error;
	}

	if (S_ISDIR(stsrc.st_mode)) {
		if (mkdir(dstpath, 0755)) {
			if (errno != EEXIST) {
				log_printl_errno(LOG_ERROR, "Failed to create directory %s",
						dstpath);
				goto dir_error;
			}
			if (stat(dstpath, &stsrc)) {
				log_printl_errno(LOG_ERROR, "Failed to stat %s", dstpath);
				goto dir_error;
			}
			if (!S_ISDIR(stsrc.st_mode)){
				log_printl(LOG_ERROR, "%s is not a directory", dstpath);
				errno = ENOTDIR;
				goto dir_error;
			}
			/* We only need to cleanup if the dir already existed */
			cleanup = true;
		}

		if (cleanup) {
			if (preserved == NULL) {
				preserved = hashmap_new();
			} else {
				own = vector_new(32);
			}
		}

		DIR *dir = fdopendir(fdsrc);
		if (dir == NULL) {
			log_printl_errno(LOG_ERROR, "Failed to open directory %s", dstpath);
			goto dir_error;
		}

		struct dirent *ent;
		while ((ent = readdir(dir))) {
			if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
				continue;
			}
			char entsrc[PATH_MAX], entdst[PATH_MAX];
			sprintf(entsrc, "%s/%s", srcpath, ent->d_name);
			sprintf(entdst, "%s/%s", dstpath, ent->d_name);
			if (cleanup) {
				char *name = strdup(ent->d_name);
				hashmap_insert(preserved, name, name);
				if (own) {
					vector_push(own, name);
				}
			}
			if (!filesync(entsrc, entdst, NULL)) {
				closedir(dir);
				return false;
			}
		}

		if (cleanup) {
			rmextra(dstpath, preserved);
			if (own) {
				for (size_t i = 0; i < own->size; i++) {
					free(own->values[i]);
				}
				vector_free(own);
			} else {
				hashmap_destroy(preserved, hm_destroy_callback);
			}
		}

		closedir(dir);
		return true;
	}

	int fddst, uptodate;
	if ((uptodate = file_is_uptodate(dstpath, &stsrc.st_mtim)) > 0) {
		goto success;
	} else if (uptodate < 0) {
		goto dir_error;
	}
	fddst = open(dstpath, O_CREAT | O_WRONLY | O_TRUNC, 0644);
	if (fddst < 0) {
		log_printl_errno(LOG_ERROR, "Failed to open/create %s", dstpath);
		goto dir_error;
	}

#ifdef __linux__
	ssize_t nwrote = sendfile(fddst, fdsrc, NULL, stsrc.st_size);
	if (nwrote != stsrc.st_size) {
		log_printl_errno(LOG_ERROR, "Failed to copy %s (wrote %lu/%lu bytes)", 
				dstpath, nwrote, stsrc.st_size);
		goto copy_error;
	}
#else
	char buf[BUFSIZE];
	ssize_t nread;

	while ((nread = read(fdsrc, buf, BUFSIZE)) > 0) {
		ssize_t nwrote = write(fddst, buf, nread);
		if (nread != nwrote) {
			log_printl_errno(LOG_ERROR, "Failed to copy %s "
					"(buffer wrote %lu/%lu bytes)", 
					dstpath, nwrote, nread);
			goto copy_error;
		}
	}
	if (nread < 0) {
		log_printl_errno(LOG_ERROR, "Failed to copy %s");
		goto copy_error;
	}
#endif

	struct timespec tms[] = {
		{ .tv_sec = stsrc.st_mtim.tv_sec, .tv_nsec = stsrc.st_mtim.tv_nsec },
		{ .tv_sec = stsrc.st_mtim.tv_sec, .tv_nsec = stsrc.st_mtim.tv_nsec },
	};
	futimens(fddst, tms);

	log_printl(LOG_DETAIL, "Copied %s", srcpath);

	close(fddst);
success:
	close(fdsrc);
	return true;
copy_error:
	close(fddst);
dir_error:
	close(fdsrc);
	return false;
}