1 // Copyright (c) 2012 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.
5 #include "base/test/test_file_util.h"
10 #include <sys/types.h>
14 #include "base/file_path.h"
15 #include "base/file_util.h"
16 #include "base/logging.h"
17 #include "base/string_util.h"
18 #include "base/utf_string_conversions.h"
24 // Deny |permission| on the file |path|.
25 bool DenyFilePermission(const FilePath
& path
, mode_t permission
) {
27 if (stat(path
.value().c_str(), &stat_buf
) != 0)
29 stat_buf
.st_mode
&= ~permission
;
31 int rv
= HANDLE_EINTR(chmod(path
.value().c_str(), stat_buf
.st_mode
));
35 // Gets a blob indicating the permission information for |path|.
36 // |length| is the length of the blob. Zero on failure.
37 // Returns the blob pointer, or NULL on failure.
38 void* GetPermissionInfo(const FilePath
& path
, size_t* length
) {
43 if (stat(path
.value().c_str(), &stat_buf
) != 0)
46 *length
= sizeof(mode_t
);
47 mode_t
* mode
= new mode_t
;
48 *mode
= stat_buf
.st_mode
& ~S_IFMT
; // Filter out file/path kind.
53 // Restores the permission information for |path|, given the blob retrieved
54 // using |GetPermissionInfo()|.
55 // |info| is the pointer to the blob.
56 // |length| is the length of the blob.
57 // Either |info| or |length| may be NULL/0, in which case nothing happens.
58 bool RestorePermissionInfo(const FilePath
& path
, void* info
, size_t length
) {
59 if (!info
|| (length
== 0))
62 DCHECK_EQ(sizeof(mode_t
), length
);
63 mode_t
* mode
= reinterpret_cast<mode_t
*>(info
);
65 int rv
= HANDLE_EINTR(chmod(path
.value().c_str(), *mode
));
74 bool DieFileDie(const FilePath
& file
, bool recurse
) {
75 // There is no need to workaround Windows problems on POSIX.
77 return file_util::Delete(file
, recurse
);
80 // Mostly a verbatim copy of CopyDirectory
81 bool CopyRecursiveDirNoCache(const FilePath
& source_dir
,
82 const FilePath
& dest_dir
) {
83 char top_dir
[PATH_MAX
];
84 if (base::strlcpy(top_dir
, source_dir
.value().c_str(),
85 arraysize(top_dir
)) >= arraysize(top_dir
)) {
89 // This function does not properly handle destinations within the source
90 FilePath real_to_path
= dest_dir
;
91 if (PathExists(real_to_path
)) {
92 if (!AbsolutePath(&real_to_path
))
95 real_to_path
= real_to_path
.DirName();
96 if (!AbsolutePath(&real_to_path
))
99 if (real_to_path
.value().compare(0, source_dir
.value().size(),
100 source_dir
.value()) == 0)
104 int traverse_type
= FileEnumerator::FILES
|
105 FileEnumerator::SHOW_SYM_LINKS
| FileEnumerator::DIRECTORIES
;
106 FileEnumerator
traversal(source_dir
, true, traverse_type
);
108 // dest_dir may not exist yet, start the loop with dest_dir
109 FileEnumerator::FindInfo info
;
110 FilePath current
= source_dir
;
111 if (stat(source_dir
.value().c_str(), &info
.stat
) < 0) {
112 DLOG(ERROR
) << "CopyRecursiveDirNoCache() couldn't stat source directory: "
113 << source_dir
.value() << " errno = " << errno
;
117 while (success
&& !current
.empty()) {
118 // |current| is the source path, including source_dir, so paste
119 // the suffix after source_dir onto dest_dir to create the target_path.
120 std::string
suffix(¤t
.value().c_str()[source_dir
.value().size()]);
121 // Strip the leading '/' (if any).
122 if (!suffix
.empty()) {
123 DCHECK_EQ('/', suffix
[0]);
126 const FilePath target_path
= dest_dir
.Append(suffix
);
128 if (S_ISDIR(info
.stat
.st_mode
)) {
129 if (mkdir(target_path
.value().c_str(), info
.stat
.st_mode
& 01777) != 0 &&
131 DLOG(ERROR
) << "CopyRecursiveDirNoCache() couldn't create directory: "
132 << target_path
.value() << " errno = " << errno
;
135 } else if (S_ISREG(info
.stat
.st_mode
)) {
136 if (CopyFile(current
, target_path
)) {
137 success
= EvictFileFromSystemCache(target_path
);
140 DLOG(ERROR
) << "CopyRecursiveDirNoCache() couldn't create file: "
141 << target_path
.value();
145 DLOG(WARNING
) << "CopyRecursiveDirNoCache() skipping non-regular file: "
149 current
= traversal
.Next();
150 traversal
.GetFindInfo(&info
);
156 #if !defined(OS_LINUX) && !defined(OS_MACOSX)
157 bool EvictFileFromSystemCache(const FilePath
& file
) {
158 // There doesn't seem to be a POSIX way to cool the disk cache.
164 std::wstring
FilePathAsWString(const FilePath
& path
) {
165 return UTF8ToWide(path
.value());
167 FilePath
WStringAsFilePath(const std::wstring
& path
) {
168 return FilePath(WideToUTF8(path
));
171 bool MakeFileUnreadable(const FilePath
& path
) {
172 return DenyFilePermission(path
, S_IRUSR
| S_IRGRP
| S_IROTH
);
175 bool MakeFileUnwritable(const FilePath
& path
) {
176 return DenyFilePermission(path
, S_IWUSR
| S_IWGRP
| S_IWOTH
);
179 PermissionRestorer::PermissionRestorer(const FilePath
& path
)
180 : path_(path
), info_(NULL
), length_(0) {
181 info_
= GetPermissionInfo(path_
, &length_
);
182 DCHECK(info_
!= NULL
);
183 DCHECK_NE(0u, length_
);
186 PermissionRestorer::~PermissionRestorer() {
187 if (!RestorePermissionInfo(path_
, info_
, length_
))
191 } // namespace file_util