OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "net/disk_cache/simple/simple_index_file.h" |
| 6 |
| 7 #include <dirent.h> |
| 8 #include <sys/stat.h> |
| 9 #include <sys/types.h> |
| 10 #include <unistd.h> |
| 11 |
| 12 #include <cstring> |
| 13 #include <string> |
| 14 |
| 15 #include "base/callback.h" |
| 16 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" |
| 18 |
| 19 namespace disk_cache { |
| 20 namespace { |
| 21 |
| 22 struct DirCloser { |
| 23 void operator()(DIR* dir) { closedir(dir); } |
| 24 }; |
| 25 |
| 26 typedef scoped_ptr<DIR, DirCloser> ScopedDir; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 // static |
| 31 bool SimpleIndexFile::TraverseCacheDirectory( |
| 32 const std::string& cache_path, |
| 33 const EntryFileCallback& entry_file_callback) { |
| 34 const ScopedDir dir(opendir(cache_path.c_str())); |
| 35 if (!dir) { |
| 36 PLOG(ERROR) << "opendir " << cache_path; |
| 37 return false; |
| 38 } |
| 39 dirent entry, *result; |
| 40 while (readdir_r(dir.get(), &entry, &result) == 0) { |
| 41 if (!result) |
| 42 return true; // The traversal completed successfully. |
| 43 if (!strcmp(result->d_name, ".") || !strcmp(result->d_name, "..")) |
| 44 continue; |
| 45 entry_file_callback.Run(result->d_name); |
| 46 } |
| 47 PLOG(ERROR) << "readdir_r " << cache_path; |
| 48 return false; |
| 49 } |
| 50 |
| 51 // static |
| 52 bool SimpleIndexFile::GetPlatformFileInfo(const char* path, |
| 53 base::PlatformFileInfo* info) { |
| 54 // Note that base::GetPlatformFileInfo() is not used here since it takes a |
| 55 // file descriptor in which could be inefficient (due to the extra open() + |
| 56 // close()). |
| 57 struct stat stat_info; |
| 58 if (stat(path, &stat_info) < 0) { |
| 59 PLOG(ERROR) << "stat " << path; |
| 60 return false; |
| 61 } |
| 62 memset(info, 0, sizeof(*info)); |
| 63 info->size = stat_info.st_size; |
| 64 info->is_directory = stat_info.st_size; |
| 65 info->last_modified = base::Time::FromTimeT(stat_info.st_mtime); |
| 66 info->last_accessed = base::Time::FromTimeT(stat_info.st_atime); |
| 67 return true; |
| 68 } |
| 69 |
| 70 } // namespace disk_cache |
OLD | NEW |