|
a/src/utils/copyfile.cpp |
|
b/src/utils/copyfile.cpp |
|
... |
|
... |
27 |
#include <cstring>
|
27 |
#include <cstring>
|
28 |
|
28 |
|
29 |
#include "copyfile.h"
|
29 |
#include "copyfile.h"
|
30 |
#include "debuglog.h"
|
30 |
#include "debuglog.h"
|
31 |
|
31 |
|
|
|
32 |
using namespace std;
|
|
|
33 |
|
32 |
#define CPBSIZ 8192
|
34 |
#define CPBSIZ 8192
|
33 |
#define COPYFILE_NOERRUNLINK 1
|
|
|
34 |
|
35 |
|
35 |
bool copyfile(const char *src, const char *dst, string &reason, int flags)
|
36 |
bool copyfile(const char *src, const char *dst, string &reason, int flags)
|
36 |
{
|
37 |
{
|
37 |
int sfd = -1;
|
38 |
int sfd = -1;
|
38 |
int dfd = -1;
|
39 |
int dfd = -1;
|
39 |
bool ret = false;
|
40 |
bool ret = false;
|
40 |
char buf[CPBSIZ];
|
41 |
char buf[CPBSIZ];
|
|
|
42 |
int oflags = O_WRONLY|O_CREAT|O_TRUNC;
|
41 |
|
43 |
|
42 |
LOGDEB(("copyfile: %s to %s\n", src, dst));
|
44 |
LOGDEB(("copyfile: %s to %s\n", src, dst));
|
43 |
|
45 |
|
44 |
if ((sfd = open(src, O_RDONLY)) < 0) {
|
46 |
if ((sfd = open(src, O_RDONLY)) < 0) {
|
45 |
reason += string("open ") + src + ": " + strerror(errno);
|
47 |
reason += string("open ") + src + ": " + strerror(errno);
|
46 |
goto out;
|
48 |
goto out;
|
47 |
}
|
49 |
}
|
48 |
|
50 |
|
49 |
if ((dfd = open(dst, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
|
51 |
if (flags & COPYFILE_EXCL) {
|
|
|
52 |
oflags |= O_EXCL;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
if ((dfd = open(dst, oflags, 0644)) < 0) {
|
50 |
reason += string("open/creat ") + dst + ": " + strerror(errno);
|
56 |
reason += string("open/creat ") + dst + ": " + strerror(errno);
|
51 |
// If we fail because of an open/truncate error, we do not want to unlink
|
57 |
// If we fail because of an open/truncate error, we do not want to unlink
|
52 |
// the file, we might succeed...
|
58 |
// the file, we might succeed...
|
53 |
flags |= COPYFILE_NOERRUNLINK;
|
59 |
flags |= COPYFILE_NOERRUNLINK;
|
54 |
goto out;
|
60 |
goto out;
|
|
... |
|
... |
141 |
#include <stdlib.h>
|
147 |
#include <stdlib.h>
|
142 |
#include <unistd.h>
|
148 |
#include <unistd.h>
|
143 |
|
149 |
|
144 |
#include <string>
|
150 |
#include <string>
|
145 |
#include <iostream>
|
151 |
#include <iostream>
|
|
|
152 |
|
|
|
153 |
#include "copyfile.h"
|
|
|
154 |
|
146 |
using namespace std;
|
155 |
using namespace std;
|
147 |
|
|
|
148 |
#include "copyfile.h"
|
|
|
149 |
|
156 |
|
150 |
static int op_flags;
|
157 |
static int op_flags;
|
151 |
#define OPT_MOINS 0x1
|
158 |
#define OPT_MOINS 0x1
|
152 |
#define OPT_m 0x2
|
159 |
#define OPT_m 0x2
|
|
|
160 |
#define OPT_e 0x4
|
153 |
|
161 |
|
154 |
static const char *thisprog;
|
162 |
static const char *thisprog;
|
155 |
static char usage [] =
|
163 |
static char usage [] =
|
156 |
"trcopyfile [-m] src dst\n"
|
164 |
"trcopyfile [-m] src dst\n"
|
157 |
" -m : move instead of copying\n"
|
165 |
" -m : move instead of copying\n"
|
|
|
166 |
" -e : fail if dest exists (only for copy)\n"
|
158 |
"\n"
|
167 |
"\n"
|
159 |
;
|
168 |
;
|
160 |
static void
|
169 |
static void
|
161 |
Usage(void)
|
170 |
Usage(void)
|
162 |
{
|
171 |
{
|
|
... |
|
... |
175 |
/* Cas du "adb - core" */
|
184 |
/* Cas du "adb - core" */
|
176 |
Usage();
|
185 |
Usage();
|
177 |
while (**argv)
|
186 |
while (**argv)
|
178 |
switch (*(*argv)++) {
|
187 |
switch (*(*argv)++) {
|
179 |
case 'm': op_flags |= OPT_m; break;
|
188 |
case 'm': op_flags |= OPT_m; break;
|
|
|
189 |
case 'e': op_flags |= OPT_e; break;
|
180 |
default: Usage(); break;
|
190 |
default: Usage(); break;
|
181 |
}
|
191 |
}
|
182 |
argc--; argv++;
|
192 |
argc--; argv++;
|
183 |
}
|
193 |
}
|
184 |
|
194 |
|
|
... |
|
... |
189 |
bool ret;
|
199 |
bool ret;
|
190 |
string reason;
|
200 |
string reason;
|
191 |
if (op_flags & OPT_m) {
|
201 |
if (op_flags & OPT_m) {
|
192 |
ret = renameormove(src.c_str(), dst.c_str(), reason);
|
202 |
ret = renameormove(src.c_str(), dst.c_str(), reason);
|
193 |
} else {
|
203 |
} else {
|
|
|
204 |
int flags = 0;
|
|
|
205 |
if (op_flags & OPT_e) {
|
|
|
206 |
flags |= COPYFILE_EXCL;
|
|
|
207 |
}
|
194 |
ret = copyfile(src.c_str(), dst.c_str(), reason);
|
208 |
ret = copyfile(src.c_str(), dst.c_str(), reason, flags);
|
195 |
}
|
209 |
}
|
196 |
if (!ret) {
|
210 |
if (!ret) {
|
197 |
cerr << reason << endl;
|
211 |
cerr << reason << endl;
|
198 |
exit(1);
|
212 |
exit(1);
|
199 |
} else {
|
213 |
} else {
|