diff options
author | Wonsik Kim <wonsik@google.com> | 2017-03-31 00:21:59 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-03-31 00:22:00 +0000 |
commit | 3428d0c2c0b0056dadbc561d86b66ec83b960583 (patch) | |
tree | a0652a7d7b433e32bcb480dc74d1f915e312fecd /init/builtins.cpp | |
parent | c1314c8b13ef718b24372b6749725bdf7e76d46d (diff) | |
parent | 395e29472fa012c4177d981d9ce699625b706f4f (diff) |
Merge "Revert "init: use read_file and write_file to implement do_copy builtin""
Diffstat (limited to 'init/builtins.cpp')
-rw-r--r-- | init/builtins.cpp | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/init/builtins.cpp b/init/builtins.cpp index 02e314f64..d8569d7b9 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -702,11 +702,61 @@ static int do_write(const std::vector<std::string>& args) { } static int do_copy(const std::vector<std::string>& args) { - std::string data; - if (read_file(args[1].c_str(), &data)) { - return write_file(args[2].c_str(), data.data()) ? 0 : 1; - } - return 1; + char *buffer = NULL; + int rc = 0; + int fd1 = -1, fd2 = -1; + struct stat info; + int brtw, brtr; + char *p; + + if (stat(args[1].c_str(), &info) < 0) + return -1; + + if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0) + goto out_err; + + if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0) + goto out_err; + + if (!(buffer = (char*) malloc(info.st_size))) + goto out_err; + + p = buffer; + brtr = info.st_size; + while(brtr) { + rc = read(fd1, p, brtr); + if (rc < 0) + goto out_err; + if (rc == 0) + break; + p += rc; + brtr -= rc; + } + + p = buffer; + brtw = info.st_size; + while(brtw) { + rc = write(fd2, p, brtw); + if (rc < 0) + goto out_err; + if (rc == 0) + break; + p += rc; + brtw -= rc; + } + + rc = 0; + goto out; +out_err: + rc = -1; +out: + if (buffer) + free(buffer); + if (fd1 >= 0) + close(fd1); + if (fd2 >= 0) + close(fd2); + return rc; } static int do_chown(const std::vector<std::string>& args) { |