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 <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <fnmatch.h> | 9 #include <fnmatch.h> |
10 #include <fts.h> | 10 #include <fts.h> |
(...skipping 20 matching lines...) Expand all Loading... |
31 if (EndsWithSeparator(path)) { | 31 if (EndsWithSeparator(path)) { |
32 std::wstring dir = path; | 32 std::wstring dir = path; |
33 TrimTrailingSeparator(&dir); | 33 TrimTrailingSeparator(&dir); |
34 return dir; | 34 return dir; |
35 } else { | 35 } else { |
36 char full_path[PATH_MAX]; | 36 char full_path[PATH_MAX]; |
37 base::strlcpy(full_path, WideToUTF8(path).c_str(), arraysize(full_path)); | 37 base::strlcpy(full_path, WideToUTF8(path).c_str(), arraysize(full_path)); |
38 return UTF8ToWide(dirname(full_path)); | 38 return UTF8ToWide(dirname(full_path)); |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 bool AbsolutePath(FilePath* path) { | 42 bool AbsolutePath(FilePath* path) { |
43 char full_path[PATH_MAX]; | 43 char full_path[PATH_MAX]; |
44 if (realpath(path->value().c_str(), full_path) == NULL) | 44 if (realpath(path->value().c_str(), full_path) == NULL) |
45 return false; | 45 return false; |
46 *path = FilePath(full_path); | 46 *path = FilePath(full_path); |
47 return true; | 47 return true; |
48 } | 48 } |
49 | 49 |
50 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" | 50 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
51 // which works both with and without the recursive flag. I'm not sure we need | 51 // which works both with and without the recursive flag. I'm not sure we need |
52 // that functionality. If not, remove from file_util_win.cc, otherwise add it | 52 // that functionality. If not, remove from file_util_win.cc, otherwise add it |
53 // here. | 53 // here. |
54 bool Delete(const FilePath& path, bool recursive) { | 54 bool Delete(const FilePath& path, bool recursive) { |
55 const char* path_str = path.value().c_str(); | 55 const char* path_str = path.value().c_str(); |
56 struct stat64 file_info; | 56 struct stat64 file_info; |
57 int test = stat64(path_str, &file_info); | 57 int test = stat64(path_str, &file_info); |
58 if (test != 0) { | 58 if (test != 0) { |
59 // The Windows version defines this condition as success. | 59 // The Windows version defines this condition as success. |
60 bool ret = (errno == ENOENT || errno == ENOTDIR); | 60 bool ret = (errno == ENOENT || errno == ENOTDIR); |
61 return ret; | 61 return ret; |
62 } | 62 } |
63 if (!S_ISDIR(file_info.st_mode)) | 63 if (!S_ISDIR(file_info.st_mode)) |
64 return (unlink(path_str) == 0); | 64 return (unlink(path_str) == 0); |
65 if (!recursive) | 65 if (!recursive) |
66 return (rmdir(path_str) == 0); | 66 return (rmdir(path_str) == 0); |
67 | 67 |
68 bool success = true; | 68 bool success = true; |
69 int ftsflags = FTS_PHYSICAL | FTS_NOSTAT; | 69 int ftsflags = FTS_PHYSICAL | FTS_NOSTAT; |
70 char top_dir[PATH_MAX]; | 70 char top_dir[PATH_MAX]; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 return S_ISDIR(file_info.st_mode); | 244 return S_ISDIR(file_info.st_mode); |
245 return false; | 245 return false; |
246 } | 246 } |
247 | 247 |
248 // TODO(erikkay): implement | 248 // TODO(erikkay): implement |
249 #if 0 | 249 #if 0 |
250 bool GetFileCreationLocalTimeFromHandle(int fd, | 250 bool GetFileCreationLocalTimeFromHandle(int fd, |
251 LPSYSTEMTIME creation_time) { | 251 LPSYSTEMTIME creation_time) { |
252 if (!file_handle) | 252 if (!file_handle) |
253 return false; | 253 return false; |
254 | 254 |
255 FILETIME utc_filetime; | 255 FILETIME utc_filetime; |
256 if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) | 256 if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) |
257 return false; | 257 return false; |
258 | 258 |
259 FILETIME local_filetime; | 259 FILETIME local_filetime; |
260 if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) | 260 if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) |
261 return false; | 261 return false; |
262 | 262 |
263 return !!FileTimeToSystemTime(&local_filetime, creation_time); | 263 return !!FileTimeToSystemTime(&local_filetime, creation_time); |
264 } | 264 } |
265 | 265 |
266 bool GetFileCreationLocalTime(const std::string& filename, | 266 bool GetFileCreationLocalTime(const std::string& filename, |
267 LPSYSTEMTIME creation_time) { | 267 LPSYSTEMTIME creation_time) { |
268 ScopedHandle file_handle( | 268 ScopedHandle file_handle( |
269 CreateFile(filename.c_str(), GENERIC_READ, | 269 CreateFile(filename.c_str(), GENERIC_READ, |
270 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, | 270 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
271 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); | 271 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
272 return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); | 272 return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); |
273 } | 273 } |
274 #endif | 274 #endif |
275 | 275 |
276 bool CreateTemporaryFileName(FilePath* path) { | 276 // Creates and opens a temporary file in |directory|, returning the |
277 if (!GetTempDir(path)) | 277 // file descriptor. |path| is set to the temporary file path. |
278 return false; | 278 // Note TODO(erikkay) comment in header for BlahFileName() calls; the |
279 | 279 // intent is to rename these files BlahFile() (since they create |
280 *path = path->Append(kTempFileName); | 280 // files, not filenames). This function does NOT unlink() the file. |
281 std::string tmpdir_string = path->value(); | 281 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
| 282 *path = directory.Append(kTempFileName); |
| 283 const std::string& tmpdir_string = path->value(); |
282 // this should be OK since mkstemp just replaces characters in place | 284 // this should be OK since mkstemp just replaces characters in place |
283 char* buffer = const_cast<char*>(tmpdir_string.c_str()); | 285 char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
284 | 286 |
285 int fd = mkstemp(buffer); | 287 return mkstemp(buffer); |
| 288 } |
| 289 |
| 290 bool CreateTemporaryFileName(FilePath* path) { |
| 291 FilePath directory; |
| 292 if (!GetTempDir(&directory)) |
| 293 return false; |
| 294 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
286 if (fd < 0) | 295 if (fd < 0) |
287 return false; | 296 return false; |
288 | |
289 close(fd); | 297 close(fd); |
290 return true; | 298 return true; |
291 } | 299 } |
292 | 300 |
| 301 FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 302 FilePath directory; |
| 303 if (!GetTempDir(&directory)) |
| 304 return false; |
| 305 |
| 306 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
| 307 if (fd < 0) |
| 308 return NULL; |
| 309 |
| 310 FILE *fp = fdopen(fd, "a+"); |
| 311 return fp; |
| 312 } |
| 313 |
| 314 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { |
| 315 FilePath directory; |
| 316 if (!GetShmemTempDir(&directory)) |
| 317 return false; |
| 318 |
| 319 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
| 320 if (fd < 0) |
| 321 return NULL; |
| 322 |
| 323 FILE *fp = fdopen(fd, "a+"); |
| 324 return fp; |
| 325 } |
| 326 |
293 bool CreateTemporaryFileNameInDir(const std::wstring& dir, | 327 bool CreateTemporaryFileNameInDir(const std::wstring& dir, |
294 std::wstring* temp_file) { | 328 std::wstring* temp_file) { |
295 // Not implemented yet. | 329 // Not implemented yet. |
296 NOTREACHED(); | 330 NOTREACHED(); |
297 return false; | 331 return false; |
298 } | 332 } |
299 | 333 |
300 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 334 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
301 FilePath* new_temp_path) { | 335 FilePath* new_temp_path) { |
302 FilePath tmpdir; | 336 FilePath tmpdir; |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 munmap(data_, length_); | 571 munmap(data_, length_); |
538 if (file_ != -1) | 572 if (file_ != -1) |
539 close(file_); | 573 close(file_); |
540 | 574 |
541 data_ = NULL; | 575 data_ = NULL; |
542 length_ = 0; | 576 length_ = 0; |
543 file_ = -1; | 577 file_ = -1; |
544 } | 578 } |
545 | 579 |
546 } // namespace file_util | 580 } // namespace file_util |
OLD | NEW |