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