forked from Matthias-Wandel/imgcomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
executable file
·468 lines (407 loc) · 14.8 KB
/
util.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//-----------------------------------------------------------------------------------
// imgcomp Various utility functions, paths, directories, file copying, log rotation
// Matthias Wandel 2015-2020
//
// Imgcomp is licensed under GPL v2 (see README.txt)
//-----------------------------------------------------------------------------------
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <utime.h>
#include <errno.h>
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
#include "imgcomp.h"
static int BackupImageCount = 0;
static int CopyFile(char * src, char * dest);
static void CopyJpgFileCmd(char * src, char * dest);
//-----------------------------------------------------------------------------------
// Concatenate dir name and file name. Not thread safe!
//-----------------------------------------------------------------------------------
char * CatPath(char *Dir, char * FileName)
{
static char catpath[502];
int pathlen;
pathlen = strlen(Dir);
if (pathlen > 300){
fprintf(Log, "path too long!");
exit(-1);
}
memcpy(catpath, Dir, pathlen+1);
if (catpath[pathlen-1] != '/' && catpath[pathlen-1] != '\\'){
catpath[pathlen] = '/';
pathlen += 1;
}
strncpy(catpath+pathlen, FileName,256);
return catpath;
}
//--------------------------------------------------------------------------------
// Ensure that a path exists
//--------------------------------------------------------------------------------
int EnsurePathExists(const char * FileName, int filepath)
{
char NewPath[PATH_MAX*2];
int a;
int LastSlash = 0;
//printf("Ensure path exists '%s' %d\n",FileName, filepath);
// Extract the path component of the file name.
strcpy(NewPath, FileName);
a = strlen(NewPath);
if (!filepath){
// If it's not a path to a file, add a trailing slash for the existing
// code to work properly.
if (NewPath[a-1] != '/'){
NewPath[a++] = '/';
}
}
for (;;){
a--;
if (a == 0){
NewPath[0] = 0;
break;
}
if (NewPath[a] == '/'){
struct stat dummy;
NewPath[a] = 0;
if (stat(NewPath, &dummy) == 0){
if (S_ISDIR(dummy.st_mode)){
// Break out of loop, and go forward along path making
// the directories.
if (LastSlash == 0){
// Full path exists. No need to create any directories.
return 1;
}
break;
}else{
// Its a file.
printf("Can't create path '%s' due to file conflict\n",NewPath);
fprintf(Log,"Can't create path '%s' due to file conflict\n",NewPath);
exit(-1);
}
}
if (LastSlash == 0) LastSlash = a;
}
}
// Now work forward.
for(;FileName[a];a++){
//printf("%d/%d: '%c'\n",a,LastSlash,FileName[a]);
if (FileName[a] == '/' || a == 0){
if (a == LastSlash) break;
NewPath[a] = FileName[a];
#ifdef _WIN32
if (NewPath[1] == ':' && strlen(NewPath) == 2) continue;
#endif
fprintf(Log,"Make directory %s\n",NewPath);
if (mkdir(NewPath,0777)){
printf("Could not create directory '%s'\n",NewPath);
fprintf(Log,"Could not create directory '%s'\n",NewPath);
// Failed to create directory.
exit(-1);
}
}
}
return 1;
}
//-----------------------------------------------------------------------------------
// Compare file names to sort directory.
//-----------------------------------------------------------------------------------
static int fncmpfunc (const void * a, const void * b)
{
return strcmp( ((DirEntry_t*)a)->FileName, ((DirEntry_t*)b)->FileName );
}
//-----------------------------------------------------------------------------------
// Read a directory and sort it.
//-----------------------------------------------------------------------------------
DirEntry_t * GetSortedDir(char * Directory, int * NumFiles)
{
DirEntry_t * FileNames;
int NumFileNames;
int NumAllocated;
DIR * dirp;
NumAllocated = 10;
FileNames = malloc(sizeof (DirEntry_t) * NumAllocated);
NumFileNames = 0;
dirp = opendir(Directory);
if (dirp == NULL){
fprintf(Log, "could not open dir\n");
return NULL;
}
for (;;){
struct dirent * dp;
struct stat buf;
int l;
dp = readdir(dirp);
if (dp == NULL) break;
//printf("name: %s %d %d\n",dp->d_name, (int)dp->d_off, (int)dp->d_reclen);
// Check that it's a regular file.
stat(CatPath(Directory, dp->d_name), &buf);
if (!S_ISREG(buf.st_mode)) continue; // not a file.
if (NumFileNames >= NumAllocated){
NumAllocated *= 2;
FileNames = realloc(FileNames, sizeof (DirEntry_t) * NumAllocated);
}
l = strlen(dp->d_name);
if (l >= 40){
printf("Filename too long %s",dp->d_name);
continue;
}
strncpy(FileNames[NumFileNames].FileName,dp->d_name,40);
FileNames[NumFileNames].MTime = buf.st_mtime;
FileNames[NumFileNames].ATime = buf.st_atime;
FileNames[NumFileNames].FileSize = buf.st_size;
NumFileNames += 1;
}
closedir(dirp);
// Now sort the names (could be in random order)
qsort(FileNames, NumFileNames, sizeof(DirEntry_t), fncmpfunc);
*NumFiles = NumFileNames;
return FileNames;
}
//-----------------------------------------------------------------------------------
// Unallocate directory structure
//-----------------------------------------------------------------------------------
void FreeDir(DirEntry_t * FileNames, int NumEntries)
{
free (FileNames);
}
//-----------------------------------------------------------------------------------
// Make a name from the file date.
//-----------------------------------------------------------------------------------
static void DestNameFromTime(char * DestPath, const char * KeepPixDir, time_t PicTime, char * NameSuffix)
{
struct tm tm;
char * p;
tm = *localtime(&PicTime);
// %d Day of month as decimal number (01 – 31)
// %H Hour in 24-hour format (00 – 23)
// %j Day of year as decimal number (001 – 366)
// %m Month as decimal number (01 – 12)
// %M Minute as decimal number (00 – 59)
// %S Second as decimal number (00 – 59)
// %U Week of year as decimal number, with Sunday as first day of week (00 – 53)
// %w Weekday as decimal number (0 – 6; Sunday is 0)
// %y Year without century, as decimal number (00 – 99)
// %Y Year with century, as decimal number
p = DestPath+sprintf(DestPath, "%s/", KeepPixDir);
// Using this rule, make a subdiretories for date and for hour.
p += strftime(p, PATH_MAX, SaveNames, &tm);
sprintf(p, "%s",NameSuffix);
}
//-----------------------------------------------------------------------------------
// Back up a photo or video file that is of interest or applies to tiemelapse.
// Or, if "DoNotCopy" is set, just make sure the directory exists.
//-----------------------------------------------------------------------------------
char * BackupImageFile(char * Name, int DiffMag, int DoNotCopy)
{
static char DstPath[500];
static char SuffixChar = ' ';
static time_t LastSaveTime;
char * extension;
int a;
struct stat statbuf;
time_t mtime;
if (SaveDir[0] == '\0') return NULL; // Picture saving not enabled.
if (stat(Name, &statbuf) == -1) {
perror(Name);
exit(1);
}
mtime = statbuf.st_mtime;
// Get extension (.jpg or .mp4) of file we started with.
extension = "\0";
for (a=0;Name[a];a++){
if (Name[a] == '.') extension = Name+a;
}
{
char NameSuffix[20];
if (LastSaveTime == mtime){
// If it's the same second, cycle through suffixes a-z
SuffixChar = (SuffixChar >= 'a' && SuffixChar <'z') ? SuffixChar+1 : 'a';
}else{
// New time. No need for a suffix.
SuffixChar = ' ';
LastSaveTime = mtime;
}
sprintf(NameSuffix, "%c%04d%s",SuffixChar, DiffMag, extension);
DestNameFromTime(DstPath, SaveDir, mtime, NameSuffix);
EnsurePathExists(DstPath, 1);
if (!DoNotCopy){
if (CopyJpgCmd[0]){
// Apply a command, such as jpegtran to copy the file
CopyJpgFileCmd(Name, DstPath);
}else{
// Just copy it from inside the program.
CopyFile(Name, DstPath);
}
}
}
BackupImageCount ++;
return DstPath;
}
//-----------------------------------------------------------------------------------
// Copy a file from within the program.
//-----------------------------------------------------------------------------------
static int CopyFile(char * src, char * dest)
{
int inputFd, outputFd, openFlags;
int filePerms;
struct stat statbuf;
size_t numRead;
#define BUF_SIZE 8192
char buf[BUF_SIZE];
// Get file modification time from old file.
stat(src, &statbuf);
// Open input and output files
inputFd = open(src, O_RDONLY, 0);
if (inputFd == -1){
fprintf(Log,"CopyFile could not open src %s\n",src);
exit(-1);
}
openFlags = O_CREAT | O_WRONLY | O_TRUNC;
filePerms = 0x1ff;
outputFd = open(dest, openFlags, filePerms);
if (outputFd == -1){
fprintf(Log,"CopyFile could not open dest %s\n",dest);
exit(-1);
}
// Transfer data until we encounter end of input or an error
for(;;){
numRead = read(inputFd, buf, BUF_SIZE);
if (numRead <= 0) break;
if (write(outputFd, buf, numRead) != numRead){
fprintf(Log,"write error to %s",dest);
exit(-1);
}
}
if (numRead == -1){
fprintf(Log,"CopyFile read error from %s\n",src);
exit(-1);
}
close(inputFd);
close(outputFd);
{
struct utimbuf mtime;
mtime.actime = statbuf.st_ctime;
mtime.modtime = statbuf.st_mtime;
utime(dest, &mtime);
}
return 0;
}
//-----------------------------------------------------------------------------------
// Copy file by shell command, so that jpegtran can be used to optimize the file.
//-----------------------------------------------------------------------------------
static void CopyJpgFileCmd(char * src, char * dest)
{
// Build the exec string. &i and &o in the exec string get replaced by input and output files.
char ExecString[PATH_MAX*2];
int e = 0;
for (int a=0;;a++){
if (e >= sizeof(ExecString)){
fprintf(stderr, "Copy command too long\n");
exit(-1);
}
if (CopyJpgCmd[a] == '&' && (CopyJpgCmd[a+1] == 'i' || CopyJpgCmd[a+1] == 'o')){
if (CopyJpgCmd[a+1] == 'o'){
if (CopyJpgCmd[a+2] == '.'){
// If it's specified "&o.", that means cut off the old extension.
// That way, we can specify a copyjpgcmd in imgcomp.conf to convert
// to a different image format. Example using imagemagick convert:
// copyjpgcmd = convert -quality 60 "&i" "&o.webp"
int extension = 0;
for (int b=0;dest[b];b++){
if (dest[b] == '.') extension = b;
}
if (extension) dest[extension] = '\0';
}
strncpy(ExecString+e, dest, sizeof(ExecString)-e-1);
}else{
strncpy(ExecString+e, src, sizeof(ExecString)-e-1);
}
a += 1;
e = strlen(ExecString);
continue;
}
ExecString[e++] = CopyJpgCmd[a];
if (CopyJpgCmd[a] == 0) break;
}
//printf("Cmd:%s\n",ExecString);
errno = 0;
int err = system(ExecString);
if (err || errno){
if (errno) perror("system");
fprintf(Log, "jpeg copy command failed (%d)\n",err);
}
}
//-----------------------------------------------------------------------------------
// Open and / or rotate logfiles
//-----------------------------------------------------------------------------------
void LogFileMaintain(int ForceLogSaveReboot)
{
static char ThisLogTo[PATH_MAX];
char NewLogTo[PATH_MAX];
int HaveLogAlready;
if (LogToFile[0] == 0){
Log = stdout;
return;
}
HaveLogAlready = (Log != NULL);
if (MoveLogNames[0]){
strftime(NewLogTo, PATH_MAX, MoveLogNames, localtime(&LastPic_mtime));
if (ForceLogSaveReboot){
// Just change the old name, so it gets backed up, and not overwritten
// by new log after reboot.
strcat(ThisLogTo, " reboot");
printf("modified log name: %s\n",ThisLogTo);
}
if (strcmp(ThisLogTo, NewLogTo)){
if (Log != NULL){
if (BackupImageCount){
printf("Log rotate %s --> %s\n", ThisLogTo, NewLogTo);
fprintf(Log,"Log rotate %s --> %s\n", ThisLogTo, NewLogTo);
EnsurePathExists(ThisLogTo, 1);
fclose(Log);
Log = NULL;
CopyFile(LogToFile, ThisLogTo);
unlink(LogToFile);
BackupImageCount = 0;
}else{
fprintf(Log,"Skip log rotate to %s (no images were saved in dir)\n", NewLogTo);
strncpy(ThisLogTo, NewLogTo, PATH_MAX);
}
}
}
}
if (Log == NULL){
Log = fopen(LogToFile,"a");
if (Log == NULL){
fprintf(stderr, "Failed to open log file %s\n",LogToFile);
exit(-1);
}
if (HaveLogAlready){
fprintf(Log,"<pre>Rotated log file (previous %s)\n",ThisLogTo);
}else{
fprintf(Log,"\n\n----------Restarting imcomp, keep old log---------------\n");
}
if (ThisLogTo[0]){
strncpy(ThisLogTo, NewLogTo, PATH_MAX);
}
strcpy(ThisLogTo, NewLogTo);
}
static int lastmin = 0;
time_t now;
time(&now);
struct tm * nowTm = localtime(&now);
if (nowTm->tm_min != lastmin && Log != NULL){
// Put an indexable tag into the log file every minute.
fprintf(Log, "<z id=\"%02d\"/>\n", nowTm->tm_min);
lastmin = nowTm->tm_min;
}
fflush(Log); // Assuming logging to ramdisk, or we wear out flash.
}