summaryrefslogtreecommitdiff
path: root/init/builtins.cpp
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2017-03-24 02:26:13 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2017-03-24 02:26:14 +0000
commit7f0d15a6f23172657974ad94ba7bbb49cc5df79b (patch)
tree68afd81365a6a1e8ab904b7f899afb934c36b71f /init/builtins.cpp
parentc1ca1a8328892be6d3f07959ebc998f395dd2d08 (diff)
parent82bac0de6d95bcdf45729516f6a4f29eb2681118 (diff)
Merge "init: use read_file and write_file to implement do_copy builtin"
Diffstat (limited to 'init/builtins.cpp')
-rw-r--r--init/builtins.cpp60
1 files changed, 5 insertions, 55 deletions
diff --git a/init/builtins.cpp b/init/builtins.cpp
index 24875d5fa..32e9ef642 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -704,61 +704,11 @@ static int do_write(const std::vector<std::string>& args) {
}
static int do_copy(const std::vector<std::string>& args) {
- 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;
+ std::string data;
+ if (read_file(args[1].c_str(), &data)) {
+ return write_file(args[2].c_str(), data.data()) ? 0 : 1;
+ }
+ return 1;
}
static int do_chown(const std::vector<std::string>& args) {