MCPcopy Create free account
hub / github.com/Tencent/phxqueue / CopyFile2

Function CopyFile2

phxqueue/comm/utils/file_util.cpp:347–397  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

345// deprecated functions
346
347int CopyFile2(const string &src_file_path, const string &dst_file_path) {
348 int src_file{open(src_file_path.c_str(), O_RDONLY, 0)};
349 int dst_file{open(dst_file_path.c_str(), O_WRONLY | O_CREAT, 0644)};
350 int ret{0};
351
352 if (src_file < 0 || dst_file < 0) {
353 ret = errno;
354 return ret;
355 }
356
357 // struct required, rationale: function stat() exists also
358 struct stat src_stat;
359 if (-1 == fstat(src_file, &src_stat)) {
360 ret = errno;
361 // TODO:
362 //QLErr("fstat \"%s\" err %d", src_file_path.c_str(), ret);
363
364 close(src_file);
365 close(dst_file);
366
367 return ret;
368 }
369
370 // TODO(walnuthe): may write fewer bytes than requested
371 // should be prepared to retry the call if there were unsent bytes
372 // <http://man7.org/linux/man-pages/man2/sendfile.2.html>
373 if (-1 == sendfile(dst_file, src_file, 0, src_stat.st_size)) {
374 ret = errno;
375 // TODO:
376 //QLErr("sendfile \"%s\" -> \"%s\" err %d",
377 // src_file_path.c_str(), dst_file_path.c_str(), ret);
378
379 close(src_file);
380 close(dst_file);
381
382 return ret;
383 }
384
385 if (-1 == close(dst_file)) {
386 ret = errno;
387 // TODO:
388 //QLErr("close \"%s\" err %d", dst_file_path.c_str(), ret);
389 }
390 if (-1 == close(src_file)) {
391 ret = errno;
392 // TODO:
393 //QLErr("close \"%s\" err %d", src_file_path.c_str(), ret);
394 }
395
396 return ret;
397}
398
399int RemoveDir2(const string &dir_path, const bool recursive) {
400 vector<string> file_paths;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected