-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_util.c
81 lines (68 loc) · 1.88 KB
/
file_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
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include "file_util.h"
/* Function with behaviour like `mkdir -p'
* copied from http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/
*/
int mkpath(const char *s, mode_t mode) {
char *q, *r = NULL, *path = NULL, *up = NULL;
int rv;
rv = -1;
if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
return (0);
if ((path = strdup(s)) == NULL)
exit(1);
if ((q = strdup(s)) == NULL)
exit(1);
if ((r = dirname(q)) == NULL)
goto out;
if ((up = strdup(r)) == NULL)
exit(1);
if ((mkpath(up, mode) == -1) && (errno != EEXIST))
goto out;
if ((mkdir(path, mode) == -1) && (errno != EEXIST))
rv = -1;
else
rv = 0;
out:
if (up != NULL)
free(up);
free(q);
free(path);
return (rv);
}
/*
* Moves a file, similar to mv. Tries to rename it and failing that copies it then
* removes the original.
*/
void move(char *source, char *dest) {
if(rename(source,dest) != 0) {
copy(source,dest);
remove(source);
}
}
/*
* Copies the file source to location dest
* returns: nothing
*/
void copy(char* source, char* dest) {
struct stat stat_buf;
off_t offset = 0;
int src = open(source, O_RDONLY);
fstat(src, &stat_buf);
int dst = open(dest,O_WRONLY | O_CREAT, stat_buf.st_mode);
if (sendfile(dst, src, &offset, stat_buf.st_size) == -1) {
printf("Error occured while copying %s to %s. Error: %s\n",
source, dest, strerror(errno));
}
close(src);
close(dst);
}