Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
4 : // Use of this source code is governed by a BSD-style license that can be
5 : // found in the LICENSE file.
6 :
7 : #include "base/file_util.h"
8 :
9 : #if defined(OS_WIN)
10 : #include <io.h>
11 : #endif
12 : #include <stdio.h>
13 : #if defined(ANDROID) || defined(OS_POSIX)
14 : #include <unistd.h>
15 : #endif
16 :
17 : #include <fstream>
18 :
19 : #include "base/file_path.h"
20 : #include "base/logging.h"
21 : #include "base/string_util.h"
22 :
23 : #include "base/string_piece.h"
24 : #include "base/sys_string_conversions.h"
25 :
26 : namespace {
27 :
28 : const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.');
29 :
30 : } // namespace
31 :
32 : namespace file_util {
33 :
34 0 : bool EndsWithSeparator(const FilePath& path) {
35 0 : FilePath::StringType value = path.value();
36 0 : if (value.empty())
37 0 : return false;
38 :
39 0 : return FilePath::IsSeparator(value[value.size() - 1]);
40 : }
41 :
42 0 : void TrimTrailingSeparator(std::wstring* dir) {
43 0 : while (dir->length() > 1 && EndsWithSeparator(dir))
44 0 : dir->resize(dir->length() - 1);
45 0 : }
46 :
47 0 : FilePath::StringType GetFileExtensionFromPath(const FilePath& path) {
48 0 : FilePath::StringType file_name = path.BaseName().value();
49 : const FilePath::StringType::size_type last_dot =
50 0 : file_name.rfind(kExtensionSeparator);
51 0 : return FilePath::StringType(last_dot == FilePath::StringType::npos ?
52 : FILE_PATH_LITERAL("") :
53 0 : file_name, last_dot+1);
54 : }
55 :
56 0 : void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) {
57 : FilePath::StringType& value =
58 0 : const_cast<FilePath::StringType&>(path->value());
59 :
60 : const FilePath::StringType::size_type last_dot =
61 0 : value.rfind(kExtensionSeparator);
62 : const FilePath::StringType::size_type last_separator =
63 0 : value.find_last_of(FilePath::StringType(FilePath::kSeparators));
64 :
65 0 : if (last_dot == FilePath::StringType::npos ||
66 0 : (last_separator != std::wstring::npos && last_dot < last_separator)) {
67 : // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo".
68 : // We should just append the suffix to the entire path.
69 0 : value.append(suffix);
70 0 : return;
71 : }
72 :
73 0 : value.insert(last_dot, suffix);
74 : }
75 :
76 0 : void ReplaceExtension(FilePath* path, const FilePath::StringType& extension) {
77 0 : FilePath::StringType clean_extension;
78 : // If the new extension is "" or ".", then we will just remove the current
79 : // extension.
80 0 : if (!extension.empty() &&
81 0 : extension != FilePath::StringType(&kExtensionSeparator, 1)) {
82 0 : if (extension[0] != kExtensionSeparator)
83 0 : clean_extension.append(&kExtensionSeparator, 1);
84 0 : clean_extension.append(extension);
85 : }
86 :
87 : FilePath::StringType& value =
88 0 : const_cast<FilePath::StringType&>(path->value());
89 : const FilePath::StringType::size_type last_dot =
90 0 : value.rfind(kExtensionSeparator);
91 : const FilePath::StringType::size_type last_separator =
92 0 : value.find_last_of(FilePath::StringType(FilePath::kSeparators));
93 :
94 : // Erase the current extension, if any.
95 0 : if ((last_dot > last_separator ||
96 0 : last_separator == FilePath::StringType::npos) &&
97 : last_dot != FilePath::StringType::npos)
98 0 : value.erase(last_dot);
99 :
100 0 : value.append(clean_extension);
101 0 : }
102 :
103 0 : FILE* CreateAndOpenTemporaryFile(FilePath* path) {
104 0 : FilePath directory;
105 0 : if (!GetTempDir(&directory))
106 0 : return NULL;
107 :
108 0 : return CreateAndOpenTemporaryFileInDir(directory, path);
109 : }
110 :
111 0 : bool GetFileSize(const FilePath& file_path, int64_t* file_size) {
112 : FileInfo info;
113 0 : if (!GetFileInfo(file_path, &info))
114 0 : return false;
115 0 : *file_size = info.size;
116 0 : return true;
117 : }
118 :
119 0 : bool CloseFile(FILE* file) {
120 0 : if (file == NULL)
121 0 : return true;
122 0 : return fclose(file) == 0;
123 : }
124 :
125 : // Deprecated functions ----------------------------------------------------
126 :
127 0 : bool AbsolutePath(std::wstring* path_str) {
128 0 : FilePath path(FilePath::FromWStringHack(*path_str));
129 0 : if (!AbsolutePath(&path))
130 0 : return false;
131 0 : *path_str = path.ToWStringHack();
132 0 : return true;
133 : }
134 0 : void AppendToPath(std::wstring* path, const std::wstring& new_ending) {
135 0 : if (!path) {
136 0 : NOTREACHED();
137 0 : return; // Don't crash in this function in release builds.
138 : }
139 :
140 0 : if (!EndsWithSeparator(path))
141 0 : path->push_back(FilePath::kSeparators[0]);
142 0 : path->append(new_ending);
143 : }
144 0 : bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
145 0 : return CopyFile(FilePath::FromWStringHack(from_path),
146 0 : FilePath::FromWStringHack(to_path));
147 : }
148 0 : bool CreateDirectory(const std::wstring& full_path) {
149 0 : return CreateDirectory(FilePath::FromWStringHack(full_path));
150 : }
151 0 : bool CreateNewTempDirectory(const std::wstring& prefix,
152 : std::wstring* new_temp_path) {
153 : #if defined(OS_WIN)
154 : FilePath::StringType dir_prefix(prefix);
155 : #elif defined(OS_POSIX)
156 0 : FilePath::StringType dir_prefix = WideToUTF8(prefix);
157 : #endif
158 0 : FilePath temp_path;
159 0 : if (!CreateNewTempDirectory(dir_prefix, &temp_path))
160 0 : return false;
161 0 : *new_temp_path = temp_path.ToWStringHack();
162 0 : return true;
163 : }
164 0 : bool CreateTemporaryFileName(std::wstring* temp_file) {
165 0 : FilePath temp_file_path;
166 0 : if (!CreateTemporaryFileName(&temp_file_path))
167 0 : return false;
168 0 : *temp_file = temp_file_path.ToWStringHack();
169 0 : return true;
170 : }
171 0 : bool Delete(const std::wstring& path) {
172 0 : return Delete(FilePath::FromWStringHack(path));
173 : }
174 0 : bool DirectoryExists(const std::wstring& path) {
175 0 : return DirectoryExists(FilePath::FromWStringHack(path));
176 : }
177 0 : bool EndsWithSeparator(std::wstring* path) {
178 0 : return EndsWithSeparator(FilePath::FromWStringHack(*path));
179 : }
180 0 : bool EndsWithSeparator(const std::wstring& path) {
181 0 : return EndsWithSeparator(FilePath::FromWStringHack(path));
182 : }
183 0 : bool GetCurrentDirectory(std::wstring* path_str) {
184 0 : FilePath path;
185 0 : if (!GetCurrentDirectory(&path))
186 0 : return false;
187 0 : *path_str = path.ToWStringHack();
188 0 : return true;
189 : }
190 0 : std::wstring GetFileExtensionFromPath(const std::wstring& path) {
191 : FilePath::StringType extension =
192 0 : GetFileExtensionFromPath(FilePath::FromWStringHack(path));
193 : #if defined(OS_WIN)
194 : return extension;
195 : #elif defined(OS_POSIX)
196 0 : return UTF8ToWide(extension);
197 : #endif
198 : }
199 0 : bool GetFileInfo(const std::wstring& file_path, FileInfo* results) {
200 0 : return GetFileInfo(FilePath::FromWStringHack(file_path), results);
201 : }
202 0 : std::wstring GetFilenameFromPath(const std::wstring& path) {
203 0 : if (path.empty() || EndsWithSeparator(path))
204 0 : return std::wstring();
205 :
206 0 : return FilePath::FromWStringHack(path).BaseName().ToWStringHack();
207 : }
208 0 : bool GetFileSize(const std::wstring& file_path, int64_t* file_size) {
209 0 : return GetFileSize(FilePath::FromWStringHack(file_path), file_size);
210 : }
211 0 : bool GetTempDir(std::wstring* path_str) {
212 0 : FilePath path;
213 0 : if (!GetTempDir(&path))
214 0 : return false;
215 0 : *path_str = path.ToWStringHack();
216 0 : return true;
217 : }
218 0 : FILE* OpenFile(const std::wstring& filename, const char* mode) {
219 0 : return OpenFile(FilePath::FromWStringHack(filename), mode);
220 : }
221 0 : bool PathExists(const std::wstring& path) {
222 0 : return PathExists(FilePath::FromWStringHack(path));
223 : }
224 0 : bool PathIsWritable(const std::wstring& path) {
225 0 : return PathIsWritable(FilePath::FromWStringHack(path));
226 : }
227 0 : int ReadFile(const std::wstring& filename, char* data, int size) {
228 0 : return ReadFile(FilePath::FromWStringHack(filename), data, size);
229 : }
230 0 : bool SetCurrentDirectory(const std::wstring& directory) {
231 0 : return SetCurrentDirectory(FilePath::FromWStringHack(directory));
232 : }
233 0 : void UpOneDirectory(std::wstring* dir) {
234 0 : FilePath path = FilePath::FromWStringHack(*dir);
235 0 : FilePath directory = path.DirName();
236 : // If there is no separator, we will get back kCurrentDirectory.
237 : // In this case don't change |dir|.
238 0 : if (directory.value() != FilePath::kCurrentDirectory)
239 0 : *dir = directory.ToWStringHack();
240 0 : }
241 0 : int WriteFile(const std::wstring& filename, const char* data, int size) {
242 0 : return WriteFile(FilePath::FromWStringHack(filename), data, size);
243 : }
244 : } // namespace
|