-
Notifications
You must be signed in to change notification settings - Fork 1
/
UtilityFunctions.c
119 lines (97 loc) · 2.94 KB
/
UtilityFunctions.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
#include "UtilityFunctions.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // for optionals
#include <syslog.h> // for logging
#include <dirent.h> // for dirent
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utime.h> // for updating time
/// For outputing to syslog simple messages.
void SimpleLog(char* message)
{ syslog (LOG_NOTICE, "%s", message); }
/// return: if there's directory at provided directoryPath.
char DoesDirectoryExistsAt(char* directoryPath)
{
DIR* directoryToCheck = opendir(directoryPath);
if (directoryToCheck == NULL )
return 0;
closedir(directoryToCheck);
return 1;
}
/// return: if there's file at provided filePath.
char DoesFileExistsAt(char* filePath)
{
int fileToCheck = open(filePath, O_WRONLY);
if (fileToCheck <0)
return 0;
close(fileToCheck);
return 1;
}
/// return: File1.ModificationTime - File2.ModificationTime
char CompareModificationTimeOfFiles(char* filePath1, char* filePath2)
{
struct stat file1_stat, file2_stat;
stat(filePath1, &file1_stat);
stat(filePath2, &file2_stat);
return file1_stat.st_mtime - file2_stat.st_mtime;
}
/// Sets receiver's (2nd param) modification time to equals of donor's.
void EqualizeModificationTime(char* donorPath, char* receiverPath)
{
struct stat donor_stat;
struct utimbuf newTime;
stat(donorPath, &donor_stat);
newTime.modtime = donor_stat.st_mtime;
// Update time.
utime(receiverPath, &newTime);
}
/// Sets receiver's (2nd param) privilages to equals of donor's.
void EqualizePrivilages(char* donorPath, char* receiverPath)
{
struct stat donor_stat;
stat(donorPath, &donor_stat);
chmod(receiverPath, donor_stat.st_mode);
}
/// Taken from: https://stackoverflow.com/questions/2256945/removing-a-non-empty-directory-programmatically-in-c-or-c/42978529
/// Removes directory at provided path.
char RemoveDirectoryAt(const char *path)
{
DIR *d = opendir(path);
size_t path_len = strlen(path);
int r = -1;
if (d)
{
struct dirent *p;
r = 0;
while (!r && (p=readdir(d)))
{
int r2 = -1;
char *buf;
size_t len;
/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
len = path_len + strlen(p->d_name) + 2;
buf = malloc(len);
if (buf)
{
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, p->d_name);
if (!stat(buf, &statbuf)) {
if (S_ISDIR(statbuf.st_mode))
r2 = RemoveDirectoryAt(buf);
else
r2 = unlink(buf);
}
free(buf);
}
r = r2;
}
closedir(d);
}
if (!r)
r = rmdir(path);
return r;
}