Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 229 lines (191 sloc) 3.798 kb
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
1 /*
2 * misc.c -- handles:
3 * copyfile() movefile()
4 *
5 */
6
567cbb8 @bdrewery * Moved eggmain.h to common.h
bdrewery authored
7 #include "common.h"
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <fcntl.h>
53cf103 @bdrewery * fixmod() was simply returning bad results
bdrewery authored
11 #include <errno.h>
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
12 #include "stat.h"
aea88c2 @bdrewery * First class!!: Tempfile
bdrewery authored
13 #include "misc_file.h"
14 #include "main.h"
7e8ca0f @bdrewery svn: 1830
bdrewery authored
15 #include "shell.h"
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
16
17 /* Copy a file from one place to another (possibly erasing old copy).
18 *
19 * returns: 0 if OK
20 * 1 if can't open original file
21 * 2 if can't open new file
22 * 3 if original file isn't normal
23 * 4 if ran out of disk space
24 */
f58a2cd @bdrewery * Added binary.[c|h]
bdrewery authored
25 int copyfile(const char *oldpath, const char *newpath)
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
26 {
1413abc @bdrewery * Undid sockt_t/size_t patches
bdrewery authored
27 int fi;
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
28
29 #ifndef CYGWIN_HACKS
30 fi = open(oldpath, O_RDONLY, 0);
31 #else
32 fi = open(oldpath, O_RDONLY | O_BINARY, 0);
33 #endif
34 if (fi < 0)
35 return 1;
5fecc94 @bdrewery * Huge commit that introduces countless bugs I'm sure.
bdrewery authored
36
37 struct stat st;
38
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
39 fstat(fi, &st);
40 if (!(st.st_mode & S_IFREG))
41 return 3;
5fecc94 @bdrewery * Huge commit that introduces countless bugs I'm sure.
bdrewery authored
42
1413abc @bdrewery * Undid sockt_t/size_t patches
bdrewery authored
43 int fo;
5fecc94 @bdrewery * Huge commit that introduces countless bugs I'm sure.
bdrewery authored
44
3fc3499 @bdrewery * 1.0.9 Release
bdrewery authored
45 fo = creat(newpath, (int) (st.st_mode & 0600));
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
46 if (fo < 0) {
47 close(fi);
48 return 2;
49 }
5fecc94 @bdrewery * Huge commit that introduces countless bugs I'm sure.
bdrewery authored
50
51 char buf[512] = "";
52
1413abc @bdrewery * Undid sockt_t/size_t patches
bdrewery authored
53 for (int x = 1; x > 0;) {
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
54 x = read(fi, buf, 512);
55 if (x > 0) {
56 if (write(fo, buf, x) < x) { /* Couldn't write */
57 close(fo);
58 close(fi);
59 unlink(newpath);
60 return 4;
61 }
62 }
63 }
64 #ifdef HAVE_FSYNC
65 fsync(fo);
66 #endif /* HAVE_FSYNC */
67 close(fo);
68 close(fi);
69 return 0;
70 }
71
f58a2cd @bdrewery * Added binary.[c|h]
bdrewery authored
72 int movefile(const char *oldpath, const char *newpath)
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
73 {
74 #ifdef HAVE_RENAME
75 /* Try to use rename first */
76 if (!rename(oldpath, newpath))
77 return 0;
78 #endif /* HAVE_RENAME */
79
80 /* If that fails, fall back to just copying and then
81 * deleting the file.
82 */
5fecc94 @bdrewery * Huge commit that introduces countless bugs I'm sure.
bdrewery authored
83 int ret = copyfile(oldpath, newpath);
84
fcfd3bd * Eggdrop 1.6.12
Eggheads Development Team authored
85 if (!ret)
86 unlink(oldpath);
87 return ret;
88 }
71a8fd7 @bdrewery Added some file functions to simplify stat()
bdrewery authored
89
90 int is_file(const char *s)
91 {
92 struct stat ss;
93 int i = stat(s, &ss);
94
95 if (i < 0)
96 return 0;
97 if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
98 return 1;
99 return 0;
100 }
101
102 int can_stat(const char *s)
103 {
104 struct stat ss;
105 int i = stat(s, &ss);
106
107 if (i < 0)
108 return 0;
109 return 1;
110 }
111
112 int can_lstat(const char *s)
113 {
114 struct stat ss;
115 int i = lstat(s, &ss);
116
117 if (i < 0)
118 return 0;
119 return 1;
120 }
121
122 int is_symlink(const char *s)
123 {
124 struct stat ss;
d6e3992 @bdrewery * is_symlink needed lstat() not stat()
bdrewery authored
125 int i = lstat(s, &ss);
71a8fd7 @bdrewery Added some file functions to simplify stat()
bdrewery authored
126
127 if (i < 0)
128 return 0;
129 if (ss.st_mode & S_IFLNK)
130 return 1;
131 return 0;
132 }
133
134 int is_dir(const char *s)
135 {
136 struct stat ss;
137 int i = stat(s, &ss);
138
139 if (i < 0)
140 return 0;
141 if (ss.st_mode & S_IFDIR)
142 return 1;
143 return 0;
144 }
145
146 int fixmod(const char *s)
147 {
0267090 @bdrewery * Dont chmod() on CYGWIN
bdrewery authored
148 #ifndef CYGWIN_HACKS
71a8fd7 @bdrewery Added some file functions to simplify stat()
bdrewery authored
149 if (!can_stat(s))
53cf103 @bdrewery * fixmod() was simply returning bad results
bdrewery authored
150 return 1;
151 return chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
0267090 @bdrewery * Dont chmod() on CYGWIN
bdrewery authored
152 #else
153 return 0;
154 #endif /* !CYGWIN_HACKS */
71a8fd7 @bdrewery Added some file functions to simplify stat()
bdrewery authored
155 }
aea88c2 @bdrewery * First class!!: Tempfile
bdrewery authored
156
157 Tempfile::Tempfile()
158 {
5583f2d @bdrewery svn: 1325
bdrewery authored
159 len = strlen(tempdir) + 1 + 6 + 1;
160 file = new char[len];
655dc43 @bdrewery svn: 1335
bdrewery authored
161 sprintf(file, "%s.XXXXXX", tempdir);
aea88c2 @bdrewery * First class!!: Tempfile
bdrewery authored
162
163 MakeTemp();
164 }
165
166 Tempfile::Tempfile(const char *prefix)
167 {
5fecc94 @bdrewery * Huge commit that introduces countless bugs I'm sure.
bdrewery authored
168 len = strlen(tempdir) + 1 + strlen(prefix) + 1 + 6 + 1;
655dc43 @bdrewery svn: 1335
bdrewery authored
169 file = new char[len];
170 sprintf(file, "%s.%s-XXXXXX", tempdir, prefix);
aea88c2 @bdrewery * First class!!: Tempfile
bdrewery authored
171
172 MakeTemp();
173 }
174
175 void Tempfile::MakeTemp()
176 {
98e5da8 @bdrewery svn: 1818
bdrewery authored
177 if ((fd = mkstemp(file)) < 0) {
178 f = NULL;
5583f2d @bdrewery svn: 1325
bdrewery authored
179 goto error;
98e5da8 @bdrewery svn: 1818
bdrewery authored
180 }
e269997 @bdrewery svn: 1323
bdrewery authored
181
5583f2d @bdrewery svn: 1325
bdrewery authored
182 if ((f = fdopen(fd, "w+b")) == NULL)
183 goto error;
184
6fd70b7 @bdrewery * fchmod() to avoid a race condition
bdrewery authored
185 fchmod(fd, S_IRUSR | S_IWUSR);
5583f2d @bdrewery svn: 1325
bdrewery authored
186
187 return;
188
189 error:
46ac2c1 @bdrewery svn: 1819
bdrewery authored
190 putlog(LOG_ERRORS, "*", "Couldn't create temporary file '%s': %s", file, strerror(errno));
5583f2d @bdrewery svn: 1325
bdrewery authored
191 delete this;
192 fatal("Cannot create tempory file!", 0);
aea88c2 @bdrewery * First class!!: Tempfile
bdrewery authored
193 }
194
195 Tempfile::~Tempfile()
196 {
197 unlink(file);
5583f2d @bdrewery svn: 1325
bdrewery authored
198 if (f)
199 fclose(f);
200 else
201 close(fd);
aea88c2 @bdrewery * First class!!: Tempfile
bdrewery authored
202 delete[] file;
203 }
204
38337c4 @bdrewery * Moved check_tempdir() to misc_file.c
bdrewery authored
205 void check_tempdir()
206 {
207 if (!can_stat(tempdir)) {
208 if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
209 unlink(tempdir);
210 if (!can_stat(tempdir))
211 if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
212 werr(ERR_TMPSTAT);
213 }
214 }
215 if (fixmod(tempdir))
216 werr(ERR_TMPMOD);
217
218 /* test tempdir: it's vital */
6c7e288 @bdrewery svn: 1847
bdrewery authored
219 Tempfile *testdir = new Tempfile("test");
220 int result;
221
222 fprintf(testdir->f, "\n");
223 result = fflush(testdir->f);
224 delete testdir;
225 if (result)
226 fatal(strerror(errno), 0);
38337c4 @bdrewery * Moved check_tempdir() to misc_file.c
bdrewery authored
227 }
228
Something went wrong with that request. Please try again.