michael@0: // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/file_util.h" michael@0: michael@0: #if defined(OS_WIN) michael@0: #include michael@0: #endif michael@0: #include michael@0: #if defined(ANDROID) || defined(OS_POSIX) michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #include "base/file_path.h" michael@0: #include "base/logging.h" michael@0: #include "base/string_util.h" michael@0: michael@0: #include "base/string_piece.h" michael@0: #include "base/sys_string_conversions.h" michael@0: michael@0: namespace { michael@0: michael@0: const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); michael@0: michael@0: } // namespace michael@0: michael@0: namespace file_util { michael@0: michael@0: bool EndsWithSeparator(const FilePath& path) { michael@0: FilePath::StringType value = path.value(); michael@0: if (value.empty()) michael@0: return false; michael@0: michael@0: return FilePath::IsSeparator(value[value.size() - 1]); michael@0: } michael@0: michael@0: void TrimTrailingSeparator(std::wstring* dir) { michael@0: while (dir->length() > 1 && EndsWithSeparator(dir)) michael@0: dir->resize(dir->length() - 1); michael@0: } michael@0: michael@0: FilePath::StringType GetFileExtensionFromPath(const FilePath& path) { michael@0: FilePath::StringType file_name = path.BaseName().value(); michael@0: const FilePath::StringType::size_type last_dot = michael@0: file_name.rfind(kExtensionSeparator); michael@0: return FilePath::StringType(last_dot == FilePath::StringType::npos ? michael@0: FILE_PATH_LITERAL("") : michael@0: file_name, last_dot+1); michael@0: } michael@0: michael@0: void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { michael@0: FilePath::StringType& value = michael@0: const_cast(path->value()); michael@0: michael@0: const FilePath::StringType::size_type last_dot = michael@0: value.rfind(kExtensionSeparator); michael@0: const FilePath::StringType::size_type last_separator = michael@0: value.find_last_of(FilePath::StringType(FilePath::kSeparators)); michael@0: michael@0: if (last_dot == FilePath::StringType::npos || michael@0: (last_separator != std::wstring::npos && last_dot < last_separator)) { michael@0: // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". michael@0: // We should just append the suffix to the entire path. michael@0: value.append(suffix); michael@0: return; michael@0: } michael@0: michael@0: value.insert(last_dot, suffix); michael@0: } michael@0: michael@0: void ReplaceExtension(FilePath* path, const FilePath::StringType& extension) { michael@0: FilePath::StringType clean_extension; michael@0: // If the new extension is "" or ".", then we will just remove the current michael@0: // extension. michael@0: if (!extension.empty() && michael@0: extension != FilePath::StringType(&kExtensionSeparator, 1)) { michael@0: if (extension[0] != kExtensionSeparator) michael@0: clean_extension.append(&kExtensionSeparator, 1); michael@0: clean_extension.append(extension); michael@0: } michael@0: michael@0: FilePath::StringType& value = michael@0: const_cast(path->value()); michael@0: const FilePath::StringType::size_type last_dot = michael@0: value.rfind(kExtensionSeparator); michael@0: const FilePath::StringType::size_type last_separator = michael@0: value.find_last_of(FilePath::StringType(FilePath::kSeparators)); michael@0: michael@0: // Erase the current extension, if any. michael@0: if ((last_dot > last_separator || michael@0: last_separator == FilePath::StringType::npos) && michael@0: last_dot != FilePath::StringType::npos) michael@0: value.erase(last_dot); michael@0: michael@0: value.append(clean_extension); michael@0: } michael@0: michael@0: FILE* CreateAndOpenTemporaryFile(FilePath* path) { michael@0: FilePath directory; michael@0: if (!GetTempDir(&directory)) michael@0: return NULL; michael@0: michael@0: return CreateAndOpenTemporaryFileInDir(directory, path); michael@0: } michael@0: michael@0: bool GetFileSize(const FilePath& file_path, int64_t* file_size) { michael@0: FileInfo info; michael@0: if (!GetFileInfo(file_path, &info)) michael@0: return false; michael@0: *file_size = info.size; michael@0: return true; michael@0: } michael@0: michael@0: bool CloseFile(FILE* file) { michael@0: if (file == NULL) michael@0: return true; michael@0: return fclose(file) == 0; michael@0: } michael@0: michael@0: // Deprecated functions ---------------------------------------------------- michael@0: michael@0: bool AbsolutePath(std::wstring* path_str) { michael@0: FilePath path(FilePath::FromWStringHack(*path_str)); michael@0: if (!AbsolutePath(&path)) michael@0: return false; michael@0: *path_str = path.ToWStringHack(); michael@0: return true; michael@0: } michael@0: void AppendToPath(std::wstring* path, const std::wstring& new_ending) { michael@0: if (!path) { michael@0: NOTREACHED(); michael@0: return; // Don't crash in this function in release builds. michael@0: } michael@0: michael@0: if (!EndsWithSeparator(path)) michael@0: path->push_back(FilePath::kSeparators[0]); michael@0: path->append(new_ending); michael@0: } michael@0: bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, michael@0: bool recursive) { michael@0: return CopyDirectory(FilePath::FromWStringHack(from_path), michael@0: FilePath::FromWStringHack(to_path), michael@0: recursive); michael@0: } michael@0: bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { michael@0: return CopyFile(FilePath::FromWStringHack(from_path), michael@0: FilePath::FromWStringHack(to_path)); michael@0: } michael@0: bool CreateDirectory(const std::wstring& full_path) { michael@0: return CreateDirectory(FilePath::FromWStringHack(full_path)); michael@0: } michael@0: bool CreateNewTempDirectory(const std::wstring& prefix, michael@0: std::wstring* new_temp_path) { michael@0: #if defined(OS_WIN) michael@0: FilePath::StringType dir_prefix(prefix); michael@0: #elif defined(OS_POSIX) michael@0: FilePath::StringType dir_prefix = WideToUTF8(prefix); michael@0: #endif michael@0: FilePath temp_path; michael@0: if (!CreateNewTempDirectory(dir_prefix, &temp_path)) michael@0: return false; michael@0: *new_temp_path = temp_path.ToWStringHack(); michael@0: return true; michael@0: } michael@0: bool CreateTemporaryFileName(std::wstring* temp_file) { michael@0: FilePath temp_file_path; michael@0: if (!CreateTemporaryFileName(&temp_file_path)) michael@0: return false; michael@0: *temp_file = temp_file_path.ToWStringHack(); michael@0: return true; michael@0: } michael@0: bool Delete(const std::wstring& path, bool recursive) { michael@0: return Delete(FilePath::FromWStringHack(path), recursive); michael@0: } michael@0: bool DirectoryExists(const std::wstring& path) { michael@0: return DirectoryExists(FilePath::FromWStringHack(path)); michael@0: } michael@0: bool EndsWithSeparator(std::wstring* path) { michael@0: return EndsWithSeparator(FilePath::FromWStringHack(*path)); michael@0: } michael@0: bool EndsWithSeparator(const std::wstring& path) { michael@0: return EndsWithSeparator(FilePath::FromWStringHack(path)); michael@0: } michael@0: bool GetCurrentDirectory(std::wstring* path_str) { michael@0: FilePath path; michael@0: if (!GetCurrentDirectory(&path)) michael@0: return false; michael@0: *path_str = path.ToWStringHack(); michael@0: return true; michael@0: } michael@0: std::wstring GetFileExtensionFromPath(const std::wstring& path) { michael@0: FilePath::StringType extension = michael@0: GetFileExtensionFromPath(FilePath::FromWStringHack(path)); michael@0: #if defined(OS_WIN) michael@0: return extension; michael@0: #elif defined(OS_POSIX) michael@0: return UTF8ToWide(extension); michael@0: #endif michael@0: } michael@0: bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { michael@0: return GetFileInfo(FilePath::FromWStringHack(file_path), results); michael@0: } michael@0: std::wstring GetFilenameFromPath(const std::wstring& path) { michael@0: if (path.empty() || EndsWithSeparator(path)) michael@0: return std::wstring(); michael@0: michael@0: return FilePath::FromWStringHack(path).BaseName().ToWStringHack(); michael@0: } michael@0: bool GetFileSize(const std::wstring& file_path, int64_t* file_size) { michael@0: return GetFileSize(FilePath::FromWStringHack(file_path), file_size); michael@0: } michael@0: bool GetTempDir(std::wstring* path_str) { michael@0: FilePath path; michael@0: if (!GetTempDir(&path)) michael@0: return false; michael@0: *path_str = path.ToWStringHack(); michael@0: return true; michael@0: } michael@0: FILE* OpenFile(const std::wstring& filename, const char* mode) { michael@0: return OpenFile(FilePath::FromWStringHack(filename), mode); michael@0: } michael@0: bool PathExists(const std::wstring& path) { michael@0: return PathExists(FilePath::FromWStringHack(path)); michael@0: } michael@0: bool PathIsWritable(const std::wstring& path) { michael@0: return PathIsWritable(FilePath::FromWStringHack(path)); michael@0: } michael@0: int ReadFile(const std::wstring& filename, char* data, int size) { michael@0: return ReadFile(FilePath::FromWStringHack(filename), data, size); michael@0: } michael@0: bool SetCurrentDirectory(const std::wstring& directory) { michael@0: return SetCurrentDirectory(FilePath::FromWStringHack(directory)); michael@0: } michael@0: void UpOneDirectory(std::wstring* dir) { michael@0: FilePath path = FilePath::FromWStringHack(*dir); michael@0: FilePath directory = path.DirName(); michael@0: // If there is no separator, we will get back kCurrentDirectory. michael@0: // In this case don't change |dir|. michael@0: if (directory.value() != FilePath::kCurrentDirectory) michael@0: *dir = directory.ToWStringHack(); michael@0: } michael@0: int WriteFile(const std::wstring& filename, const char* data, int size) { michael@0: return WriteFile(FilePath::FromWStringHack(filename), data, size); michael@0: } michael@0: } // namespace