fileutils.c
4.66 KB
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
/*
clsync - file tree sync utility based on inotify
Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "port-hacks.h"
#include "error.h"
#include "malloc.h"
char *fd2fpath_malloc(int fd) {
#if __linux__
stat64_t st64;
if(fd <= 0) {
error("Invalid file descriptor supplied: fd2fpath_malloc(%i).", fd);
errno = EINVAL;
return NULL;
}
char *fpath = xmalloc((1<<8) + 2);
sprintf(fpath, "/proc/self/fd/%i", fd);
if(lstat64(fpath, &st64)) {
error("Cannot lstat64(\"%s\", st64).", fpath);
return NULL;
}
ssize_t fpathlen = st64.st_size;
if(fpathlen > (1<<8))
fpath = xrealloc(fpath, fpathlen+2);
debug(3, "Getting file path from symlink \"%s\". Path length is: %i.", fpath, fpathlen);
if((fpathlen = readlink(fpath, fpath, fpathlen+1)) < 0) {
error("Cannot readlink(\"%s\", fpath, bufsize).", fpath);
return NULL;
}
debug(3, "The path is: \"%s\"", fpath);
fpath[fpathlen] = 0;
return fpath;
#else
critical("Function fd2fpath_malloc() is not supported in this OS");
return NULL;
#endif
}
/**
* @brief Copies file
*
* @param[in] path_from Source file path
* @param[in] path_to Destination file path
*
* @retval zero Successfully copied
* @retval non-zero Got error, while copying
*
*/
int fileutils_copy(const char *path_from, const char *path_to) {
char buf[BUFSIZ];
FILE *from, *to;
from = fopen(path_from, "r");
if(from == NULL) {
error("fileutils_copy(\"%s\", \"%s\"): Cannot open file \"%s\" for reading",
path_from, path_to, path_from);
return errno;
}
to = fopen(path_to, "w");
if(to == NULL) {
error("fileutils_copy(\"%s\", \"%s\"): Cannot open file \"%s\" for writing",
path_from, path_to, path_to);
return errno;
}
while(!feof(from)) {
int err;
size_t r, w;
r = fread(buf, 1, BUFSIZ, from);
if((err=ferror(from))) {
error("fileutils_copy(\"%s\", \"%s\"): Cannot read from file \"%s\"",
path_from, path_to, path_from);
return errno; // CHECK: Is the "errno" should be used in fread() case?
}
w = fwrite(buf, 1, r, to);
if((err=ferror(to))) {
error("fileutils_copy(\"%s\", \"%s\"): Cannot write to file \"%s\"",
path_from, path_to, path_to);
return errno; // CHECK: is the "errno" should be used in fwrite() case?
}
if(r != w) {
error("fileutils_copy(\"%s\", \"%s\"): Got error while writing to file \"%s\" (%u != %u)",
path_from, path_to, path_to, r, w);
return errno; // CHECK: is the "errno" should be used in case "r != w"?
}
}
return 0;
}
/**
* @brief Calculates directory level of a canonized path (actually it just counts "/"-s)
*
* @param[in] path Canonized path (with realpath())
*
* @retval zero or prositive Direcory level
* @retval negative Got error, while calculation. Error-code is placed to errno.
*
*/
short int fileutils_calcdirlevel(const char *path) {
short int dirlevel = 0;
const char *ptr = path;
if(path == NULL) {
error("path is NULL.");
errno=EINVAL;
return -1;
}
if(*path == 0) {
error("path has zero length.");
errno=EINVAL;
return -2;
}
if(*path != '/') {
error("path \"%s\" is not canonized.", path);
errno=EINVAL;
return -3;
}
while(*(ptr++))
if(*ptr == '/')
dirlevel++;
return dirlevel;
}
/**
* @brief Combination of mkdirat() and openat()
*
* @param[in] dir_path Path to directory to create and open
@ @param[in] dirfd_parent File descriptor of directory for relative paths
@ @param[in] dir_mode Modes for newly created directory (e.g. 750)
*
* @retval dirfd File descriptor to newly created directory
* @retval NULL On error
*
*/
int mkdirat_open(const char *const dir_path, int dirfd_parent, mode_t dir_mode) {
int dirfd;
debug(5, "mkdirat(%u, \"%s\", %o)", dirfd_parent, dir_path, dir_mode);
if (mkdirat(dirfd_parent, dir_path, dir_mode))
return -1;
debug(5, "openat(%u, \"%s\", %x)", dirfd_parent, dir_path, O_RDWR|O_DIRECTORY|O_PATH);
dirfd = openat(dirfd_parent, dir_path, O_RDWR|O_DIRECTORY|O_PATH);
if (dirfd == -1)
return -1;
return dirfd;
}