1 // Copyright (c) 2009 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/test/test_file_util.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 #include <string>
13
14 #include "base/logging.h"
15 #include "base/file_path.h"
16 #include "base/file_util.h"
17 #include "base/string_util.h"
18
19 namespace file_util {
20
DieFileDie(const FilePath & file,bool recurse)21 bool DieFileDie(const FilePath& file, bool recurse) {
22 // There is no need to workaround Windows problems on POSIX.
23 // Just pass-through.
24 return file_util::Delete(file, recurse);
25 }
26
27 // Mostly a verbatim copy of CopyDirectory
CopyRecursiveDirNoCache(const FilePath & source_dir,const FilePath & dest_dir)28 bool CopyRecursiveDirNoCache(const FilePath& source_dir,
29 const FilePath& dest_dir) {
30 char top_dir[PATH_MAX];
31 if (base::strlcpy(top_dir, source_dir.value().c_str(),
32 arraysize(top_dir)) >= arraysize(top_dir)) {
33 return false;
34 }
35
36 // This function does not properly handle destinations within the source
37 FilePath real_to_path = dest_dir;
38 if (PathExists(real_to_path)) {
39 if (!AbsolutePath(&real_to_path))
40 return false;
41 } else {
42 real_to_path = real_to_path.DirName();
43 if (!AbsolutePath(&real_to_path))
44 return false;
45 }
46 if (real_to_path.value().compare(0, source_dir.value().size(),
47 source_dir.value()) == 0)
48 return false;
49
50 bool success = true;
51 FileEnumerator::FILE_TYPE traverse_type =
52 static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES |
53 FileEnumerator::SHOW_SYM_LINKS | FileEnumerator::DIRECTORIES);
54 FileEnumerator traversal(source_dir, true, traverse_type);
55
56 // dest_dir may not exist yet, start the loop with dest_dir
57 FileEnumerator::FindInfo info;
58 FilePath current = source_dir;
59 if (stat(source_dir.value().c_str(), &info.stat) < 0) {
60 LOG(ERROR) << "CopyRecursiveDirNoCache() couldn't stat source directory: "
61 << source_dir.value() << " errno = " << errno;
62 success = false;
63 }
64
65 while (success && !current.empty()) {
66 // |current| is the source path, including source_dir, so paste
67 // the suffix after source_dir onto dest_dir to create the target_path.
68 std::string suffix(¤t.value().c_str()[source_dir.value().size()]);
69 // Strip the leading '/' (if any).
70 if (!suffix.empty()) {
71 DCHECK_EQ('/', suffix[0]);
72 suffix.erase(0, 1);
73 }
74 const FilePath target_path = dest_dir.Append(suffix);
75
76 if (S_ISDIR(info.stat.st_mode)) {
77 if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 &&
78 errno != EEXIST) {
79 LOG(ERROR) << "CopyRecursiveDirNoCache() couldn't create directory: " <<
80 target_path.value() << " errno = " << errno;
81 success = false;
82 }
83 } else if (S_ISREG(info.stat.st_mode)) {
84 if (CopyFile(current, target_path)) {
85 success = EvictFileFromSystemCache(target_path);
86 DCHECK(success);
87 } else {
88 LOG(ERROR) << "CopyRecursiveDirNoCache() couldn't create file: " <<
89 target_path.value();
90 success = false;
91 }
92 } else {
93 LOG(WARNING) << "CopyRecursiveDirNoCache() skipping non-regular file: " <<
94 current.value();
95 }
96
97 current = traversal.Next();
98 traversal.GetFindInfo(&info);
99 }
100
101 return success;
102 }
103
104 #if !defined(OS_LINUX) && !defined(OS_MACOSX)
EvictFileFromSystemCache(const FilePath & file)105 bool EvictFileFromSystemCache(const FilePath& file) {
106 // There doesn't seem to be a POSIX way to cool the disk cache.
107 NOTIMPLEMENTED();
108 return false;
109 }
110 #endif
111
112 } // namespace file_util
113