/
misc_file.c
executable file
·202 lines (167 loc) · 3.17 KB
/
misc_file.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
/*
* misc.c -- handles:
* copyfile() movefile()
*
*/
#include "common.h"
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "stat.h"
#include "misc_file.h"
#include "main.h"
/* Copy a file from one place to another (possibly erasing old copy).
*
* returns: 0 if OK
* 1 if can't open original file
* 2 if can't open new file
* 3 if original file isn't normal
* 4 if ran out of disk space
*/
int copyfile(const char *oldpath, const char *newpath)
{
sock_t fi;
#ifndef CYGWIN_HACKS
fi = open(oldpath, O_RDONLY, 0);
#else
fi = open(oldpath, O_RDONLY | O_BINARY, 0);
#endif
if (fi < 0)
return 1;
struct stat st;
fstat(fi, &st);
if (!(st.st_mode & S_IFREG))
return 3;
sock_t fo;
fo = creat(newpath, (int) (st.st_mode & 0600));
if (fo < 0) {
close(fi);
return 2;
}
char buf[512] = "";
for (ssize_t x = 1; x > 0;) {
x = read(fi, buf, 512);
if (x > 0) {
if (write(fo, buf, x) < x) { /* Couldn't write */
close(fo);
close(fi);
unlink(newpath);
return 4;
}
}
}
#ifdef HAVE_FSYNC
fsync(fo);
#endif /* HAVE_FSYNC */
close(fo);
close(fi);
return 0;
}
int movefile(const char *oldpath, const char *newpath)
{
#ifdef HAVE_RENAME
/* Try to use rename first */
if (!rename(oldpath, newpath))
return 0;
#endif /* HAVE_RENAME */
/* If that fails, fall back to just copying and then
* deleting the file.
*/
int ret = copyfile(oldpath, newpath);
if (!ret)
unlink(oldpath);
return ret;
}
int is_file(const char *s)
{
struct stat ss;
int i = stat(s, &ss);
if (i < 0)
return 0;
if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
return 1;
return 0;
}
int can_stat(const char *s)
{
struct stat ss;
int i = stat(s, &ss);
if (i < 0)
return 0;
return 1;
}
int can_lstat(const char *s)
{
struct stat ss;
int i = lstat(s, &ss);
if (i < 0)
return 0;
return 1;
}
int is_symlink(const char *s)
{
struct stat ss;
int i = lstat(s, &ss);
if (i < 0)
return 0;
if (ss.st_mode & S_IFLNK)
return 1;
return 0;
}
int is_dir(const char *s)
{
struct stat ss;
int i = stat(s, &ss);
if (i < 0)
return 0;
if (ss.st_mode & S_IFDIR)
return 1;
return 0;
}
int fixmod(const char *s)
{
#ifndef CYGWIN_HACKS
if (!can_stat(s))
return 1;
return chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
#else
return 0;
#endif /* !CYGWIN_HACKS */
}
Tempfile::Tempfile()
{
len = strlen(tempdir) + 1 + 6 + 1;
file = new char[len];
sprintf(file, "%s.XXXXXX", tempdir);
MakeTemp();
}
Tempfile::Tempfile(const char *prefix)
{
len = strlen(tempdir) + 1 + strlen(prefix) + 1 + 6 + 1;
file = new char[len];
sprintf(file, "%s.%s-XXXXXX", tempdir, prefix);
MakeTemp();
}
void Tempfile::MakeTemp()
{
if ((fd = mkstemp(file)) < 0)
goto error;
if ((f = fdopen(fd, "w+b")) == NULL)
goto error;
fchmod(fd, S_IRUSR | S_IWUSR);
return;
error:
putlog(LOG_ERRORS, "Couldn't create temporary file '%s': %s", file, strerror(errno));
delete this;
fatal("Cannot create tempory file!", 0);
}
Tempfile::~Tempfile()
{
unlink(file);
if (f)
fclose(f);
else
close(fd);
delete[] file;
}