-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinux.cpp
124 lines (109 loc) · 2.8 KB
/
linux.cpp
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
struct Filetime
{
time_t filetime;
};
struct ProcessInformation
{
pid_t processID;
};
Filetime GetLastWriteTime(char *filename)
{
Filetime result = {};
struct stat fileData = {};
stat(filename, &fileData);
result.filetime = fileData.st_mtime;
return result;
}
int CompareFiletime(Filetime *first, Filetime *second)
{
/*
-1 First early then second
0 First same as second
1 First late then second
*/
double seconds = difftime(first->filetime, second->filetime);
if(seconds < 0.0)
{
return -1;
}
else if(seconds == 0.0)
{
return 0;
}
else
{
return 1;
}
}
void CopyFile(char *sourceFilepath, char *destinationFilepath)
{
int sourceFD = open(sourceFilepath, O_RDONLY);
struct stat sourceFileData = {};
stat(sourceFilepath, &sourceFileData);
char *buffer = new char[sourceFileData.st_size];
if(sourceFD == -1)
{
fprintf(stderr, "ERROR: Could not open source file:%s for copying\n", sourceFilepath);
delete[] buffer;
close(sourceFD);
return;
}
ssize_t bytesRead = read(sourceFD, (void *)buffer, sourceFileData.st_size);
if(bytesRead != sourceFileData.st_size)
{
fprintf(stderr, "ERROR: Could not read source file:%s for copying\n", sourceFilepath);
delete[] buffer;
close(sourceFD);
return;
}
close(sourceFD);
int destinationFD = open(destinationFilepath, O_WRONLY | O_CREAT);
if(destinationFD == -1)
{
fprintf(stderr, "ERROR: Could not open destination file:%s for copying\n", destinationFilepath);
delete[] buffer;
close(destinationFD);
return;
}
ssize_t bytesWritten = write(destinationFD, (void *)buffer, sourceFileData.st_size);
if(bytesWritten != sourceFileData.st_size)
{
fprintf(stderr, "ERROR: Could not write to destination file:%s while copying\n", destinationFilepath);
delete[] buffer;
close(destinationFD);
return;
}
delete[] buffer;
close(destinationFD);
return;
}
ProcessInformation LaunchProcess(char *path, char **cmdArgs, char *baseDirectory)
{
ProcessInformation result = {};
chdir(baseDirectory);
pid_t processID;
posix_spawn(&processID, path, NULL, NULL, cmdArgs, environ);
result.processID = processID;
return result;
}
void SleepSeconds(int seconds)
{
sleep(seconds);
}
void KillProcess(ProcessInformation pInfo)
{
kill(pInfo.processID, SIGTERM);
sleep(1);
kill(pInfo.processID, SIGKILL);
}
bool HasProcessExited(ProcessInformation pInfo)
{
bool exited = false;
int status;
int returnProcessID = waitpid(pInfo.processID, &status, WNOHANG);
if(returnProcessID == pInfo.processID)
{
exited = true;
}
return exited;
}