diff options
author | Yongqin Liu <yongqin.liu@linaro.org> | 2016-12-28 16:06:19 +0800 |
---|---|---|
committer | Tom Cherry <tomcherry@google.com> | 2017-04-04 06:21:29 +0000 |
commit | dbe88e7953ed53961056c7f5531d91d229293462 (patch) | |
tree | 8fb5586888fdaabceee56c689c53edba71bc8545 /init/builtins.cpp | |
parent | 53089aa25ca9707e22e45e862f794bfc958d302a (diff) |
init: use read_file and write_file to implement do_copy builtin
this will make the implementation more cleaner,
and has error message output when failed on some operations
also add the O_TRUNC flag explicitly for the open function
called in write_file.
And add more test on read_file and write_file functions
Bug: 36726045
Test: manual with hikey
Test: boot and init tests on bullhead
Test: cast with fugu, per b/36726045
Merged-In: If3c30a2fff58cfece2fcd27e69c30382146e6808
Change-Id: If3c30a2fff58cfece2fcd27e69c30382146e6808
Signed-off-by: Yongqin Liu <yongqin.liu@linaro.org>
Diffstat (limited to 'init/builtins.cpp')
-rw-r--r-- | init/builtins.cpp | 60 |
1 files changed, 5 insertions, 55 deletions
diff --git a/init/builtins.cpp b/init/builtins.cpp index 4ad36e078..64c00e954 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -700,61 +700,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], &data)) { + return write_file(args[2], data) ? 0 : 1; + } + return 1; } static int do_chown(const std::vector<std::string>& args) { |