/[cvs]/eggdrop1.9/src/misc_file.c
ViewVC logotype

Contents of /eggdrop1.9/src/misc_file.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Wed Jan 16 22:40:57 2002 UTC (17 years, 9 months ago) by ite
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +1 -1 lines
File MIME type: text/x-chdr
FILE REMOVED
Removed sources obsoleted by previous patches

1 /*
2 * misc.c -- handles:
3 * copyfile() movefile()
4 *
5 * $Id: misc_file.c,v 1.5 2001/04/12 02:44:22 guppy Exp $
6 */
7 /*
8 * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #include "main.h"
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include "stat.h"
30
31
32 /* Copy a file from one place to another (possibly erasing old copy).
33 *
34 * returns: 0 if OK
35 * 1 if can't open original file
36 * 2 if can't open new file
37 * 3 if original file isn't normal
38 * 4 if ran out of disk space
39 */
40 int copyfile(char *oldpath, char *newpath)
41 {
42 int fi, fo, x;
43 char buf[512];
44 struct stat st;
45
46 #ifndef CYGWIN_HACKS
47 fi = open(oldpath, O_RDONLY, 0);
48 #else
49 fi = open(oldpath, O_RDONLY | O_BINARY, 0);
50 #endif
51 if (fi < 0)
52 return 1;
53 fstat(fi, &st);
54 if (!(st.st_mode & S_IFREG))
55 return 3;
56 fo = creat(newpath, (int) (st.st_mode & 0777));
57 if (fo < 0) {
58 close(fi);
59 return 2;
60 }
61 for (x = 1; x > 0;) {
62 x = read(fi, buf, 512);
63 if (x > 0) {
64 if (write(fo, buf, x) < x) { /* Couldn't write */
65 close(fo);
66 close(fi);
67 unlink(newpath);
68 return 4;
69 }
70 }
71 }
72 #ifdef HAVE_FSYNC
73 fsync(fo);
74 #endif /* HAVE_FSYNC */
75 close(fo);
76 close(fi);
77 return 0;
78 }
79
80 int movefile(char *oldpath, char *newpath)
81 {
82 int ret;
83
84 #ifdef HAVE_RENAME
85 /* Try to use rename first */
86 if (!rename(oldpath, newpath))
87 return 0;
88 #endif /* HAVE_RENAME */
89
90 /* If that fails, fall back to just copying and then
91 * deleting the file.
92 */
93 ret = copyfile(oldpath, newpath);
94 if (!ret)
95 unlink(oldpath);
96 return ret;
97 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23