| 24 | } while (0) |
| 25 | |
| 26 | static bool copy_file(acl::ifstream& in, const acl::string& to_path, |
| 27 | const acl::string& to_filepath, int* ncopied) |
| 28 | { |
| 29 | if (in.fseek(0, SEEK_SET) < 0) |
| 30 | { |
| 31 | logger_error("fseek from file: %s error: %s", |
| 32 | in.file_path(), acl::last_serror()); |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | if (access(to_path.c_str(), 0) != 0) |
| 37 | { |
| 38 | if (acl_make_dirs(to_path.c_str(), 0755) == -1) |
| 39 | { |
| 40 | logger_error("create dirs: %s error: %s", |
| 41 | to_path.c_str(), acl::last_serror()); |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | acl_int64 length = in.fsize(); |
| 47 | if (length < 0) |
| 48 | { |
| 49 | logger_error("get file(%s)'s size error: %s", in.file_path(), |
| 50 | acl::last_serror()); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | acl::ofstream out; |
| 55 | |
| 56 | if (out.open_trunc(to_filepath.c_str()) == false) |
| 57 | { |
| 58 | logger_error("ope_trunc file: %s error: %s", |
| 59 | to_filepath.c_str(), acl::last_serror()); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | logger("copying from file: %s, to file: %s", in.file_path(), |
| 64 | to_filepath.c_str()); |
| 65 | |
| 66 | char buf[4096]; |
| 67 | int ret; |
| 68 | acl_int64 nread = 0; |
| 69 | |
| 70 | while (true) |
| 71 | { |
| 72 | ret = in.read(buf, sizeof(buf), false); |
| 73 | if (ret == -1) |
| 74 | { |
| 75 | if (nread == length) |
| 76 | break; |
| 77 | |
| 78 | logger_error("read from file: %s error: %s, " |
| 79 | "to file: %s, nread: %lld, length: %lld", |
| 80 | in.file_path(), acl::last_serror(), |
| 81 | to_filepath.c_str(), nread, length); |
| 82 | return false; |
| 83 | } |
no test coverage detected
searching dependent graphs…