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 3// found in the LICENSE file. 4 5#include "base/file_util.h" 6 7#include <fcntl.h> 8 9#include <string> 10#include <vector> 11 12#include "base/eintr_wrapper.h" 13#include "base/file_path.h" 14#include "base/string_util.h" 15 16namespace file_util { 17 18bool GetTempDir(FilePath* path) { 19 const char* tmp = getenv("TMPDIR"); 20 if (tmp) 21 *path = FilePath(tmp); 22 else 23 *path = FilePath("/tmp"); 24 return true; 25} 26 27bool GetShmemTempDir(FilePath* path) { 28 *path = FilePath("/dev/shm"); 29 return true; 30} 31 32bool CopyFile(const FilePath& from_path, const FilePath& to_path) { 33 int infile = open(from_path.value().c_str(), O_RDONLY); 34 if (infile < 0) 35 return false; 36 37 int outfile = creat(to_path.value().c_str(), 0666); 38 if (outfile < 0) { 39 close(infile); 40 return false; 41 } 42 43 const size_t kBufferSize = 32768; 44 std::vector<char> buffer(kBufferSize); 45 bool result = true; 46 47 while (result) { 48 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); 49 if (bytes_read < 0) { 50 result = false; 51 break; 52 } 53 if (bytes_read == 0) 54 break; 55 // Allow for partial writes 56 ssize_t bytes_written_per_read = 0; 57 do { 58 ssize_t bytes_written_partial = HANDLE_EINTR(write( 59 outfile, 60 &buffer[bytes_written_per_read], 61 bytes_read - bytes_written_per_read)); 62 if (bytes_written_partial < 0) { 63 result = false; 64 break; 65 } 66 bytes_written_per_read += bytes_written_partial; 67 } while (bytes_written_per_read < bytes_read); 68 } 69 70 if (HANDLE_EINTR(close(infile)) < 0) 71 result = false; 72 if (HANDLE_EINTR(close(outfile)) < 0) 73 result = false; 74 75 return result; 76} 77 78} // namespace file_util 79