blob: dad8eddb170a8731639f1530874a651d770f3b84 [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"
spang@chromium.org8d0fa9a2013-11-20 14:33:46 +090027#elif !defined(OS_CHROMEOS) && defined(USE_GLIB)
satorux@chromium.org0c64e3e2013-11-07 20:38:32 +090028#include <glib.h> // for g_get_home_dir()
mark@chromium.org0e56c162009-09-17 02:31:25 +090029#endif
30
mmentovai@google.comaa13be62008-09-03 03:20:34 +090031#include "base/basictypes.h"
brettw@chromium.org56946722013-06-08 13:53:36 +090032#include "base/files/file_enumerator.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090033#include "base/files/file_path.h"
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +090034#include "base/files/scoped_file.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090035#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090036#include "base/memory/scoped_ptr.h"
37#include "base/memory/singleton.h"
nileshagrawal@chromium.org62001b92012-05-18 05:09:06 +090038#include "base/path_service.h"
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090039#include "base/posix/eintr_wrapper.h"
skerner@chromium.org80784142011-10-18 06:30:29 +090040#include "base/stl_util.h"
avi@chromium.org94bd5732013-06-11 22:36:37 +090041#include "base/strings/string_util.h"
42#include "base/strings/stringprintf.h"
brettw@chromium.org1f67a912013-02-08 04:18:03 +090043#include "base/strings/sys_string_conversions.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090044#include "base/strings/utf_string_conversions.h"
stevenjb@chromium.org861313b2013-09-28 04:28:24 +090045#include "base/sys_info.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090046#include "base/threading/thread_restrictions.h"
avi@chromium.orgb039e8b2013-06-28 09:49:07 +090047#include "base/time/time.h"
estade@chromium.org868ecbc2009-06-24 12:29:26 +090048
michaelbai@google.com2251c622011-06-22 07:34:50 +090049#if defined(OS_ANDROID)
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090050#include "base/android/content_uri_utils.h"
michaelbai@google.com2251c622011-06-22 07:34:50 +090051#include "base/os_compat_android.h"
52#endif
53
qsr@chromium.org4ab5de92012-07-09 23:40:39 +090054#if !defined(OS_IOS)
55#include <grp.h>
56#endif
57
brettw@chromium.org99b198e2013-04-12 14:17:15 +090058namespace base {
59
hidehiko58e19072014-11-06 08:51:52 +090060#if !defined(OS_NACL_NONSFI)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090061namespace {
62
rvargas@chromium.org527ea312014-04-05 11:39:18 +090063#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090064static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090065 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090066 return stat(path, sb);
67}
68static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090069 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090070 return lstat(path, sb);
71}
rvargas@chromium.org527ea312014-04-05 11:39:18 +090072#else // defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090073static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090074 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090075 return stat64(path, sb);
76}
77static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090078 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090079 return lstat64(path, sb);
80}
thestig@chromium.org5b29c252014-04-29 20:21:50 +090081#endif // !(defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL))
skerner@google.com93449ef2011-09-22 23:47:18 +090082
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090083// Helper for NormalizeFilePath(), defined below.
84bool RealPath(const FilePath& path, FilePath* real_path) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090085 ThreadRestrictions::AssertIOAllowed(); // For realpath().
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090086 FilePath::CharType buf[PATH_MAX];
87 if (!realpath(path.value().c_str(), buf))
88 return false;
89
90 *real_path = FilePath(buf);
91 return true;
92}
93
skerner@google.com93449ef2011-09-22 23:47:18 +090094// Helper for VerifyPathControlledByUser.
95bool VerifySpecificPathControlledByUser(const FilePath& path,
96 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +090097 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +090098 stat_wrapper_t stat_info;
99 if (CallLstat(path.value().c_str(), &stat_info) != 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900100 DPLOG(ERROR) << "Failed to get information on path "
101 << path.value();
skerner@google.com93449ef2011-09-22 23:47:18 +0900102 return false;
103 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900104
skerner@google.com93449ef2011-09-22 23:47:18 +0900105 if (S_ISLNK(stat_info.st_mode)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900106 DLOG(ERROR) << "Path " << path.value()
skerner@google.com93449ef2011-09-22 23:47:18 +0900107 << " is a symbolic link.";
108 return false;
109 }
110
111 if (stat_info.st_uid != owner_uid) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900112 DLOG(ERROR) << "Path " << path.value()
113 << " is owned by the wrong user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900114 return false;
115 }
116
skerner@chromium.org80784142011-10-18 06:30:29 +0900117 if ((stat_info.st_mode & S_IWGRP) &&
118 !ContainsKey(group_gids, stat_info.st_gid)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900119 DLOG(ERROR) << "Path " << path.value()
120 << " is writable by an unprivileged group.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900121 return false;
122 }
123
124 if (stat_info.st_mode & S_IWOTH) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900125 DLOG(ERROR) << "Path " << path.value()
126 << " is writable by any user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900127 return false;
128 }
129
130 return true;
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900131}
skerner@google.com93449ef2011-09-22 23:47:18 +0900132
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900133std::string TempFileName() {
134#if defined(OS_MACOSX)
135 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
136#endif
137
138#if defined(GOOGLE_CHROME_BUILD)
139 return std::string(".com.google.Chrome.XXXXXX");
140#else
141 return std::string(".org.chromium.Chromium.XXXXXX");
142#endif
143}
144
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900145// Creates and opens a temporary file in |directory|, returning the
146// file descriptor. |path| is set to the temporary file path.
147// This function does NOT unlink() the file.
148int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
149 ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
150 *path = directory.Append(base::TempFileName());
151 const std::string& tmpdir_string = path->value();
152 // this should be OK since mkstemp just replaces characters in place
153 char* buffer = const_cast<char*>(tmpdir_string.c_str());
154
155 return HANDLE_EINTR(mkstemp(buffer));
156}
157
158#if defined(OS_LINUX)
159// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
160// This depends on the mount options used for /dev/shm, which vary among
161// different Linux distributions and possibly local configuration. It also
162// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
163// but its kernel allows mprotect with PROT_EXEC anyway.
164bool DetermineDevShmExecutable() {
165 bool result = false;
166 FilePath path;
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900167
168 ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path));
169 if (fd.is_valid()) {
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900170 DeleteFile(path, false);
171 long sysconf_result = sysconf(_SC_PAGESIZE);
172 CHECK_GE(sysconf_result, 0);
173 size_t pagesize = static_cast<size_t>(sysconf_result);
174 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
thestig@chromium.org5b29c252014-04-29 20:21:50 +0900175 void* mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0);
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900176 if (mapping != MAP_FAILED) {
177 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
178 result = true;
179 munmap(mapping, pagesize);
180 }
181 }
182 return result;
183}
184#endif // defined(OS_LINUX)
185
skerner@google.com93449ef2011-09-22 23:47:18 +0900186} // namespace
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900187
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}
366
367bool DirectoryExists(const FilePath& path) {
368 ThreadRestrictions::AssertIOAllowed();
369 stat_wrapper_t file_info;
370 if (CallStat(path.value().c_str(), &file_info) == 0)
371 return S_ISDIR(file_info.st_mode);
372 return false;
373}
hidehiko58e19072014-11-06 08:51:52 +0900374#endif // !defined(OS_NACL_NONSFI)
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900375
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.";
479#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900480 // g_get_home_dir calls getpwent, which can fall through to LDAP calls so
481 // this may do I/O. However, it should be rare that $HOME is not defined and
482 // this is typically called from the path service which has no threading
483 // restrictions. The path service will cache the result which limits the
484 // badness of blocking on I/O. As a result, we don't have a thread
485 // restriction here.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900486 home_dir = g_get_home_dir();
487 if (home_dir && home_dir[0])
488 return FilePath(home_dir);
489#endif
490
491 FilePath rv;
492 if (GetTempDir(&rv))
493 return rv;
494
495 // Last resort.
496 return FilePath("/tmp");
497}
498#endif // !defined(OS_MACOSX)
499
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900500bool CreateTemporaryFile(FilePath* path) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900501 ThreadRestrictions::AssertIOAllowed(); // For call to close().
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900502 FilePath directory;
503 if (!GetTempDir(&directory))
504 return false;
505 int fd = CreateAndOpenFdForTemporaryFile(directory, path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900506 if (fd < 0)
507 return false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900508 close(fd);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900509 return true;
510}
511
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900512FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
513 int fd = CreateAndOpenFdForTemporaryFile(dir, path);
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900514 if (fd < 0)
515 return NULL;
516
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900517 FILE* file = fdopen(fd, "a+");
518 if (!file)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900519 close(fd);
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900520 return file;
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900521}
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900522
523bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900524 ThreadRestrictions::AssertIOAllowed(); // For call to close().
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900525 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file);
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900526 return ((fd >= 0) && !IGNORE_EINTR(close(fd)));
jcampan@chromium.orgbf29e602008-10-11 03:50:32 +0900527}
528
skerner@chromium.orge4432392010-05-01 02:00:09 +0900529static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
530 const FilePath::StringType& name_tmpl,
531 FilePath* new_dir) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900532 ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900533 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
534 << "Directory name template must contain \"XXXXXX\".";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900535
536 FilePath sub_dir = base_dir.Append(name_tmpl);
537 std::string sub_dir_string = sub_dir.value();
538
539 // this should be OK since mkdtemp just replaces characters in place
540 char* buffer = const_cast<char*>(sub_dir_string.c_str());
541 char* dtemp = mkdtemp(buffer);
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900542 if (!dtemp) {
543 DPLOG(ERROR) << "mkdtemp";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900544 return false;
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900545 }
skerner@chromium.orge4432392010-05-01 02:00:09 +0900546 *new_dir = FilePath(dtemp);
547 return true;
548}
549
550bool CreateTemporaryDirInDir(const FilePath& base_dir,
551 const FilePath::StringType& prefix,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +0900552 FilePath* new_dir) {
skerner@chromium.orge4432392010-05-01 02:00:09 +0900553 FilePath::StringType mkdtemp_template = prefix;
554 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX"));
555 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir);
556}
557
erikkay@google.comcce83822008-12-24 05:20:10 +0900558bool CreateNewTempDirectory(const FilePath::StringType& prefix,
559 FilePath* new_temp_path) {
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900560 FilePath tmpdir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900561 if (!GetTempDir(&tmpdir))
562 return false;
skerner@chromium.orge4432392010-05-01 02:00:09 +0900563
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900564 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900565}
566
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900567bool CreateDirectoryAndGetError(const FilePath& full_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900568 File::Error* error) {
brettw@chromium.org738669a2013-12-04 05:08:54 +0900569 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
evanm@google.com874d1672008-10-31 08:54:04 +0900570 std::vector<FilePath> subpaths;
571
572 // Collect a list of all parent directories.
573 FilePath last_path = full_path;
574 subpaths.push_back(full_path);
575 for (FilePath path = full_path.DirName();
576 path.value() != last_path.value(); path = path.DirName()) {
577 subpaths.push_back(path);
578 last_path = path;
579 }
580
581 // Iterate through the parents and create the missing ones.
582 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
583 i != subpaths.rend(); ++i) {
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900584 if (DirectoryExists(*i))
585 continue;
586 if (mkdir(i->value().c_str(), 0700) == 0)
587 continue;
588 // Mkdir failed, but it might have failed with EEXIST, or some other error
589 // due to the the directory appearing out of thin air. This can occur if
590 // two processes are trying to create the same file system tree at the same
591 // time. Check to see if it exists and make sure it is a directory.
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900592 int saved_errno = errno;
593 if (!DirectoryExists(*i)) {
594 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900595 *error = File::OSErrorToFileError(saved_errno);
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900596 return false;
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900597 }
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900598 }
599 return true;
600}
601
brettw@chromium.org70684242013-12-05 03:22:49 +0900602bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
603 FilePath real_path_result;
604 if (!RealPath(path, &real_path_result))
605 return false;
606
607 // To be consistant with windows, fail if |real_path_result| is a
608 // directory.
609 stat_wrapper_t file_info;
610 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
611 S_ISDIR(file_info.st_mode))
612 return false;
613
614 *normalized_path = real_path_result;
615 return true;
616}
617
brettw@chromium.orga9154032013-12-05 05:56:49 +0900618// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
619// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
620bool IsLink(const FilePath& file_path) {
621 stat_wrapper_t st;
622 // If we can't lstat the file, it's safe to assume that the file won't at
623 // least be a 'followable' link.
624 if (CallLstat(file_path.value().c_str(), &st) != 0)
625 return false;
626
627 if (S_ISLNK(st.st_mode))
628 return true;
629 else
630 return false;
631}
632
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900633bool GetFileInfo(const FilePath& file_path, File::Info* results) {
brettw@chromium.orga9154032013-12-05 05:56:49 +0900634 stat_wrapper_t file_info;
635#if defined(OS_ANDROID)
636 if (file_path.IsContentUri()) {
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900637 File file = OpenContentUriForRead(file_path);
638 if (!file.IsValid())
brettw@chromium.orga9154032013-12-05 05:56:49 +0900639 return false;
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900640 return file.GetInfo(results);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900641 } else {
642#endif // defined(OS_ANDROID)
643 if (CallStat(file_path.value().c_str(), &file_info) != 0)
644 return false;
645#if defined(OS_ANDROID)
646 }
647#endif // defined(OS_ANDROID)
rvargas@chromium.org527ea312014-04-05 11:39:18 +0900648
649 results->FromStat(file_info);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900650 return true;
651}
652
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900653FILE* OpenFile(const FilePath& filename, const char* mode) {
654 ThreadRestrictions::AssertIOAllowed();
655 FILE* result = NULL;
656 do {
657 result = fopen(filename.value().c_str(), mode);
658 } while (!result && errno == EINTR);
659 return result;
660}
661
rvargas@chromium.org43d4a8e2014-06-10 20:19:50 +0900662// NaCl doesn't implement system calls to open files directly.
663#if !defined(OS_NACL)
664FILE* FileToFILE(File file, const char* mode) {
665 FILE* stream = fdopen(file.GetPlatformFile(), mode);
666 if (stream)
667 file.TakePlatformFile();
668 return stream;
669}
670#endif // !defined(OS_NACL)
671
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900672int ReadFile(const FilePath& filename, char* data, int max_size) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900673 ThreadRestrictions::AssertIOAllowed();
674 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY));
675 if (fd < 0)
676 return -1;
677
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900678 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, max_size));
679 if (IGNORE_EINTR(close(fd)) < 0)
680 return -1;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900681 return bytes_read;
682}
683
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900684int WriteFile(const FilePath& filename, const char* data, int size) {
685 ThreadRestrictions::AssertIOAllowed();
aurimas@chromium.orgb4b9d512014-06-04 04:35:06 +0900686 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0640));
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900687 if (fd < 0)
688 return -1;
689
chirantan05106cc2014-10-08 08:15:30 +0900690 int bytes_written = WriteFileDescriptor(fd, data, size) ? size : -1;
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900691 if (IGNORE_EINTR(close(fd)) < 0)
692 return -1;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900693 return bytes_written;
694}
695
chirantan05106cc2014-10-08 08:15:30 +0900696bool WriteFileDescriptor(const int fd, const char* data, int size) {
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900697 // Allow for partial writes.
698 ssize_t bytes_written_total = 0;
699 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
700 bytes_written_total += bytes_written_partial) {
701 bytes_written_partial =
702 HANDLE_EINTR(write(fd, data + bytes_written_total,
703 size - bytes_written_total));
704 if (bytes_written_partial < 0)
chirantan05106cc2014-10-08 08:15:30 +0900705 return false;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900706 }
707
chirantan05106cc2014-10-08 08:15:30 +0900708 return true;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900709}
710
chirantan05106cc2014-10-08 08:15:30 +0900711bool AppendToFile(const FilePath& filename, const char* data, int size) {
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900712 ThreadRestrictions::AssertIOAllowed();
chirantan05106cc2014-10-08 08:15:30 +0900713 bool ret = true;
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900714 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
chirantan05106cc2014-10-08 08:15:30 +0900715 if (fd < 0) {
716 VPLOG(1) << "Unable to create file " << filename.value();
717 return false;
718 }
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900719
chirantan05106cc2014-10-08 08:15:30 +0900720 // This call will either write all of the data or return false.
721 if (!WriteFileDescriptor(fd, data, size)) {
722 VPLOG(1) << "Error while writing to file " << filename.value();
723 ret = false;
724 }
725
726 if (IGNORE_EINTR(close(fd)) < 0) {
727 VPLOG(1) << "Error while closing file " << filename.value();
728 return false;
729 }
730
731 return ret;
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900732}
733
734// Gets the current working directory for the process.
735bool GetCurrentDirectory(FilePath* dir) {
736 // getcwd can return ENOENT, which implies it checks against the disk.
737 ThreadRestrictions::AssertIOAllowed();
738
739 char system_buffer[PATH_MAX] = "";
740 if (!getcwd(system_buffer, sizeof(system_buffer))) {
741 NOTREACHED();
742 return false;
743 }
744 *dir = FilePath(system_buffer);
745 return true;
746}
747
748// Sets the current working directory for the process.
749bool SetCurrentDirectory(const FilePath& path) {
750 ThreadRestrictions::AssertIOAllowed();
751 int ret = chdir(path.value().c_str());
752 return !ret;
753}
754
skerner@google.com93449ef2011-09-22 23:47:18 +0900755bool VerifyPathControlledByUser(const FilePath& base,
756 const FilePath& path,
757 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900758 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900759 if (base != path && !base.IsParent(path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900760 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \""
761 << base.value() << "\", path = \"" << path.value() << "\"";
skerner@google.com93449ef2011-09-22 23:47:18 +0900762 return false;
763 }
764
765 std::vector<FilePath::StringType> base_components;
766 std::vector<FilePath::StringType> path_components;
767
768 base.GetComponents(&base_components);
769 path.GetComponents(&path_components);
770
771 std::vector<FilePath::StringType>::const_iterator ib, ip;
772 for (ib = base_components.begin(), ip = path_components.begin();
773 ib != base_components.end(); ++ib, ++ip) {
774 // |base| must be a subpath of |path|, so all components should match.
775 // If these CHECKs fail, look at the test that base is a parent of
776 // path at the top of this function.
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900777 DCHECK(ip != path_components.end());
778 DCHECK(*ip == *ib);
skerner@google.com93449ef2011-09-22 23:47:18 +0900779 }
780
781 FilePath current_path = base;
skerner@chromium.org80784142011-10-18 06:30:29 +0900782 if (!VerifySpecificPathControlledByUser(current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900783 return false;
784
785 for (; ip != path_components.end(); ++ip) {
786 current_path = current_path.Append(*ip);
skerner@chromium.org80784142011-10-18 06:30:29 +0900787 if (!VerifySpecificPathControlledByUser(
788 current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900789 return false;
790 }
791 return true;
792}
793
qsr@chromium.org4ab5de92012-07-09 23:40:39 +0900794#if defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900795bool VerifyPathControlledByAdmin(const FilePath& path) {
796 const unsigned kRootUid = 0;
797 const FilePath kFileSystemRoot("/");
798
799 // The name of the administrator group on mac os.
skerner@chromium.org80784142011-10-18 06:30:29 +0900800 const char* const kAdminGroupNames[] = {
801 "admin",
802 "wheel"
803 };
skerner@google.com93449ef2011-09-22 23:47:18 +0900804
805 // Reading the groups database may touch the file system.
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900806 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +0900807
skerner@chromium.org80784142011-10-18 06:30:29 +0900808 std::set<gid_t> allowed_group_ids;
809 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) {
810 struct group *group_record = getgrnam(kAdminGroupNames[i]);
811 if (!group_record) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900812 DPLOG(ERROR) << "Could not get the group ID of group \""
813 << kAdminGroupNames[i] << "\".";
skerner@chromium.org80784142011-10-18 06:30:29 +0900814 continue;
815 }
816
817 allowed_group_ids.insert(group_record->gr_gid);
skerner@google.com93449ef2011-09-22 23:47:18 +0900818 }
819
820 return VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +0900821 kFileSystemRoot, path, kRootUid, allowed_group_ids);
skerner@google.com93449ef2011-09-22 23:47:18 +0900822}
stuartmorgan@chromium.org925e0b72012-07-24 20:23:32 +0900823#endif // defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900824
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900825int GetMaximumPathComponentLength(const FilePath& path) {
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900826 ThreadRestrictions::AssertIOAllowed();
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900827 return pathconf(path.value().c_str(), _PC_NAME_MAX);
828}
829
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900830#if !defined(OS_ANDROID)
831// This is implemented in file_util_android.cc for that platform.
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900832bool GetShmemTempDir(bool executable, FilePath* path) {
833#if defined(OS_LINUX)
834 bool use_dev_shm = true;
835 if (executable) {
836 static const bool s_dev_shm_executable = DetermineDevShmExecutable();
837 use_dev_shm = s_dev_shm_executable;
838 }
839 if (use_dev_shm) {
840 *path = FilePath("/dev/shm");
841 return true;
842 }
843#endif
844 return GetTempDir(path);
845}
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900846#endif // !defined(OS_ANDROID)
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900847
cmumford1daaa6b2014-11-18 03:58:20 +0900848#if !defined(OS_MACOSX)
849// Mac has its own implementation, this is for all other Posix systems.
850bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
851 ThreadRestrictions::AssertIOAllowed();
852 File infile;
853#if defined(OS_ANDROID)
854 if (from_path.IsContentUri()) {
855 infile = OpenContentUriForRead(from_path);
856 } else {
857 infile = File(from_path, File::FLAG_OPEN | File::FLAG_READ);
858 }
859#else
860 infile = File(from_path, File::FLAG_OPEN | File::FLAG_READ);
861#endif
862 if (!infile.IsValid())
863 return false;
864
865 File outfile(to_path, File::FLAG_WRITE | File::FLAG_CREATE_ALWAYS);
866 if (!outfile.IsValid())
867 return false;
868
869 const size_t kBufferSize = 32768;
870 std::vector<char> buffer(kBufferSize);
871 bool result = true;
872
873 while (result) {
874 ssize_t bytes_read = infile.ReadAtCurrentPos(&buffer[0], buffer.size());
875 if (bytes_read < 0) {
876 result = false;
877 break;
878 }
879 if (bytes_read == 0)
880 break;
881 // Allow for partial writes
882 ssize_t bytes_written_per_read = 0;
883 do {
884 ssize_t bytes_written_partial = outfile.WriteAtCurrentPos(
885 &buffer[bytes_written_per_read], bytes_read - bytes_written_per_read);
886 if (bytes_written_partial < 0) {
887 result = false;
888 break;
889 }
890 bytes_written_per_read += bytes_written_partial;
891 } while (bytes_written_per_read < bytes_read);
892 }
893
894 return result;
895}
896#endif // !defined(OS_MACOSX)
897
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900898// -----------------------------------------------------------------------------
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900899
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900900namespace internal {
901
902bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
903 ThreadRestrictions::AssertIOAllowed();
904 // Windows compatibility: if to_path exists, from_path and to_path
905 // must be the same type, either both files, or both directories.
906 stat_wrapper_t to_file_info;
907 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
908 stat_wrapper_t from_file_info;
909 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
910 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
911 return false;
912 } else {
913 return false;
914 }
915 }
916
917 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
918 return true;
919
920 if (!CopyDirectory(from_path, to_path, true))
921 return false;
922
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900923 DeleteFile(from_path, true);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900924 return true;
925}
926
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900927} // namespace internal
hidehikoce6251b2014-10-17 15:40:30 +0900928
hidehiko58e19072014-11-06 08:51:52 +0900929#endif // !defined(OS_NACL_NONSFI)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900930} // namespace base