blob: eb8620d02c4d7479e2bdb7d9ac1655ac47300d56 [file] [log] [blame]
tzik@chromium.orgf5864952012-03-10 09:18:31 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
mmentovai@google.comaa13be62008-09-03 03:20:34 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettw@chromium.org01f3da42014-08-14 05:22:14 +09005#include "base/files/file_util.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +09006
evan@chromium.org37301322009-04-21 10:50:39 +09007#include <dirent.h>
evanm@google.com5c1d39b2008-09-19 04:15:54 +09008#include <errno.h>
mmentovai@google.comaa13be62008-09-03 03:20:34 +09009#include <fcntl.h>
mmentovai@google.comaa13be62008-09-03 03:20:34 +090010#include <libgen.h>
skerner@chromium.org5f3c4562010-05-13 06:36:39 +090011#include <limits.h>
mark@chromium.orgd1bafc62008-10-02 02:40:13 +090012#include <stdio.h>
evan@chromium.org73aec0e2010-04-23 08:28:05 +090013#include <stdlib.h>
evanm@google.com5c1d39b2008-09-19 04:15:54 +090014#include <string.h>
mmentovai@google.comaa13be62008-09-03 03:20:34 +090015#include <sys/errno.h>
estade@chromium.org2c233532008-12-13 08:43:03 +090016#include <sys/mman.h>
skerner@chromium.org559baa92010-05-13 00:13:57 +090017#include <sys/param.h>
mmentovai@google.comaa13be62008-09-03 03:20:34 +090018#include <sys/stat.h>
jochen@chromium.orga6879772010-02-18 19:02:26 +090019#include <sys/time.h>
evan@chromium.org37301322009-04-21 10:50:39 +090020#include <sys/types.h>
mmentovai@google.comaa13be62008-09-03 03:20:34 +090021#include <time.h>
evan@chromium.org37301322009-04-21 10:50:39 +090022#include <unistd.h>
mmentovai@google.comaa13be62008-09-03 03:20:34 +090023
mark@chromium.org0e56c162009-09-17 02:31:25 +090024#if defined(OS_MACOSX)
25#include <AvailabilityMacros.h>
mark@chromium.org13aa8aa2011-04-22 13:15:13 +090026#include "base/mac/foundation_util.h"
mark@chromium.org0e56c162009-09-17 02:31:25 +090027#endif
28
mmentovai@google.comaa13be62008-09-03 03:20:34 +090029#include "base/basictypes.h"
brettw@chromium.org56946722013-06-08 13:53:36 +090030#include "base/files/file_enumerator.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090031#include "base/files/file_path.h"
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +090032#include "base/files/scoped_file.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090033#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090034#include "base/memory/scoped_ptr.h"
35#include "base/memory/singleton.h"
nileshagrawal@chromium.org62001b92012-05-18 05:09:06 +090036#include "base/path_service.h"
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090037#include "base/posix/eintr_wrapper.h"
skerner@chromium.org80784142011-10-18 06:30:29 +090038#include "base/stl_util.h"
avi@chromium.org94bd5732013-06-11 22:36:37 +090039#include "base/strings/string_util.h"
40#include "base/strings/stringprintf.h"
brettw@chromium.org1f67a912013-02-08 04:18:03 +090041#include "base/strings/sys_string_conversions.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090042#include "base/strings/utf_string_conversions.h"
stevenjb@chromium.org861313b2013-09-28 04:28:24 +090043#include "base/sys_info.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090044#include "base/threading/thread_restrictions.h"
avi@chromium.orgb039e8b2013-06-28 09:49:07 +090045#include "base/time/time.h"
estade@chromium.org868ecbc2009-06-24 12:29:26 +090046
michaelbai@google.com2251c622011-06-22 07:34:50 +090047#if defined(OS_ANDROID)
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090048#include "base/android/content_uri_utils.h"
michaelbai@google.com2251c622011-06-22 07:34:50 +090049#include "base/os_compat_android.h"
50#endif
51
qsr@chromium.org4ab5de92012-07-09 23:40:39 +090052#if !defined(OS_IOS)
53#include <grp.h>
54#endif
55
brettw@chromium.org99b198e2013-04-12 14:17:15 +090056namespace base {
57
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090058namespace {
59
rvargas@chromium.org527ea312014-04-05 11:39:18 +090060#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090061static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090062 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090063 return stat(path, sb);
64}
65static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090066 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090067 return lstat(path, sb);
68}
rvargas@chromium.org527ea312014-04-05 11:39:18 +090069#else // defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090070static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090071 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090072 return stat64(path, sb);
73}
74static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090075 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090076 return lstat64(path, sb);
77}
thestig@chromium.org5b29c252014-04-29 20:21:50 +090078#endif // !(defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL))
skerner@google.com93449ef2011-09-22 23:47:18 +090079
hidehiko45972012015-04-02 03:31:15 +090080#if !defined(OS_NACL_NONSFI)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090081// Helper for NormalizeFilePath(), defined below.
82bool RealPath(const FilePath& path, FilePath* real_path) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090083 ThreadRestrictions::AssertIOAllowed(); // For realpath().
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090084 FilePath::CharType buf[PATH_MAX];
85 if (!realpath(path.value().c_str(), buf))
86 return false;
87
88 *real_path = FilePath(buf);
89 return true;
90}
91
skerner@google.com93449ef2011-09-22 23:47:18 +090092// Helper for VerifyPathControlledByUser.
93bool VerifySpecificPathControlledByUser(const FilePath& path,
94 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +090095 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +090096 stat_wrapper_t stat_info;
97 if (CallLstat(path.value().c_str(), &stat_info) != 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090098 DPLOG(ERROR) << "Failed to get information on path "
99 << path.value();
skerner@google.com93449ef2011-09-22 23:47:18 +0900100 return false;
101 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900102
skerner@google.com93449ef2011-09-22 23:47:18 +0900103 if (S_ISLNK(stat_info.st_mode)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900104 DLOG(ERROR) << "Path " << path.value()
skerner@google.com93449ef2011-09-22 23:47:18 +0900105 << " is a symbolic link.";
106 return false;
107 }
108
109 if (stat_info.st_uid != owner_uid) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900110 DLOG(ERROR) << "Path " << path.value()
111 << " is owned by the wrong user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900112 return false;
113 }
114
skerner@chromium.org80784142011-10-18 06:30:29 +0900115 if ((stat_info.st_mode & S_IWGRP) &&
116 !ContainsKey(group_gids, stat_info.st_gid)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900117 DLOG(ERROR) << "Path " << path.value()
118 << " is writable by an unprivileged group.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900119 return false;
120 }
121
122 if (stat_info.st_mode & S_IWOTH) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900123 DLOG(ERROR) << "Path " << path.value()
124 << " is writable by any user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900125 return false;
126 }
127
128 return true;
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900129}
skerner@google.com93449ef2011-09-22 23:47:18 +0900130
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900131std::string TempFileName() {
132#if defined(OS_MACOSX)
133 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
134#endif
135
136#if defined(GOOGLE_CHROME_BUILD)
137 return std::string(".com.google.Chrome.XXXXXX");
138#else
139 return std::string(".org.chromium.Chromium.XXXXXX");
140#endif
141}
142
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900143// Creates and opens a temporary file in |directory|, returning the
144// file descriptor. |path| is set to the temporary file path.
145// This function does NOT unlink() the file.
146int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
147 ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
148 *path = directory.Append(base::TempFileName());
149 const std::string& tmpdir_string = path->value();
150 // this should be OK since mkstemp just replaces characters in place
151 char* buffer = const_cast<char*>(tmpdir_string.c_str());
152
153 return HANDLE_EINTR(mkstemp(buffer));
154}
155
156#if defined(OS_LINUX)
157// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
158// This depends on the mount options used for /dev/shm, which vary among
159// different Linux distributions and possibly local configuration. It also
160// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
161// but its kernel allows mprotect with PROT_EXEC anyway.
162bool DetermineDevShmExecutable() {
163 bool result = false;
164 FilePath path;
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900165
166 ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path));
167 if (fd.is_valid()) {
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900168 DeleteFile(path, false);
169 long sysconf_result = sysconf(_SC_PAGESIZE);
170 CHECK_GE(sysconf_result, 0);
171 size_t pagesize = static_cast<size_t>(sysconf_result);
172 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
thestig@chromium.org5b29c252014-04-29 20:21:50 +0900173 void* mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0);
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900174 if (mapping != MAP_FAILED) {
175 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
176 result = true;
177 munmap(mapping, pagesize);
178 }
179 }
180 return result;
181}
182#endif // defined(OS_LINUX)
hidehiko45972012015-04-02 03:31:15 +0900183#endif // !defined(OS_NACL_NONSFI)
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900184
skerner@google.com93449ef2011-09-22 23:47:18 +0900185} // namespace
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900186
hidehiko45972012015-04-02 03:31:15 +0900187#if !defined(OS_NACL_NONSFI)
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900188FilePath MakeAbsoluteFilePath(const FilePath& input) {
189 ThreadRestrictions::AssertIOAllowed();
190 char full_path[PATH_MAX];
191 if (realpath(input.value().c_str(), full_path) == NULL)
192 return FilePath();
193 return FilePath(full_path);
mark@chromium.org13aa8aa2011-04-22 13:15:13 +0900194}
mark@chromium.org8ca0d272008-09-12 02:36:23 +0900195
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900196// TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*"
197// which works both with and without the recursive flag. I'm not sure we need
198// that functionality. If not, remove from file_util_win.cc, otherwise add it
199// here.
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900200bool DeleteFile(const FilePath& path, bool recursive) {
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900201 ThreadRestrictions::AssertIOAllowed();
evanm@google.com874d1672008-10-31 08:54:04 +0900202 const char* path_str = path.value().c_str();
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900203 stat_wrapper_t file_info;
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900204 int test = CallLstat(path_str, &file_info);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900205 if (test != 0) {
206 // The Windows version defines this condition as success.
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900207 bool ret = (errno == ENOENT || errno == ENOTDIR);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900208 return ret;
209 }
210 if (!S_ISDIR(file_info.st_mode))
evanm@google.com874d1672008-10-31 08:54:04 +0900211 return (unlink(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900212 if (!recursive)
evanm@google.com874d1672008-10-31 08:54:04 +0900213 return (rmdir(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900214
215 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900216 std::stack<std::string> directories;
217 directories.push(path.value());
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900218 FileEnumerator traversal(path, true,
219 FileEnumerator::FILES | FileEnumerator::DIRECTORIES |
220 FileEnumerator::SHOW_SYM_LINKS);
thestig@chromium.org3217c822009-08-07 06:23:07 +0900221 for (FilePath current = traversal.Next(); success && !current.empty();
222 current = traversal.Next()) {
brettw@chromium.org56946722013-06-08 13:53:36 +0900223 if (traversal.GetInfo().IsDirectory())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900224 directories.push(current.value());
225 else
226 success = (unlink(current.value().c_str()) == 0);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900227 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900228
229 while (success && !directories.empty()) {
230 FilePath dir = FilePath(directories.top());
231 directories.pop();
232 success = (rmdir(dir.value().c_str()) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900233 }
234 return success;
235}
236
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900237bool ReplaceFile(const FilePath& from_path,
238 const FilePath& to_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900239 File::Error* error) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900240 ThreadRestrictions::AssertIOAllowed();
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900241 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
242 return true;
243 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900244 *error = File::OSErrorToFileError(errno);
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900245 return false;
phajdan.jr@chromium.orgd86bea02009-05-20 02:21:07 +0900246}
247
dbeam@chromium.org85aa52a2013-05-08 14:46:20 +0900248bool CopyDirectory(const FilePath& from_path,
249 const FilePath& to_path,
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900250 bool recursive) {
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900251 ThreadRestrictions::AssertIOAllowed();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900252 // Some old callers of CopyDirectory want it to support wildcards.
253 // After some discussion, we decided to fix those callers.
254 // Break loudly here if anyone tries to do this.
evanm@google.com874d1672008-10-31 08:54:04 +0900255 DCHECK(to_path.value().find('*') == std::string::npos);
256 DCHECK(from_path.value().find('*') == std::string::npos);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900257
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900258 if (from_path.value().size() >= PATH_MAX) {
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900259 return false;
260 }
261
thestig@chromium.org3217c822009-08-07 06:23:07 +0900262 // This function does not properly handle destinations within the source
263 FilePath real_to_path = to_path;
brettw@chromium.org10b64122013-07-12 02:36:07 +0900264 if (PathExists(real_to_path)) {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900265 real_to_path = MakeAbsoluteFilePath(real_to_path);
266 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900267 return false;
268 } else {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900269 real_to_path = MakeAbsoluteFilePath(real_to_path.DirName());
270 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900271 return false;
272 }
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900273 FilePath real_from_path = MakeAbsoluteFilePath(from_path);
274 if (real_from_path.empty())
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900275 return false;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900276 if (real_to_path.value().size() >= real_from_path.value().size() &&
277 real_to_path.value().compare(0, real_from_path.value().size(),
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900278 real_from_path.value()) == 0) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900279 return false;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900280 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900281
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900282 int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900283 if (recursive)
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900284 traverse_type |= FileEnumerator::DIRECTORIES;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900285 FileEnumerator traversal(from_path, recursive, traverse_type);
286
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900287 // We have to mimic windows behavior here. |to_path| may not exist yet,
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900288 // start the loop with |to_path|.
brettw@chromium.org56946722013-06-08 13:53:36 +0900289 struct stat from_stat;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900290 FilePath current = from_path;
brettw@chromium.org56946722013-06-08 13:53:36 +0900291 if (stat(from_path.value().c_str(), &from_stat) < 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900292 DLOG(ERROR) << "CopyDirectory() couldn't stat source directory: "
293 << from_path.value() << " errno = " << errno;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900294 return false;
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900295 }
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900296 struct stat to_path_stat;
297 FilePath from_path_base = from_path;
298 if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 &&
299 S_ISDIR(to_path_stat.st_mode)) {
300 // If the destination already exists and is a directory, then the
301 // top level of source needs to be copied.
302 from_path_base = from_path.DirName();
303 }
304
305 // The Windows version of this function assumes that non-recursive calls
306 // will always have a directory for from_path.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900307 // TODO(maruel): This is not necessary anymore.
brettw@chromium.org56946722013-06-08 13:53:36 +0900308 DCHECK(recursive || S_ISDIR(from_stat.st_mode));
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900309
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900310 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900311 while (success && !current.empty()) {
aedla@chromium.orgfef1a202013-01-30 20:38:02 +0900312 // current is the source path, including from_path, so append
313 // the suffix after from_path to to_path to create the target_path.
314 FilePath target_path(to_path);
315 if (from_path_base != current) {
316 if (!from_path_base.AppendRelativePath(current, &target_path)) {
317 success = false;
318 break;
319 }
phajdan.jr@chromium.orgecf50752009-01-14 03:57:46 +0900320 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900321
brettw@chromium.org56946722013-06-08 13:53:36 +0900322 if (S_ISDIR(from_stat.st_mode)) {
rippf8459182014-10-27 18:59:53 +0900323 if (mkdir(target_path.value().c_str(),
324 (from_stat.st_mode & 01777) | S_IRUSR | S_IXUSR | S_IWUSR) !=
325 0 &&
thestig@chromium.org3217c822009-08-07 06:23:07 +0900326 errno != EEXIST) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900327 DLOG(ERROR) << "CopyDirectory() couldn't create directory: "
328 << target_path.value() << " errno = " << errno;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900329 success = false;
330 }
brettw@chromium.org56946722013-06-08 13:53:36 +0900331 } else if (S_ISREG(from_stat.st_mode)) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900332 if (!CopyFile(current, target_path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900333 DLOG(ERROR) << "CopyDirectory() couldn't create file: "
334 << target_path.value();
thestig@chromium.org3217c822009-08-07 06:23:07 +0900335 success = false;
336 }
337 } else {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900338 DLOG(WARNING) << "CopyDirectory() skipping non-regular file: "
339 << current.value();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900340 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900341
thestig@chromium.org3217c822009-08-07 06:23:07 +0900342 current = traversal.Next();
brettw@chromium.org56946722013-06-08 13:53:36 +0900343 if (!current.empty())
344 from_stat = traversal.GetInfo().stat();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900345 }
346
thestig@chromium.org3217c822009-08-07 06:23:07 +0900347 return success;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900348}
hidehiko58e19072014-11-06 08:51:52 +0900349#endif // !defined(OS_NACL_NONSFI)
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900350
brettw@chromium.org10b64122013-07-12 02:36:07 +0900351bool PathExists(const FilePath& path) {
352 ThreadRestrictions::AssertIOAllowed();
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +0900353#if defined(OS_ANDROID)
354 if (path.IsContentUri()) {
355 return ContentUriExists(path);
356 }
357#endif
brettw@chromium.org10b64122013-07-12 02:36:07 +0900358 return access(path.value().c_str(), F_OK) == 0;
359}
360
hidehiko58e19072014-11-06 08:51:52 +0900361#if !defined(OS_NACL_NONSFI)
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900362bool PathIsWritable(const FilePath& path) {
363 ThreadRestrictions::AssertIOAllowed();
364 return access(path.value().c_str(), W_OK) == 0;
365}
hidehiko45972012015-04-02 03:31:15 +0900366#endif // !defined(OS_NACL_NONSFI)
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900367
368bool DirectoryExists(const FilePath& path) {
369 ThreadRestrictions::AssertIOAllowed();
370 stat_wrapper_t file_info;
371 if (CallStat(path.value().c_str(), &file_info) == 0)
372 return S_ISDIR(file_info.st_mode);
373 return false;
374}
375
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900376bool ReadFromFD(int fd, char* buffer, size_t bytes) {
377 size_t total_read = 0;
378 while (total_read < bytes) {
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900379 ssize_t bytes_read =
380 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read));
381 if (bytes_read <= 0)
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900382 break;
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900383 total_read += bytes_read;
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900384 }
385 return total_read == bytes;
386}
387
hidehiko58e19072014-11-06 08:51:52 +0900388#if !defined(OS_NACL_NONSFI)
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900389bool CreateSymbolicLink(const FilePath& target_path,
390 const FilePath& symlink_path) {
391 DCHECK(!symlink_path.empty());
392 DCHECK(!target_path.empty());
393 return ::symlink(target_path.value().c_str(),
394 symlink_path.value().c_str()) != -1;
395}
396
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900397bool ReadSymbolicLink(const FilePath& symlink_path, FilePath* target_path) {
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900398 DCHECK(!symlink_path.empty());
399 DCHECK(target_path);
400 char buf[PATH_MAX];
401 ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf));
402
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900403 if (count <= 0) {
404 target_path->clear();
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900405 return false;
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900406 }
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900407
408 *target_path = FilePath(FilePath::StringType(buf, count));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900409 return true;
410}
411
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900412bool GetPosixFilePermissions(const FilePath& path, int* mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900413 ThreadRestrictions::AssertIOAllowed();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900414 DCHECK(mode);
415
416 stat_wrapper_t file_info;
417 // Uses stat(), because on symbolic link, lstat() does not return valid
418 // permission bits in st_mode
419 if (CallStat(path.value().c_str(), &file_info) != 0)
420 return false;
421
422 *mode = file_info.st_mode & FILE_PERMISSION_MASK;
423 return true;
424}
425
426bool SetPosixFilePermissions(const FilePath& path,
427 int mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900428 ThreadRestrictions::AssertIOAllowed();
cmumford83c1a7a2014-11-12 06:28:29 +0900429 DCHECK_EQ(mode & ~FILE_PERMISSION_MASK, 0);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900430
431 // Calls stat() so that we can preserve the higher bits like S_ISGID.
432 stat_wrapper_t stat_buf;
433 if (CallStat(path.value().c_str(), &stat_buf) != 0)
434 return false;
435
436 // Clears the existing permission bits, and adds the new ones.
437 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK;
438 updated_mode_bits |= mode & FILE_PERMISSION_MASK;
439
440 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0)
441 return false;
442
443 return true;
444}
445
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900446#if !defined(OS_MACOSX)
447// This is implemented in file_util_mac.mm for Mac.
448bool GetTempDir(FilePath* path) {
449 const char* tmp = getenv("TMPDIR");
450 if (tmp) {
451 *path = FilePath(tmp);
452 } else {
453#if defined(OS_ANDROID)
454 return PathService::Get(base::DIR_CACHE, path);
455#else
456 *path = FilePath("/tmp");
457#endif
458 }
459 return true;
460}
461#endif // !defined(OS_MACOSX)
462
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900463#if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900464FilePath GetHomeDir() {
465#if defined(OS_CHROMEOS)
nkostylev@chromium.org45c656f2014-05-27 09:12:33 +0900466 if (SysInfo::IsRunningOnChromeOS()) {
nkostylev@chromium.orgcbf27562014-06-04 06:01:57 +0900467 // On Chrome OS chrome::DIR_USER_DATA is overridden with a primary user
468 // homedir once it becomes available. Return / as the safe option.
nkostylev@chromium.org45c656f2014-05-27 09:12:33 +0900469 return FilePath("/");
470 }
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900471#endif
472
473 const char* home_dir = getenv("HOME");
474 if (home_dir && home_dir[0])
475 return FilePath(home_dir);
476
477#if defined(OS_ANDROID)
478 DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900479#endif
480
481 FilePath rv;
482 if (GetTempDir(&rv))
483 return rv;
484
485 // Last resort.
486 return FilePath("/tmp");
487}
488#endif // !defined(OS_MACOSX)
489
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900490bool CreateTemporaryFile(FilePath* path) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900491 ThreadRestrictions::AssertIOAllowed(); // For call to close().
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900492 FilePath directory;
493 if (!GetTempDir(&directory))
494 return false;
495 int fd = CreateAndOpenFdForTemporaryFile(directory, path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900496 if (fd < 0)
497 return false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900498 close(fd);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900499 return true;
500}
501
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900502FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
503 int fd = CreateAndOpenFdForTemporaryFile(dir, path);
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900504 if (fd < 0)
505 return NULL;
506
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900507 FILE* file = fdopen(fd, "a+");
508 if (!file)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900509 close(fd);
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900510 return file;
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900511}
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900512
513bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900514 ThreadRestrictions::AssertIOAllowed(); // For call to close().
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900515 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file);
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900516 return ((fd >= 0) && !IGNORE_EINTR(close(fd)));
jcampan@chromium.orgbf29e602008-10-11 03:50:32 +0900517}
518
skerner@chromium.orge4432392010-05-01 02:00:09 +0900519static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
520 const FilePath::StringType& name_tmpl,
521 FilePath* new_dir) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900522 ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900523 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
524 << "Directory name template must contain \"XXXXXX\".";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900525
526 FilePath sub_dir = base_dir.Append(name_tmpl);
527 std::string sub_dir_string = sub_dir.value();
528
529 // this should be OK since mkdtemp just replaces characters in place
530 char* buffer = const_cast<char*>(sub_dir_string.c_str());
531 char* dtemp = mkdtemp(buffer);
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900532 if (!dtemp) {
533 DPLOG(ERROR) << "mkdtemp";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900534 return false;
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900535 }
skerner@chromium.orge4432392010-05-01 02:00:09 +0900536 *new_dir = FilePath(dtemp);
537 return true;
538}
539
540bool CreateTemporaryDirInDir(const FilePath& base_dir,
541 const FilePath::StringType& prefix,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +0900542 FilePath* new_dir) {
skerner@chromium.orge4432392010-05-01 02:00:09 +0900543 FilePath::StringType mkdtemp_template = prefix;
544 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX"));
545 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir);
546}
547
erikkay@google.comcce83822008-12-24 05:20:10 +0900548bool CreateNewTempDirectory(const FilePath::StringType& prefix,
549 FilePath* new_temp_path) {
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900550 FilePath tmpdir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900551 if (!GetTempDir(&tmpdir))
552 return false;
skerner@chromium.orge4432392010-05-01 02:00:09 +0900553
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900554 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900555}
556
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900557bool CreateDirectoryAndGetError(const FilePath& full_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900558 File::Error* error) {
brettw@chromium.org738669a2013-12-04 05:08:54 +0900559 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
evanm@google.com874d1672008-10-31 08:54:04 +0900560 std::vector<FilePath> subpaths;
561
562 // Collect a list of all parent directories.
563 FilePath last_path = full_path;
564 subpaths.push_back(full_path);
565 for (FilePath path = full_path.DirName();
566 path.value() != last_path.value(); path = path.DirName()) {
567 subpaths.push_back(path);
568 last_path = path;
569 }
570
571 // Iterate through the parents and create the missing ones.
572 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
573 i != subpaths.rend(); ++i) {
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900574 if (DirectoryExists(*i))
575 continue;
576 if (mkdir(i->value().c_str(), 0700) == 0)
577 continue;
578 // Mkdir failed, but it might have failed with EEXIST, or some other error
579 // due to the the directory appearing out of thin air. This can occur if
580 // two processes are trying to create the same file system tree at the same
581 // time. Check to see if it exists and make sure it is a directory.
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900582 int saved_errno = errno;
583 if (!DirectoryExists(*i)) {
584 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900585 *error = File::OSErrorToFileError(saved_errno);
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900586 return false;
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900587 }
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900588 }
589 return true;
590}
591
brettw@chromium.org70684242013-12-05 03:22:49 +0900592bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
593 FilePath real_path_result;
594 if (!RealPath(path, &real_path_result))
595 return false;
596
597 // To be consistant with windows, fail if |real_path_result| is a
598 // directory.
599 stat_wrapper_t file_info;
600 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
601 S_ISDIR(file_info.st_mode))
602 return false;
603
604 *normalized_path = real_path_result;
605 return true;
606}
607
brettw@chromium.orga9154032013-12-05 05:56:49 +0900608// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
609// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
610bool IsLink(const FilePath& file_path) {
611 stat_wrapper_t st;
612 // If we can't lstat the file, it's safe to assume that the file won't at
613 // least be a 'followable' link.
614 if (CallLstat(file_path.value().c_str(), &st) != 0)
615 return false;
616
617 if (S_ISLNK(st.st_mode))
618 return true;
619 else
620 return false;
621}
622
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900623bool GetFileInfo(const FilePath& file_path, File::Info* results) {
brettw@chromium.orga9154032013-12-05 05:56:49 +0900624 stat_wrapper_t file_info;
625#if defined(OS_ANDROID)
626 if (file_path.IsContentUri()) {
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900627 File file = OpenContentUriForRead(file_path);
628 if (!file.IsValid())
brettw@chromium.orga9154032013-12-05 05:56:49 +0900629 return false;
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900630 return file.GetInfo(results);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900631 } else {
632#endif // defined(OS_ANDROID)
633 if (CallStat(file_path.value().c_str(), &file_info) != 0)
634 return false;
635#if defined(OS_ANDROID)
636 }
637#endif // defined(OS_ANDROID)
rvargas@chromium.org527ea312014-04-05 11:39:18 +0900638
639 results->FromStat(file_info);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900640 return true;
641}
hidehiko4f3f44c2015-06-09 11:50:57 +0900642#endif // !defined(OS_NACL_NONSFI)
brettw@chromium.orga9154032013-12-05 05:56:49 +0900643
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900644FILE* OpenFile(const FilePath& filename, const char* mode) {
645 ThreadRestrictions::AssertIOAllowed();
646 FILE* result = NULL;
647 do {
648 result = fopen(filename.value().c_str(), mode);
649 } while (!result && errno == EINTR);
650 return result;
651}
652
rvargas@chromium.org43d4a8e2014-06-10 20:19:50 +0900653// NaCl doesn't implement system calls to open files directly.
654#if !defined(OS_NACL)
655FILE* FileToFILE(File file, const char* mode) {
656 FILE* stream = fdopen(file.GetPlatformFile(), mode);
657 if (stream)
658 file.TakePlatformFile();
659 return stream;
660}
661#endif // !defined(OS_NACL)
662
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900663int ReadFile(const FilePath& filename, char* data, int max_size) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900664 ThreadRestrictions::AssertIOAllowed();
665 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY));
666 if (fd < 0)
667 return -1;
668
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900669 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, max_size));
670 if (IGNORE_EINTR(close(fd)) < 0)
671 return -1;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900672 return bytes_read;
673}
674
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900675int WriteFile(const FilePath& filename, const char* data, int size) {
676 ThreadRestrictions::AssertIOAllowed();
hashimotoc9ac1392015-10-14 14:18:54 +0900677 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900678 if (fd < 0)
679 return -1;
680
chirantan05106cc2014-10-08 08:15:30 +0900681 int bytes_written = WriteFileDescriptor(fd, data, size) ? size : -1;
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900682 if (IGNORE_EINTR(close(fd)) < 0)
683 return -1;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900684 return bytes_written;
685}
686
chirantan05106cc2014-10-08 08:15:30 +0900687bool WriteFileDescriptor(const int fd, const char* data, int size) {
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900688 // Allow for partial writes.
689 ssize_t bytes_written_total = 0;
690 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
691 bytes_written_total += bytes_written_partial) {
692 bytes_written_partial =
693 HANDLE_EINTR(write(fd, data + bytes_written_total,
694 size - bytes_written_total));
695 if (bytes_written_partial < 0)
chirantan05106cc2014-10-08 08:15:30 +0900696 return false;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900697 }
698
chirantan05106cc2014-10-08 08:15:30 +0900699 return true;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900700}
701
hidehiko4f3f44c2015-06-09 11:50:57 +0900702#if !defined(OS_NACL_NONSFI)
703
chirantan05106cc2014-10-08 08:15:30 +0900704bool AppendToFile(const FilePath& filename, const char* data, int size) {
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900705 ThreadRestrictions::AssertIOAllowed();
chirantan05106cc2014-10-08 08:15:30 +0900706 bool ret = true;
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900707 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
chirantan05106cc2014-10-08 08:15:30 +0900708 if (fd < 0) {
709 VPLOG(1) << "Unable to create file " << filename.value();
710 return false;
711 }
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900712
chirantan05106cc2014-10-08 08:15:30 +0900713 // This call will either write all of the data or return false.
714 if (!WriteFileDescriptor(fd, data, size)) {
715 VPLOG(1) << "Error while writing to file " << filename.value();
716 ret = false;
717 }
718
719 if (IGNORE_EINTR(close(fd)) < 0) {
720 VPLOG(1) << "Error while closing file " << filename.value();
721 return false;
722 }
723
724 return ret;
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900725}
726
727// Gets the current working directory for the process.
728bool GetCurrentDirectory(FilePath* dir) {
729 // getcwd can return ENOENT, which implies it checks against the disk.
730 ThreadRestrictions::AssertIOAllowed();
731
732 char system_buffer[PATH_MAX] = "";
733 if (!getcwd(system_buffer, sizeof(system_buffer))) {
734 NOTREACHED();
735 return false;
736 }
737 *dir = FilePath(system_buffer);
738 return true;
739}
740
741// Sets the current working directory for the process.
742bool SetCurrentDirectory(const FilePath& path) {
743 ThreadRestrictions::AssertIOAllowed();
744 int ret = chdir(path.value().c_str());
745 return !ret;
746}
747
skerner@google.com93449ef2011-09-22 23:47:18 +0900748bool VerifyPathControlledByUser(const FilePath& base,
749 const FilePath& path,
750 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900751 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900752 if (base != path && !base.IsParent(path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900753 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \""
754 << base.value() << "\", path = \"" << path.value() << "\"";
skerner@google.com93449ef2011-09-22 23:47:18 +0900755 return false;
756 }
757
758 std::vector<FilePath::StringType> base_components;
759 std::vector<FilePath::StringType> path_components;
760
761 base.GetComponents(&base_components);
762 path.GetComponents(&path_components);
763
764 std::vector<FilePath::StringType>::const_iterator ib, ip;
765 for (ib = base_components.begin(), ip = path_components.begin();
766 ib != base_components.end(); ++ib, ++ip) {
767 // |base| must be a subpath of |path|, so all components should match.
768 // If these CHECKs fail, look at the test that base is a parent of
769 // path at the top of this function.
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900770 DCHECK(ip != path_components.end());
771 DCHECK(*ip == *ib);
skerner@google.com93449ef2011-09-22 23:47:18 +0900772 }
773
774 FilePath current_path = base;
skerner@chromium.org80784142011-10-18 06:30:29 +0900775 if (!VerifySpecificPathControlledByUser(current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900776 return false;
777
778 for (; ip != path_components.end(); ++ip) {
779 current_path = current_path.Append(*ip);
skerner@chromium.org80784142011-10-18 06:30:29 +0900780 if (!VerifySpecificPathControlledByUser(
781 current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900782 return false;
783 }
784 return true;
785}
786
qsr@chromium.org4ab5de92012-07-09 23:40:39 +0900787#if defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900788bool VerifyPathControlledByAdmin(const FilePath& path) {
789 const unsigned kRootUid = 0;
790 const FilePath kFileSystemRoot("/");
791
792 // The name of the administrator group on mac os.
skerner@chromium.org80784142011-10-18 06:30:29 +0900793 const char* const kAdminGroupNames[] = {
794 "admin",
795 "wheel"
796 };
skerner@google.com93449ef2011-09-22 23:47:18 +0900797
798 // Reading the groups database may touch the file system.
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900799 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +0900800
skerner@chromium.org80784142011-10-18 06:30:29 +0900801 std::set<gid_t> allowed_group_ids;
802 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) {
803 struct group *group_record = getgrnam(kAdminGroupNames[i]);
804 if (!group_record) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900805 DPLOG(ERROR) << "Could not get the group ID of group \""
806 << kAdminGroupNames[i] << "\".";
skerner@chromium.org80784142011-10-18 06:30:29 +0900807 continue;
808 }
809
810 allowed_group_ids.insert(group_record->gr_gid);
skerner@google.com93449ef2011-09-22 23:47:18 +0900811 }
812
813 return VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +0900814 kFileSystemRoot, path, kRootUid, allowed_group_ids);
skerner@google.com93449ef2011-09-22 23:47:18 +0900815}
stuartmorgan@chromium.org925e0b72012-07-24 20:23:32 +0900816#endif // defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900817
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900818int GetMaximumPathComponentLength(const FilePath& path) {
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900819 ThreadRestrictions::AssertIOAllowed();
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900820 return pathconf(path.value().c_str(), _PC_NAME_MAX);
821}
822
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900823#if !defined(OS_ANDROID)
824// This is implemented in file_util_android.cc for that platform.
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900825bool GetShmemTempDir(bool executable, FilePath* path) {
826#if defined(OS_LINUX)
827 bool use_dev_shm = true;
828 if (executable) {
829 static const bool s_dev_shm_executable = DetermineDevShmExecutable();
830 use_dev_shm = s_dev_shm_executable;
831 }
832 if (use_dev_shm) {
833 *path = FilePath("/dev/shm");
834 return true;
835 }
836#endif
837 return GetTempDir(path);
838}
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900839#endif // !defined(OS_ANDROID)
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900840
cmumford1daaa6b2014-11-18 03:58:20 +0900841#if !defined(OS_MACOSX)
842// Mac has its own implementation, this is for all other Posix systems.
843bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
844 ThreadRestrictions::AssertIOAllowed();
845 File infile;
846#if defined(OS_ANDROID)
847 if (from_path.IsContentUri()) {
848 infile = OpenContentUriForRead(from_path);
849 } else {
850 infile = File(from_path, File::FLAG_OPEN | File::FLAG_READ);
851 }
852#else
853 infile = File(from_path, File::FLAG_OPEN | File::FLAG_READ);
854#endif
855 if (!infile.IsValid())
856 return false;
857
858 File outfile(to_path, File::FLAG_WRITE | File::FLAG_CREATE_ALWAYS);
859 if (!outfile.IsValid())
860 return false;
861
862 const size_t kBufferSize = 32768;
863 std::vector<char> buffer(kBufferSize);
864 bool result = true;
865
866 while (result) {
867 ssize_t bytes_read = infile.ReadAtCurrentPos(&buffer[0], buffer.size());
868 if (bytes_read < 0) {
869 result = false;
870 break;
871 }
872 if (bytes_read == 0)
873 break;
874 // Allow for partial writes
875 ssize_t bytes_written_per_read = 0;
876 do {
877 ssize_t bytes_written_partial = outfile.WriteAtCurrentPos(
878 &buffer[bytes_written_per_read], bytes_read - bytes_written_per_read);
879 if (bytes_written_partial < 0) {
880 result = false;
881 break;
882 }
883 bytes_written_per_read += bytes_written_partial;
884 } while (bytes_written_per_read < bytes_read);
885 }
886
887 return result;
888}
889#endif // !defined(OS_MACOSX)
890
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900891// -----------------------------------------------------------------------------
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900892
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900893namespace internal {
894
895bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
896 ThreadRestrictions::AssertIOAllowed();
897 // Windows compatibility: if to_path exists, from_path and to_path
898 // must be the same type, either both files, or both directories.
899 stat_wrapper_t to_file_info;
900 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
901 stat_wrapper_t from_file_info;
902 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
903 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
904 return false;
905 } else {
906 return false;
907 }
908 }
909
910 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
911 return true;
912
913 if (!CopyDirectory(from_path, to_path, true))
914 return false;
915
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900916 DeleteFile(from_path, true);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900917 return true;
918}
919
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900920} // namespace internal
hidehikoce6251b2014-10-17 15:40:30 +0900921
hidehiko58e19072014-11-06 08:51:52 +0900922#endif // !defined(OS_NACL_NONSFI)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900923} // namespace base