1 |
guppy |
1.4 |
/* |
2 |
fabian |
1.1 |
* misc.c -- handles: |
3 |
|
|
* copyfile() movefile() |
4 |
guppy |
1.4 |
* |
5 |
ite |
1.6 |
* $Id: misc_file.c,v 1.5 2001/04/12 02:44:22 guppy Exp $ |
6 |
fabian |
1.1 |
*/ |
7 |
guppy |
1.4 |
/* |
8 |
|
|
* Copyright (C) 1999, 2000, 2001 Eggheads Development Team |
9 |
|
|
* |
10 |
fabian |
1.1 |
* 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 |
guppy |
1.4 |
* |
15 |
fabian |
1.1 |
* 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 |
guppy |
1.4 |
* |
20 |
fabian |
1.1 |
* 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 |
fabian |
1.2 |
#ifndef CYGWIN_HACKS |
47 |
fabian |
1.1 |
fi = open(oldpath, O_RDONLY, 0); |
48 |
fabian |
1.2 |
#else |
49 |
|
|
fi = open(oldpath, O_RDONLY | O_BINARY, 0); |
50 |
|
|
#endif |
51 |
fabian |
1.1 |
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 |
guppy |
1.5 |
#ifdef HAVE_FSYNC |
73 |
|
|
fsync(fo); |
74 |
|
|
#endif /* HAVE_FSYNC */ |
75 |
fabian |
1.1 |
close(fo); |
76 |
|
|
close(fi); |
77 |
|
|
return 0; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
int movefile(char *oldpath, char *newpath) |
81 |
|
|
{ |
82 |
|
|
int ret; |
83 |
guppy |
1.4 |
|
84 |
fabian |
1.1 |
#ifdef HAVE_RENAME |
85 |
|
|
/* Try to use rename first */ |
86 |
guppy |
1.3 |
if (!rename(oldpath, newpath)) |
87 |
fabian |
1.1 |
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 |
guppy |
1.3 |
if (!ret) |
95 |
fabian |
1.1 |
unlink(oldpath); |
96 |
|
|
return ret; |
97 |
|
|
} |