Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | |
| 8 | |
| 7 #include <string> | 9 #include <string> |
| 10 #include <vector> | |
| 8 | 11 |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 11 | 14 |
| 12 namespace file_util { | 15 namespace file_util { |
| 13 | 16 |
| 14 const wchar_t kPathSeparator = L'/'; | 17 const wchar_t kPathSeparator = L'/'; |
| 15 | 18 |
| 16 bool GetTempDir(std::wstring* path) { | 19 bool GetTempDir(std::wstring* path) { |
| 17 const char* tmp = getenv("TMPDIR"); | 20 const char* tmp = getenv("TMPDIR"); |
| 18 if (tmp) | 21 if (tmp) |
| 19 *path = UTF8ToWide(tmp); | 22 *path = UTF8ToWide(tmp); |
| 20 else | 23 else |
| 21 *path = L"/tmp"; | 24 *path = L"/tmp"; |
| 22 return true; | 25 return true; |
| 23 } | 26 } |
| 24 | 27 |
| 25 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { | 28 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { |
| 26 // TODO(erikkay): implement | 29 int infile = open(WideToUTF8(from_path).c_str(), O_RDONLY); |
| 27 NOTIMPLEMENTED(); | 30 if (infile < 0) |
| 28 return false; | 31 return false; |
| 32 | |
| 33 int outfile = creat(WideToUTF8(to_path).c_str(), 0666); | |
|
Dean McNamee
2008/09/12 13:59:46
Is this the right thing to do? I would think we s
Erik does not do reviews
2008/09/12 16:00:50
My understanding is that both creat() and open() u
| |
| 34 if (outfile < 0) { | |
| 35 close(infile); | |
| 36 return false; | |
| 37 } | |
| 38 | |
| 39 const size_t kBufferSize = 32768; | |
|
Dean McNamee
2008/09/12 13:59:46
If this is 32k, it would be nice as 32 * 1024;
| |
| 40 std::vector<char> buffer(kBufferSize); | |
| 41 bool result = true; | |
| 42 | |
| 43 while (result) { | |
| 44 ssize_t bytes_read = read(infile, &buffer[0], buffer.size()); | |
| 45 if (bytes_read < 0) { | |
| 46 result = false; | |
| 47 break; | |
| 48 } | |
| 49 if (bytes_read == 0) | |
| 50 break; | |
| 51 // Allow for partial writes | |
| 52 ssize_t bytes_written_per_read = 0; | |
| 53 do { | |
| 54 ssize_t bytes_written_partial = write( | |
| 55 outfile, | |
| 56 &buffer[bytes_written_per_read], | |
| 57 bytes_read - bytes_written_per_read); | |
| 58 if (bytes_written_partial < 0) { | |
| 59 result = false; | |
| 60 break; | |
| 61 } | |
| 62 bytes_written_per_read += bytes_written_partial; | |
| 63 } while (bytes_written_per_read < bytes_read); | |
| 64 } | |
| 65 | |
| 66 if (close(infile) < 0) | |
| 67 result = false; | |
| 68 if (close(outfile) < 0) | |
| 69 result = false; | |
| 70 | |
| 71 return result; | |
| 29 } | 72 } |
| 30 | 73 |
| 31 } // namespace file_util | 74 } // namespace file_util |
| OLD | NEW |