diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-11 17:36:23 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-11 17:36:23 +0000 |
commit | 778e8c51cad3649127edf90631e785e7c2c6c3f7 (patch) | |
tree | 0dc085fd2a444f24822179cf5759d472662833b1 /base/file_util_linux.cc | |
parent | 37312d701f670b52f796b217adc8726a2a0fa52a (diff) | |
download | chromium_src-778e8c51cad3649127edf90631e785e7c2c6c3f7.zip chromium_src-778e8c51cad3649127edf90631e785e7c2c6c3f7.tar.gz chromium_src-778e8c51cad3649127edf90631e785e7c2c6c3f7.tar.bz2 |
POSIX/Linux related changes to file_util:
- Replaced mktemp with mkstemp
- Implemented file_util::CopyFile for Linux, run FileUtilTest on Linux
- Cleaned up some invalid uses of c_str() on a temporary std::string
- Changed file_util::WriteFile to allow for partial writes
Patch by Matthias Reitinger <reimarvin@gmail.com>
http://codereview.chromium.org/1869
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2070 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_linux.cc')
-rw-r--r-- | base/file_util_linux.cc | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/base/file_util_linux.cc b/base/file_util_linux.cc index bfcc816..8c1a780 100644 --- a/base/file_util_linux.cc +++ b/base/file_util_linux.cc @@ -4,7 +4,10 @@ #include "base/file_util.h" +#include <fcntl.h> + #include <string> +#include <vector> #include "base/logging.h" #include "base/string_util.h" @@ -23,9 +26,49 @@ bool GetTempDir(std::wstring* path) { } bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { - // TODO(erikkay): implement - NOTIMPLEMENTED(); - return false; + int infile = open(WideToUTF8(from_path).c_str(), O_RDONLY); + if (infile < 0) + return false; + + int outfile = creat(WideToUTF8(to_path).c_str(), 0666); + if (outfile < 0) { + close(infile); + return false; + } + + const size_t kBufferSize = 32768; + std::vector<char> buffer(kBufferSize); + bool result = true; + + while (result) { + ssize_t bytes_read = read(infile, &buffer[0], buffer.size()); + if (bytes_read < 0) { + result = false; + break; + } + if (bytes_read == 0) + break; + // Allow for partial writes + ssize_t bytes_written_per_read = 0; + do { + ssize_t bytes_written_partial = write( + outfile, + &buffer[bytes_written_per_read], + bytes_read - bytes_written_per_read); + if (bytes_written_partial < 0) { + result = false; + break; + } + bytes_written_per_read += bytes_written_partial; + } while (bytes_written_per_read < bytes_read); + } + + if (close(infile) < 0) + result = false; + if (close(outfile) < 0) + result = false; + + return result; } } // namespace file_util |