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 | |
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')
-rw-r--r-- | base/SConscript | 2 | ||||
-rw-r--r-- | base/file_util_linux.cc | 49 | ||||
-rw-r--r-- | base/file_util_posix.cc | 47 |
3 files changed, 78 insertions, 20 deletions
diff --git a/base/SConscript b/base/SConscript index efbaecb..4684674 100644 --- a/base/SConscript +++ b/base/SConscript @@ -231,6 +231,7 @@ test_files = [ 'atomicops_unittest.cc', 'command_line_unittest.cc', 'condition_variable_unittest.cc', + 'file_util_unittest.cc', 'histogram_unittest.cc', 'json_reader_unittest.cc', 'json_writer_unittest.cc', @@ -276,7 +277,6 @@ if env['PLATFORM'] == 'win32': test_files.extend([ 'clipboard_unittest.cc', - 'file_util_unittest.cc', 'hmac_unittest.cc', 'idletimer_unittest.cc', 'process_util_unittest.cc', 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 diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index f6beb57..8225dca 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -20,6 +20,8 @@ namespace file_util { +static const wchar_t* kTempFileName = L"com.google.chrome.XXXXXX"; + std::wstring GetDirectoryFromPath(const std::wstring& path) { if (EndsWithSeparator(path)) { std::wstring dir = path; @@ -45,7 +47,8 @@ bool AbsolutePath(std::wstring* path) { // that functionality. If not, remove from file_util_win.cc, otherwise add it // here. bool Delete(const std::wstring& path, bool recursive) { - const char* utf8_path = WideToUTF8(path).c_str(); + std::string utf8_path_string = WideToUTF8(path); + const char* utf8_path = utf8_path_string.c_str(); struct stat64 file_info; int test = stat64(utf8_path, &file_info); if (test != 0) { @@ -150,14 +153,15 @@ bool CreateTemporaryFileName(std::wstring* temp_file) { std::wstring tmpdir; if (!GetTempDir(&tmpdir)) return false; - tmpdir.append(L"com.google.chrome.XXXXXX"); - // this should be OK since mktemp just replaces characters in place - char* buffer = const_cast<char*>(WideToUTF8(tmpdir).c_str()); - *temp_file = UTF8ToWide(mktemp(buffer)); - int fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666); + AppendToPath(&tmpdir, kTempFileName); + std::string tmpdir_string = WideToUTF8(tmpdir); + // this should be OK since mkstemp just replaces characters in place + char* buffer = const_cast<char*>(tmpdir_string.c_str()); + int fd = mkstemp(buffer); if (fd < 0) return false; - close(fd); + *temp_file = UTF8ToWide(buffer); + close(fd); return true; } @@ -166,9 +170,10 @@ bool CreateNewTempDirectory(const std::wstring& prefix, std::wstring tmpdir; if (!GetTempDir(&tmpdir)) return false; - tmpdir.append(L"/com.google.chrome.XXXXXX"); + AppendToPath(&tmpdir, kTempFileName); + std::string tmpdir_string = WideToUTF8(tmpdir); // this should be OK since mkdtemp just replaces characters in place - char* buffer = const_cast<char*>(WideToUTF8(tmpdir).c_str()); + char* buffer = const_cast<char*>(tmpdir_string.c_str()); char* dtemp = mkdtemp(buffer); if (!dtemp) return false; @@ -213,14 +218,25 @@ int ReadFile(const std::wstring& filename, char* data, int size) { } int WriteFile(const std::wstring& filename, const char* data, int size) { - int fd = open(WideToUTF8(filename).c_str(), O_WRONLY | O_CREAT | O_TRUNC, - 0666); + int fd = creat(WideToUTF8(filename).c_str(), 0666); if (fd < 0) return -1; - - int ret_value = write(fd, data, size); + + // Allow for partial writes + ssize_t bytes_written_total = 0; + do { + ssize_t bytes_written_partial = write(fd, + data + bytes_written_total, + size - bytes_written_total); + if (bytes_written_partial < 0) { + close(fd); + return -1; + } + bytes_written_total += bytes_written_partial; + } while (bytes_written_total < size); + close(fd); - return ret_value; + return bytes_written_total; } // Gets the current working directory for the process. @@ -308,8 +324,7 @@ std::wstring FileEnumerator::Next() { // Patterns are only matched on the items in the top-most directory. // (see Windows implementation) if (fts_ent->fts_level == 1 && pattern_.length() > 0) { - const char* utf8_pattern = WideToUTF8(pattern_).c_str(); - if (fnmatch(utf8_pattern, fts_ent->fts_path, 0) != 0) { + if (fnmatch(WideToUTF8(pattern_).c_str(), fts_ent->fts_path, 0) != 0) { if (fts_ent->fts_info == FTS_D) fts_set(fts_, fts_ent, FTS_SKIP); return Next(); |