blob: 5a94cef661319e39b98ea10037113371b4a2d2ac [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 <fstream>
32
33#include "base/basictypes.h"
brettw@chromium.org56946722013-06-08 13:53:36 +090034#include "base/files/file_enumerator.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090035#include "base/files/file_path.h"
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +090036#include "base/files/scoped_file.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090037#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090038#include "base/memory/scoped_ptr.h"
39#include "base/memory/singleton.h"
nileshagrawal@chromium.org62001b92012-05-18 05:09:06 +090040#include "base/path_service.h"
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090041#include "base/posix/eintr_wrapper.h"
skerner@chromium.org80784142011-10-18 06:30:29 +090042#include "base/stl_util.h"
avi@chromium.org94bd5732013-06-11 22:36:37 +090043#include "base/strings/string_util.h"
44#include "base/strings/stringprintf.h"
brettw@chromium.org1f67a912013-02-08 04:18:03 +090045#include "base/strings/sys_string_conversions.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090046#include "base/strings/utf_string_conversions.h"
stevenjb@chromium.org861313b2013-09-28 04:28:24 +090047#include "base/sys_info.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090048#include "base/threading/thread_restrictions.h"
avi@chromium.orgb039e8b2013-06-28 09:49:07 +090049#include "base/time/time.h"
estade@chromium.org868ecbc2009-06-24 12:29:26 +090050
michaelbai@google.com2251c622011-06-22 07:34:50 +090051#if defined(OS_ANDROID)
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090052#include "base/android/content_uri_utils.h"
michaelbai@google.com2251c622011-06-22 07:34:50 +090053#include "base/os_compat_android.h"
54#endif
55
qsr@chromium.org4ab5de92012-07-09 23:40:39 +090056#if !defined(OS_IOS)
57#include <grp.h>
58#endif
59
brettw@chromium.org99b198e2013-04-12 14:17:15 +090060namespace base {
61
hidehikoce6251b2014-10-17 15:40:30 +090062#if !defined(__native_client_nonsfi__)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090063namespace {
64
rvargas@chromium.org527ea312014-04-05 11:39:18 +090065#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090066static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090067 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090068 return stat(path, sb);
69}
70static int CallLstat(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 lstat(path, sb);
73}
rvargas@chromium.org527ea312014-04-05 11:39:18 +090074#else // defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090075static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090076 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090077 return stat64(path, sb);
78}
79static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090080 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090081 return lstat64(path, sb);
82}
thestig@chromium.org5b29c252014-04-29 20:21:50 +090083#endif // !(defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL))
skerner@google.com93449ef2011-09-22 23:47:18 +090084
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090085// Helper for NormalizeFilePath(), defined below.
86bool RealPath(const FilePath& path, FilePath* real_path) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090087 ThreadRestrictions::AssertIOAllowed(); // For realpath().
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090088 FilePath::CharType buf[PATH_MAX];
89 if (!realpath(path.value().c_str(), buf))
90 return false;
91
92 *real_path = FilePath(buf);
93 return true;
94}
95
skerner@google.com93449ef2011-09-22 23:47:18 +090096// Helper for VerifyPathControlledByUser.
97bool VerifySpecificPathControlledByUser(const FilePath& path,
98 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +090099 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900100 stat_wrapper_t stat_info;
101 if (CallLstat(path.value().c_str(), &stat_info) != 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900102 DPLOG(ERROR) << "Failed to get information on path "
103 << path.value();
skerner@google.com93449ef2011-09-22 23:47:18 +0900104 return false;
105 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900106
skerner@google.com93449ef2011-09-22 23:47:18 +0900107 if (S_ISLNK(stat_info.st_mode)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900108 DLOG(ERROR) << "Path " << path.value()
skerner@google.com93449ef2011-09-22 23:47:18 +0900109 << " is a symbolic link.";
110 return false;
111 }
112
113 if (stat_info.st_uid != owner_uid) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900114 DLOG(ERROR) << "Path " << path.value()
115 << " is owned by the wrong user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900116 return false;
117 }
118
skerner@chromium.org80784142011-10-18 06:30:29 +0900119 if ((stat_info.st_mode & S_IWGRP) &&
120 !ContainsKey(group_gids, stat_info.st_gid)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900121 DLOG(ERROR) << "Path " << path.value()
122 << " is writable by an unprivileged group.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900123 return false;
124 }
125
126 if (stat_info.st_mode & S_IWOTH) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900127 DLOG(ERROR) << "Path " << path.value()
128 << " is writable by any user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900129 return false;
130 }
131
132 return true;
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900133}
skerner@google.com93449ef2011-09-22 23:47:18 +0900134
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900135std::string TempFileName() {
136#if defined(OS_MACOSX)
137 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
138#endif
139
140#if defined(GOOGLE_CHROME_BUILD)
141 return std::string(".com.google.Chrome.XXXXXX");
142#else
143 return std::string(".org.chromium.Chromium.XXXXXX");
144#endif
145}
146
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900147// Creates and opens a temporary file in |directory|, returning the
148// file descriptor. |path| is set to the temporary file path.
149// This function does NOT unlink() the file.
150int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
151 ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
152 *path = directory.Append(base::TempFileName());
153 const std::string& tmpdir_string = path->value();
154 // this should be OK since mkstemp just replaces characters in place
155 char* buffer = const_cast<char*>(tmpdir_string.c_str());
156
157 return HANDLE_EINTR(mkstemp(buffer));
158}
159
160#if defined(OS_LINUX)
161// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
162// This depends on the mount options used for /dev/shm, which vary among
163// different Linux distributions and possibly local configuration. It also
164// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
165// but its kernel allows mprotect with PROT_EXEC anyway.
166bool DetermineDevShmExecutable() {
167 bool result = false;
168 FilePath path;
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900169
170 ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path));
171 if (fd.is_valid()) {
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900172 DeleteFile(path, false);
173 long sysconf_result = sysconf(_SC_PAGESIZE);
174 CHECK_GE(sysconf_result, 0);
175 size_t pagesize = static_cast<size_t>(sysconf_result);
176 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
thestig@chromium.org5b29c252014-04-29 20:21:50 +0900177 void* mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0);
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900178 if (mapping != MAP_FAILED) {
179 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
180 result = true;
181 munmap(mapping, pagesize);
182 }
183 }
184 return result;
185}
186#endif // defined(OS_LINUX)
187
skerner@google.com93449ef2011-09-22 23:47:18 +0900188} // namespace
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900189
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900190FilePath MakeAbsoluteFilePath(const FilePath& input) {
191 ThreadRestrictions::AssertIOAllowed();
192 char full_path[PATH_MAX];
193 if (realpath(input.value().c_str(), full_path) == NULL)
194 return FilePath();
195 return FilePath(full_path);
mark@chromium.org13aa8aa2011-04-22 13:15:13 +0900196}
mark@chromium.org8ca0d272008-09-12 02:36:23 +0900197
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900198// TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*"
199// which works both with and without the recursive flag. I'm not sure we need
200// that functionality. If not, remove from file_util_win.cc, otherwise add it
201// here.
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900202bool DeleteFile(const FilePath& path, bool recursive) {
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900203 ThreadRestrictions::AssertIOAllowed();
evanm@google.com874d1672008-10-31 08:54:04 +0900204 const char* path_str = path.value().c_str();
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900205 stat_wrapper_t file_info;
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900206 int test = CallLstat(path_str, &file_info);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900207 if (test != 0) {
208 // The Windows version defines this condition as success.
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900209 bool ret = (errno == ENOENT || errno == ENOTDIR);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900210 return ret;
211 }
212 if (!S_ISDIR(file_info.st_mode))
evanm@google.com874d1672008-10-31 08:54:04 +0900213 return (unlink(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900214 if (!recursive)
evanm@google.com874d1672008-10-31 08:54:04 +0900215 return (rmdir(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900216
217 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900218 std::stack<std::string> directories;
219 directories.push(path.value());
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900220 FileEnumerator traversal(path, true,
221 FileEnumerator::FILES | FileEnumerator::DIRECTORIES |
222 FileEnumerator::SHOW_SYM_LINKS);
thestig@chromium.org3217c822009-08-07 06:23:07 +0900223 for (FilePath current = traversal.Next(); success && !current.empty();
224 current = traversal.Next()) {
brettw@chromium.org56946722013-06-08 13:53:36 +0900225 if (traversal.GetInfo().IsDirectory())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900226 directories.push(current.value());
227 else
228 success = (unlink(current.value().c_str()) == 0);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900229 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900230
231 while (success && !directories.empty()) {
232 FilePath dir = FilePath(directories.top());
233 directories.pop();
234 success = (rmdir(dir.value().c_str()) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900235 }
236 return success;
237}
238
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900239bool ReplaceFile(const FilePath& from_path,
240 const FilePath& to_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900241 File::Error* error) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900242 ThreadRestrictions::AssertIOAllowed();
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900243 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
244 return true;
245 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900246 *error = File::OSErrorToFileError(errno);
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900247 return false;
phajdan.jr@chromium.orgd86bea02009-05-20 02:21:07 +0900248}
249
dbeam@chromium.org85aa52a2013-05-08 14:46:20 +0900250bool CopyDirectory(const FilePath& from_path,
251 const FilePath& to_path,
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900252 bool recursive) {
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900253 ThreadRestrictions::AssertIOAllowed();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900254 // Some old callers of CopyDirectory want it to support wildcards.
255 // After some discussion, we decided to fix those callers.
256 // Break loudly here if anyone tries to do this.
evanm@google.com874d1672008-10-31 08:54:04 +0900257 DCHECK(to_path.value().find('*') == std::string::npos);
258 DCHECK(from_path.value().find('*') == std::string::npos);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900259
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900260 if (from_path.value().size() >= PATH_MAX) {
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900261 return false;
262 }
263
thestig@chromium.org3217c822009-08-07 06:23:07 +0900264 // This function does not properly handle destinations within the source
265 FilePath real_to_path = to_path;
brettw@chromium.org10b64122013-07-12 02:36:07 +0900266 if (PathExists(real_to_path)) {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900267 real_to_path = MakeAbsoluteFilePath(real_to_path);
268 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900269 return false;
270 } else {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900271 real_to_path = MakeAbsoluteFilePath(real_to_path.DirName());
272 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900273 return false;
274 }
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900275 FilePath real_from_path = MakeAbsoluteFilePath(from_path);
276 if (real_from_path.empty())
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900277 return false;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900278 if (real_to_path.value().size() >= real_from_path.value().size() &&
279 real_to_path.value().compare(0, real_from_path.value().size(),
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900280 real_from_path.value()) == 0) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900281 return false;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900282 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900283
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900284 int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900285 if (recursive)
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900286 traverse_type |= FileEnumerator::DIRECTORIES;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900287 FileEnumerator traversal(from_path, recursive, traverse_type);
288
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900289 // We have to mimic windows behavior here. |to_path| may not exist yet,
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900290 // start the loop with |to_path|.
brettw@chromium.org56946722013-06-08 13:53:36 +0900291 struct stat from_stat;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900292 FilePath current = from_path;
brettw@chromium.org56946722013-06-08 13:53:36 +0900293 if (stat(from_path.value().c_str(), &from_stat) < 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900294 DLOG(ERROR) << "CopyDirectory() couldn't stat source directory: "
295 << from_path.value() << " errno = " << errno;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900296 return false;
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900297 }
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900298 struct stat to_path_stat;
299 FilePath from_path_base = from_path;
300 if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 &&
301 S_ISDIR(to_path_stat.st_mode)) {
302 // If the destination already exists and is a directory, then the
303 // top level of source needs to be copied.
304 from_path_base = from_path.DirName();
305 }
306
307 // The Windows version of this function assumes that non-recursive calls
308 // will always have a directory for from_path.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900309 // TODO(maruel): This is not necessary anymore.
brettw@chromium.org56946722013-06-08 13:53:36 +0900310 DCHECK(recursive || S_ISDIR(from_stat.st_mode));
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900311
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900312 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900313 while (success && !current.empty()) {
aedla@chromium.orgfef1a202013-01-30 20:38:02 +0900314 // current is the source path, including from_path, so append
315 // the suffix after from_path to to_path to create the target_path.
316 FilePath target_path(to_path);
317 if (from_path_base != current) {
318 if (!from_path_base.AppendRelativePath(current, &target_path)) {
319 success = false;
320 break;
321 }
phajdan.jr@chromium.orgecf50752009-01-14 03:57:46 +0900322 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900323
brettw@chromium.org56946722013-06-08 13:53:36 +0900324 if (S_ISDIR(from_stat.st_mode)) {
325 if (mkdir(target_path.value().c_str(), from_stat.st_mode & 01777) != 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}
hidehikoce6251b2014-10-17 15:40:30 +0900349#endif // !defined(__native_client_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
hidehikoce6251b2014-10-17 15:40:30 +0900361#if !defined(__native_client_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}
hidehikoce6251b2014-10-17 15:40:30 +0900374#endif // !defined(__native_client_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
hidehikoce6251b2014-10-17 15:40:30 +0900388#if !defined(__native_client_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();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900429 DCHECK((mode & ~FILE_PERMISSION_MASK) == 0);
430
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
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900848// -----------------------------------------------------------------------------
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900849
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900850namespace internal {
851
852bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
853 ThreadRestrictions::AssertIOAllowed();
854 // Windows compatibility: if to_path exists, from_path and to_path
855 // must be the same type, either both files, or both directories.
856 stat_wrapper_t to_file_info;
857 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
858 stat_wrapper_t from_file_info;
859 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
860 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
861 return false;
862 } else {
863 return false;
864 }
865 }
866
867 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
868 return true;
869
870 if (!CopyDirectory(from_path, to_path, true))
871 return false;
872
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900873 DeleteFile(from_path, true);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900874 return true;
875}
876
877#if !defined(OS_MACOSX)
878// Mac has its own implementation, this is for all other Posix systems.
879bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
880 ThreadRestrictions::AssertIOAllowed();
881 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY));
882 if (infile < 0)
883 return false;
884
885 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666));
886 if (outfile < 0) {
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900887 close(infile);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900888 return false;
889 }
890
891 const size_t kBufferSize = 32768;
892 std::vector<char> buffer(kBufferSize);
893 bool result = true;
894
895 while (result) {
896 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size()));
897 if (bytes_read < 0) {
898 result = false;
899 break;
900 }
901 if (bytes_read == 0)
902 break;
903 // Allow for partial writes
904 ssize_t bytes_written_per_read = 0;
905 do {
906 ssize_t bytes_written_partial = HANDLE_EINTR(write(
907 outfile,
908 &buffer[bytes_written_per_read],
909 bytes_read - bytes_written_per_read));
910 if (bytes_written_partial < 0) {
911 result = false;
912 break;
913 }
914 bytes_written_per_read += bytes_written_partial;
915 } while (bytes_written_per_read < bytes_read);
916 }
917
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900918 if (IGNORE_EINTR(close(infile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900919 result = false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900920 if (IGNORE_EINTR(close(outfile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900921 result = false;
922
923 return result;
924}
925#endif // !defined(OS_MACOSX)
926
927} // namespace internal
hidehikoce6251b2014-10-17 15:40:30 +0900928
929#endif // !defined(__native_client_nonsfi__)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900930} // namespace base