Switch to unified view

a/src/utils/copyfile.cpp b/src/utils/copyfile.cpp
...
...
33
33
34
#define CPBSIZ 8192
34
#define CPBSIZ 8192
35
35
36
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)
37
{
37
{
38
  int sfd = -1;
38
    int sfd = -1;
39
  int dfd = -1;
39
    int dfd = -1;
40
  bool ret = false;
40
    bool ret = false;
41
  char buf[CPBSIZ];
41
    char buf[CPBSIZ];
42
  int oflags = O_WRONLY|O_CREAT|O_TRUNC;
42
    int oflags = O_WRONLY|O_CREAT|O_TRUNC;
43
43
44
  LOGDEB(("copyfile: %s to %s\n", src, dst));
44
    LOGDEB(("copyfile: %s to %s\n", src, dst));
45
45
46
  if ((sfd = open(src, O_RDONLY)) < 0) {
46
    if ((sfd = ::open(src, O_RDONLY)) < 0) {
47
      reason += string("open ") + src + ": " + strerror(errno);
47
        reason += string("open ") + src + ": " + strerror(errno);
48
      goto out;
48
        goto out;
49
  }
49
    }
50
50
51
  if (flags & COPYFILE_EXCL) {
51
    if (flags & COPYFILE_EXCL) {
52
      oflags |= O_EXCL;
52
        oflags |= O_EXCL;
53
  }
53
    }
54
54
55
  if ((dfd = open(dst, oflags, 0644)) < 0) {
55
    if ((dfd = ::open(dst, oflags, 0644)) < 0) {
56
      reason += string("open/creat ") + dst + ": " + strerror(errno);
56
        reason += string("open/creat ") + dst + ": " + strerror(errno);
57
      // 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
58
      // the file, we might succeed...
58
        // want to unlink the file, we might succeed...
59
      flags |= COPYFILE_NOERRUNLINK;
59
        flags |= COPYFILE_NOERRUNLINK;
60
      goto out;
60
        goto out;
61
  }
61
    }
62
62
63
  for (;;) {
63
    for (;;) {
64
      int didread;
64
        int didread;
65
      didread = read(sfd, buf, CPBSIZ);
65
        didread = ::read(sfd, buf, CPBSIZ);
66
      if (didread < 0) {
66
        if (didread < 0) {
67
    reason += string("read src ") + src + ": " + strerror(errno);
67
            reason += string("read src ") + src + ": " + strerror(errno);
68
    goto out;
68
            goto out;
69
      }
69
        }
70
      if (didread == 0)
70
        if (didread == 0)
71
    break;
71
            break;
72
      if (write(dfd, buf, didread) != didread) {
72
        if (::write(dfd, buf, didread) != didread) {
73
    reason += string("write dst ") + src + ": " + strerror(errno);
73
            reason += string("write dst ") + src + ": " + strerror(errno);
74
    goto out;
74
            goto out;
75
      }
75
        }
76
  }
76
    }
77
77
78
  ret = true;
78
    ret = true;
79
 out:
79
out:
80
  if (ret == false && !(flags&COPYFILE_NOERRUNLINK))
80
    if (ret == false && !(flags&COPYFILE_NOERRUNLINK))
81
    unlink(dst);
81
        ::unlink(dst);
82
  if (sfd >= 0)
82
    if (sfd >= 0)
83
    close(sfd);
83
        ::close(sfd);
84
  if (dfd >= 0)
84
    if (dfd >= 0)
85
    close(dfd);
85
        ::close(dfd);
86
  return ret;
86
    return ret;
87
}
88
89
bool stringtofile(const string& dt, const char *dst, string& reason,
90
                  int flags)
91
{
92
    LOGDEB(("stringtofile:\n"));
93
    int dfd = -1;
94
    bool ret = false;
95
    int oflags = O_WRONLY|O_CREAT|O_TRUNC;
96
97
    LOGDEB(("stringtofile: %u bytes to %s\n", (unsigned int)dt.size(), dst));
98
99
    if (flags & COPYFILE_EXCL) {
100
        oflags |= O_EXCL;
101
    }
102
103
    if ((dfd = ::open(dst, oflags, 0644)) < 0) {
104
        reason += string("open/creat ") + dst + ": " + strerror(errno);
105
        // If we fail because of an open/truncate error, we do not
106
        // want to unlink the file, we might succeed...
107
        flags |= COPYFILE_NOERRUNLINK;
108
        goto out;
109
    }
110
111
    if (::write(dfd, dt.c_str(), size_t(dt.size())) != ssize_t(dt.size())) {
112
        reason += string("write dst ") + ": " + strerror(errno);
113
        goto out;
114
    }
115
116
    ret = true;
117
out:
118
    if (ret == false && !(flags&COPYFILE_NOERRUNLINK))
119
        ::unlink(dst);
120
    if (dfd >= 0)
121
        ::close(dfd);
122
    return ret;
87
}
123
}
88
124
89
bool renameormove(const char *src, const char *dst, string &reason)
125
bool renameormove(const char *src, const char *dst, string &reason)
90
{
126
{
91
    // First try rename(2). If this succeeds we're done. If this fails
127
    // First try rename(2). If this succeeds we're done. If this fails
...
...
115
151
116
    // Try to preserve modes, owner, times. This may fail for a number
152
    // Try to preserve modes, owner, times. This may fail for a number
117
    // of reasons
153
    // of reasons
118
    if ((st1.st_mode & 0777) != (st.st_mode & 0777)) {
154
    if ((st1.st_mode & 0777) != (st.st_mode & 0777)) {
119
        if (chmod(dst, st.st_mode&0777) != 0) {
155
        if (chmod(dst, st.st_mode&0777) != 0) {
120
      reason += string("Chmod ") + dst + "Error : " + strerror(errno);
156
            reason += string("Chmod ") + dst + "Error : " + strerror(errno);
121
  }
157
        }
122
    }
158
    }
123
    if (st.st_uid != st1.st_uid || st.st_gid != st1.st_gid) {
159
    if (st.st_uid != st1.st_uid || st.st_gid != st1.st_gid) {
124
        if (chown(dst, st.st_uid, st.st_gid) != 0) {
160
        if (chown(dst, st.st_uid, st.st_gid) != 0) {
125
      reason += string("Chown ") + dst + "Error : " + strerror(errno);
161
            reason += string("Chown ") + dst + "Error : " + strerror(errno);
126
  }
162
        }
127
    }
163
    }
128
    struct timeval times[2];
164
    struct timeval times[2];
129
    times[0].tv_sec = st.st_atime;
165
    times[0].tv_sec = st.st_atime;
130
    times[0].tv_usec = 0;
166
    times[0].tv_usec = 0;
131
    times[1].tv_sec = st.st_mtime;
167
    times[1].tv_sec = st.st_mtime;
...
...
159
#define OPT_m     0x2
195
#define OPT_m     0x2
160
#define OPT_e     0x4
196
#define OPT_e     0x4
161
197
162
static const char *thisprog;
198
static const char *thisprog;
163
static char usage [] =
199
static char usage [] =
164
"trcopyfile [-m] src dst\n"
200
    "trcopyfile [-m] src dst\n"
165
" -m : move instead of copying\n"
201
    " -m : move instead of copying\n"
166
" -e : fail if dest exists (only for copy)\n"
202
    " -e : fail if dest exists (only for copy)\n"
167
"\n"
203
    "\n"
168
;
204
    ;
169
static void
205
static void
170
Usage(void)
206
Usage(void)
171
{
207
{
172
    fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
208
    fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
173
    exit(1);
209
    exit(1);
...
...
183
        if (!(**argv))
219
        if (!(**argv))
184
            /* Cas du "adb - core" */
220
            /* Cas du "adb - core" */
185
            Usage();
221
            Usage();
186
        while (**argv)
222
        while (**argv)
187
            switch (*(*argv)++) {
223
            switch (*(*argv)++) {
188
            case 'm': op_flags |= OPT_m; break;
224
            case 'm':   op_flags |= OPT_m; break;
189
            case 'e': op_flags |= OPT_e; break;
225
            case 'e':   op_flags |= OPT_e; break;
190
            default: Usage(); break;
226
            default: Usage();   break;
191
            }
227
            }
192
        argc--; argv++;
228
        argc--; argv++;
193
    }
229
    }
194
230
195
    if (argc != 2)
231
    if (argc != 2)
...
...
209
    }
245
    }
210
    if (!ret) {
246
    if (!ret) {
211
        cerr << reason << endl;
247
        cerr << reason << endl;
212
        exit(1);
248
        exit(1);
213
    }  else {
249
    }  else {
214
  cout << "Succeeded" << endl;
250
        cout << "Succeeded" << endl;
215
  if (!reason.empty()) {
251
        if (!reason.empty()) {
216
      cout << "Warnings: " << reason << endl;
252
            cout << "Warnings: " << reason << endl;
217
  }
253
        }
218
  exit(0);
254
        exit(0);
219
    }
255
    }
220
}
256
}
221
257
222
#endif
258
#endif