blob: d86d9bcc7e94f2067b35ae74855d55a4d1bee290 [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)) {
rippf8459182014-10-27 18:59:53 +0900325 if (mkdir(target_path.value().c_str(),
326 (from_stat.st_mode & 01777) | S_IRUSR | S_IXUSR | S_IWUSR) !=
327 0 &&
thestig@chromium.org3217c822009-08-07 06:23:07 +0900328 errno != EEXIST) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900329 DLOG(ERROR) << "CopyDirectory() couldn't create directory: "
330 << target_path.value() << " errno = " << errno;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900331 success = false;
332 }
brettw@chromium.org56946722013-06-08 13:53:36 +0900333 } else if (S_ISREG(from_stat.st_mode)) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900334 if (!CopyFile(current, target_path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900335 DLOG(ERROR) << "CopyDirectory() couldn't create file: "
336 << target_path.value();
thestig@chromium.org3217c822009-08-07 06:23:07 +0900337 success = false;
338 }
339 } else {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900340 DLOG(WARNING) << "CopyDirectory() skipping non-regular file: "
341 << current.value();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900342 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900343
thestig@chromium.org3217c822009-08-07 06:23:07 +0900344 current = traversal.Next();
brettw@chromium.org56946722013-06-08 13:53:36 +0900345 if (!current.empty())
346 from_stat = traversal.GetInfo().stat();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900347 }
348
thestig@chromium.org3217c822009-08-07 06:23:07 +0900349 return success;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900350}
hidehikoce6251b2014-10-17 15:40:30 +0900351#endif // !defined(__native_client_nonsfi__)
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900352
brettw@chromium.org10b64122013-07-12 02:36:07 +0900353bool PathExists(const FilePath& path) {
354 ThreadRestrictions::AssertIOAllowed();
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +0900355#if defined(OS_ANDROID)
356 if (path.IsContentUri()) {
357 return ContentUriExists(path);
358 }
359#endif
brettw@chromium.org10b64122013-07-12 02:36:07 +0900360 return access(path.value().c_str(), F_OK) == 0;
361}
362
hidehikoce6251b2014-10-17 15:40:30 +0900363#if !defined(__native_client_nonsfi__)
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900364bool PathIsWritable(const FilePath& path) {
365 ThreadRestrictions::AssertIOAllowed();
366 return access(path.value().c_str(), W_OK) == 0;
367}
368
369bool DirectoryExists(const FilePath& path) {
370 ThreadRestrictions::AssertIOAllowed();
371 stat_wrapper_t file_info;
372 if (CallStat(path.value().c_str(), &file_info) == 0)
373 return S_ISDIR(file_info.st_mode);
374 return false;
375}
hidehikoce6251b2014-10-17 15:40:30 +0900376#endif // !defined(__native_client_nonsfi__)
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900377
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900378bool ReadFromFD(int fd, char* buffer, size_t bytes) {
379 size_t total_read = 0;
380 while (total_read < bytes) {
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900381 ssize_t bytes_read =
382 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read));
383 if (bytes_read <= 0)
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900384 break;
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900385 total_read += bytes_read;
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900386 }
387 return total_read == bytes;
388}
389
hidehikoce6251b2014-10-17 15:40:30 +0900390#if !defined(__native_client_nonsfi__)
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900391bool CreateSymbolicLink(const FilePath& target_path,
392 const FilePath& symlink_path) {
393 DCHECK(!symlink_path.empty());
394 DCHECK(!target_path.empty());
395 return ::symlink(target_path.value().c_str(),
396 symlink_path.value().c_str()) != -1;
397}
398
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900399bool ReadSymbolicLink(const FilePath& symlink_path, FilePath* target_path) {
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900400 DCHECK(!symlink_path.empty());
401 DCHECK(target_path);
402 char buf[PATH_MAX];
403 ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf));
404
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900405 if (count <= 0) {
406 target_path->clear();
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900407 return false;
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900408 }
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900409
410 *target_path = FilePath(FilePath::StringType(buf, count));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900411 return true;
412}
413
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900414bool GetPosixFilePermissions(const FilePath& path, int* mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900415 ThreadRestrictions::AssertIOAllowed();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900416 DCHECK(mode);
417
418 stat_wrapper_t file_info;
419 // Uses stat(), because on symbolic link, lstat() does not return valid
420 // permission bits in st_mode
421 if (CallStat(path.value().c_str(), &file_info) != 0)
422 return false;
423
424 *mode = file_info.st_mode & FILE_PERMISSION_MASK;
425 return true;
426}
427
428bool SetPosixFilePermissions(const FilePath& path,
429 int mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900430 ThreadRestrictions::AssertIOAllowed();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900431 DCHECK((mode & ~FILE_PERMISSION_MASK) == 0);
432
433 // Calls stat() so that we can preserve the higher bits like S_ISGID.
434 stat_wrapper_t stat_buf;
435 if (CallStat(path.value().c_str(), &stat_buf) != 0)
436 return false;
437
438 // Clears the existing permission bits, and adds the new ones.
439 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK;
440 updated_mode_bits |= mode & FILE_PERMISSION_MASK;
441
442 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0)
443 return false;
444
445 return true;
446}
447
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900448#if !defined(OS_MACOSX)
449// This is implemented in file_util_mac.mm for Mac.
450bool GetTempDir(FilePath* path) {
451 const char* tmp = getenv("TMPDIR");
452 if (tmp) {
453 *path = FilePath(tmp);
454 } else {
455#if defined(OS_ANDROID)
456 return PathService::Get(base::DIR_CACHE, path);
457#else
458 *path = FilePath("/tmp");
459#endif
460 }
461 return true;
462}
463#endif // !defined(OS_MACOSX)
464
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900465#if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900466FilePath GetHomeDir() {
467#if defined(OS_CHROMEOS)
nkostylev@chromium.org45c656f2014-05-27 09:12:33 +0900468 if (SysInfo::IsRunningOnChromeOS()) {
nkostylev@chromium.orgcbf27562014-06-04 06:01:57 +0900469 // On Chrome OS chrome::DIR_USER_DATA is overridden with a primary user
470 // homedir once it becomes available. Return / as the safe option.
nkostylev@chromium.org45c656f2014-05-27 09:12:33 +0900471 return FilePath("/");
472 }
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900473#endif
474
475 const char* home_dir = getenv("HOME");
476 if (home_dir && home_dir[0])
477 return FilePath(home_dir);
478
479#if defined(OS_ANDROID)
480 DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
481#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900482 // g_get_home_dir calls getpwent, which can fall through to LDAP calls so
483 // this may do I/O. However, it should be rare that $HOME is not defined and
484 // this is typically called from the path service which has no threading
485 // restrictions. The path service will cache the result which limits the
486 // badness of blocking on I/O. As a result, we don't have a thread
487 // restriction here.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900488 home_dir = g_get_home_dir();
489 if (home_dir && home_dir[0])
490 return FilePath(home_dir);
491#endif
492
493 FilePath rv;
494 if (GetTempDir(&rv))
495 return rv;
496
497 // Last resort.
498 return FilePath("/tmp");
499}
500#endif // !defined(OS_MACOSX)
501
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900502bool CreateTemporaryFile(FilePath* path) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900503 ThreadRestrictions::AssertIOAllowed(); // For call to close().
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900504 FilePath directory;
505 if (!GetTempDir(&directory))
506 return false;
507 int fd = CreateAndOpenFdForTemporaryFile(directory, path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900508 if (fd < 0)
509 return false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900510 close(fd);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900511 return true;
512}
513
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900514FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
515 int fd = CreateAndOpenFdForTemporaryFile(dir, path);
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900516 if (fd < 0)
517 return NULL;
518
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900519 FILE* file = fdopen(fd, "a+");
520 if (!file)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900521 close(fd);
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900522 return file;
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900523}
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900524
525bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900526 ThreadRestrictions::AssertIOAllowed(); // For call to close().
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900527 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file);
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900528 return ((fd >= 0) && !IGNORE_EINTR(close(fd)));
jcampan@chromium.orgbf29e602008-10-11 03:50:32 +0900529}
530
skerner@chromium.orge4432392010-05-01 02:00:09 +0900531static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
532 const FilePath::StringType& name_tmpl,
533 FilePath* new_dir) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900534 ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900535 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
536 << "Directory name template must contain \"XXXXXX\".";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900537
538 FilePath sub_dir = base_dir.Append(name_tmpl);
539 std::string sub_dir_string = sub_dir.value();
540
541 // this should be OK since mkdtemp just replaces characters in place
542 char* buffer = const_cast<char*>(sub_dir_string.c_str());
543 char* dtemp = mkdtemp(buffer);
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900544 if (!dtemp) {
545 DPLOG(ERROR) << "mkdtemp";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900546 return false;
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900547 }
skerner@chromium.orge4432392010-05-01 02:00:09 +0900548 *new_dir = FilePath(dtemp);
549 return true;
550}
551
552bool CreateTemporaryDirInDir(const FilePath& base_dir,
553 const FilePath::StringType& prefix,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +0900554 FilePath* new_dir) {
skerner@chromium.orge4432392010-05-01 02:00:09 +0900555 FilePath::StringType mkdtemp_template = prefix;
556 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX"));
557 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir);
558}
559
erikkay@google.comcce83822008-12-24 05:20:10 +0900560bool CreateNewTempDirectory(const FilePath::StringType& prefix,
561 FilePath* new_temp_path) {
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900562 FilePath tmpdir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900563 if (!GetTempDir(&tmpdir))
564 return false;
skerner@chromium.orge4432392010-05-01 02:00:09 +0900565
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900566 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900567}
568
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900569bool CreateDirectoryAndGetError(const FilePath& full_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900570 File::Error* error) {
brettw@chromium.org738669a2013-12-04 05:08:54 +0900571 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
evanm@google.com874d1672008-10-31 08:54:04 +0900572 std::vector<FilePath> subpaths;
573
574 // Collect a list of all parent directories.
575 FilePath last_path = full_path;
576 subpaths.push_back(full_path);
577 for (FilePath path = full_path.DirName();
578 path.value() != last_path.value(); path = path.DirName()) {
579 subpaths.push_back(path);
580 last_path = path;
581 }
582
583 // Iterate through the parents and create the missing ones.
584 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
585 i != subpaths.rend(); ++i) {
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900586 if (DirectoryExists(*i))
587 continue;
588 if (mkdir(i->value().c_str(), 0700) == 0)
589 continue;
590 // Mkdir failed, but it might have failed with EEXIST, or some other error
591 // due to the the directory appearing out of thin air. This can occur if
592 // two processes are trying to create the same file system tree at the same
593 // time. Check to see if it exists and make sure it is a directory.
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900594 int saved_errno = errno;
595 if (!DirectoryExists(*i)) {
596 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900597 *error = File::OSErrorToFileError(saved_errno);
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900598 return false;
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900599 }
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900600 }
601 return true;
602}
603
brettw@chromium.org70684242013-12-05 03:22:49 +0900604bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
605 FilePath real_path_result;
606 if (!RealPath(path, &real_path_result))
607 return false;
608
609 // To be consistant with windows, fail if |real_path_result| is a
610 // directory.
611 stat_wrapper_t file_info;
612 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
613 S_ISDIR(file_info.st_mode))
614 return false;
615
616 *normalized_path = real_path_result;
617 return true;
618}
619
brettw@chromium.orga9154032013-12-05 05:56:49 +0900620// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
621// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
622bool IsLink(const FilePath& file_path) {
623 stat_wrapper_t st;
624 // If we can't lstat the file, it's safe to assume that the file won't at
625 // least be a 'followable' link.
626 if (CallLstat(file_path.value().c_str(), &st) != 0)
627 return false;
628
629 if (S_ISLNK(st.st_mode))
630 return true;
631 else
632 return false;
633}
634
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900635bool GetFileInfo(const FilePath& file_path, File::Info* results) {
brettw@chromium.orga9154032013-12-05 05:56:49 +0900636 stat_wrapper_t file_info;
637#if defined(OS_ANDROID)
638 if (file_path.IsContentUri()) {
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900639 File file = OpenContentUriForRead(file_path);
640 if (!file.IsValid())
brettw@chromium.orga9154032013-12-05 05:56:49 +0900641 return false;
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900642 return file.GetInfo(results);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900643 } else {
644#endif // defined(OS_ANDROID)
645 if (CallStat(file_path.value().c_str(), &file_info) != 0)
646 return false;
647#if defined(OS_ANDROID)
648 }
649#endif // defined(OS_ANDROID)
rvargas@chromium.org527ea312014-04-05 11:39:18 +0900650
651 results->FromStat(file_info);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900652 return true;
653}
654
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900655FILE* OpenFile(const FilePath& filename, const char* mode) {
656 ThreadRestrictions::AssertIOAllowed();
657 FILE* result = NULL;
658 do {
659 result = fopen(filename.value().c_str(), mode);
660 } while (!result && errno == EINTR);
661 return result;
662}
663
rvargas@chromium.org43d4a8e2014-06-10 20:19:50 +0900664// NaCl doesn't implement system calls to open files directly.
665#if !defined(OS_NACL)
666FILE* FileToFILE(File file, const char* mode) {
667 FILE* stream = fdopen(file.GetPlatformFile(), mode);
668 if (stream)
669 file.TakePlatformFile();
670 return stream;
671}
672#endif // !defined(OS_NACL)
673
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900674int ReadFile(const FilePath& filename, char* data, int max_size) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900675 ThreadRestrictions::AssertIOAllowed();
676 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY));
677 if (fd < 0)
678 return -1;
679
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900680 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, max_size));
681 if (IGNORE_EINTR(close(fd)) < 0)
682 return -1;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900683 return bytes_read;
684}
685
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900686int WriteFile(const FilePath& filename, const char* data, int size) {
687 ThreadRestrictions::AssertIOAllowed();
aurimas@chromium.orgb4b9d512014-06-04 04:35:06 +0900688 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0640));
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900689 if (fd < 0)
690 return -1;
691
chirantan05106cc2014-10-08 08:15:30 +0900692 int bytes_written = WriteFileDescriptor(fd, data, size) ? size : -1;
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900693 if (IGNORE_EINTR(close(fd)) < 0)
694 return -1;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900695 return bytes_written;
696}
697
chirantan05106cc2014-10-08 08:15:30 +0900698bool WriteFileDescriptor(const int fd, const char* data, int size) {
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900699 // Allow for partial writes.
700 ssize_t bytes_written_total = 0;
701 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
702 bytes_written_total += bytes_written_partial) {
703 bytes_written_partial =
704 HANDLE_EINTR(write(fd, data + bytes_written_total,
705 size - bytes_written_total));
706 if (bytes_written_partial < 0)
chirantan05106cc2014-10-08 08:15:30 +0900707 return false;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900708 }
709
chirantan05106cc2014-10-08 08:15:30 +0900710 return true;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900711}
712
chirantan05106cc2014-10-08 08:15:30 +0900713bool AppendToFile(const FilePath& filename, const char* data, int size) {
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900714 ThreadRestrictions::AssertIOAllowed();
chirantan05106cc2014-10-08 08:15:30 +0900715 bool ret = true;
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900716 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
chirantan05106cc2014-10-08 08:15:30 +0900717 if (fd < 0) {
718 VPLOG(1) << "Unable to create file " << filename.value();
719 return false;
720 }
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900721
chirantan05106cc2014-10-08 08:15:30 +0900722 // This call will either write all of the data or return false.
723 if (!WriteFileDescriptor(fd, data, size)) {
724 VPLOG(1) << "Error while writing to file " << filename.value();
725 ret = false;
726 }
727
728 if (IGNORE_EINTR(close(fd)) < 0) {
729 VPLOG(1) << "Error while closing file " << filename.value();
730 return false;
731 }
732
733 return ret;
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900734}
735
736// Gets the current working directory for the process.
737bool GetCurrentDirectory(FilePath* dir) {
738 // getcwd can return ENOENT, which implies it checks against the disk.
739 ThreadRestrictions::AssertIOAllowed();
740
741 char system_buffer[PATH_MAX] = "";
742 if (!getcwd(system_buffer, sizeof(system_buffer))) {
743 NOTREACHED();
744 return false;
745 }
746 *dir = FilePath(system_buffer);
747 return true;
748}
749
750// Sets the current working directory for the process.
751bool SetCurrentDirectory(const FilePath& path) {
752 ThreadRestrictions::AssertIOAllowed();
753 int ret = chdir(path.value().c_str());
754 return !ret;
755}
756
skerner@google.com93449ef2011-09-22 23:47:18 +0900757bool VerifyPathControlledByUser(const FilePath& base,
758 const FilePath& path,
759 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900760 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900761 if (base != path && !base.IsParent(path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900762 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \""
763 << base.value() << "\", path = \"" << path.value() << "\"";
skerner@google.com93449ef2011-09-22 23:47:18 +0900764 return false;
765 }
766
767 std::vector<FilePath::StringType> base_components;
768 std::vector<FilePath::StringType> path_components;
769
770 base.GetComponents(&base_components);
771 path.GetComponents(&path_components);
772
773 std::vector<FilePath::StringType>::const_iterator ib, ip;
774 for (ib = base_components.begin(), ip = path_components.begin();
775 ib != base_components.end(); ++ib, ++ip) {
776 // |base| must be a subpath of |path|, so all components should match.
777 // If these CHECKs fail, look at the test that base is a parent of
778 // path at the top of this function.
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900779 DCHECK(ip != path_components.end());
780 DCHECK(*ip == *ib);
skerner@google.com93449ef2011-09-22 23:47:18 +0900781 }
782
783 FilePath current_path = base;
skerner@chromium.org80784142011-10-18 06:30:29 +0900784 if (!VerifySpecificPathControlledByUser(current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900785 return false;
786
787 for (; ip != path_components.end(); ++ip) {
788 current_path = current_path.Append(*ip);
skerner@chromium.org80784142011-10-18 06:30:29 +0900789 if (!VerifySpecificPathControlledByUser(
790 current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900791 return false;
792 }
793 return true;
794}
795
qsr@chromium.org4ab5de92012-07-09 23:40:39 +0900796#if defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900797bool VerifyPathControlledByAdmin(const FilePath& path) {
798 const unsigned kRootUid = 0;
799 const FilePath kFileSystemRoot("/");
800
801 // The name of the administrator group on mac os.
skerner@chromium.org80784142011-10-18 06:30:29 +0900802 const char* const kAdminGroupNames[] = {
803 "admin",
804 "wheel"
805 };
skerner@google.com93449ef2011-09-22 23:47:18 +0900806
807 // Reading the groups database may touch the file system.
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900808 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +0900809
skerner@chromium.org80784142011-10-18 06:30:29 +0900810 std::set<gid_t> allowed_group_ids;
811 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) {
812 struct group *group_record = getgrnam(kAdminGroupNames[i]);
813 if (!group_record) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900814 DPLOG(ERROR) << "Could not get the group ID of group \""
815 << kAdminGroupNames[i] << "\".";
skerner@chromium.org80784142011-10-18 06:30:29 +0900816 continue;
817 }
818
819 allowed_group_ids.insert(group_record->gr_gid);
skerner@google.com93449ef2011-09-22 23:47:18 +0900820 }
821
822 return VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +0900823 kFileSystemRoot, path, kRootUid, allowed_group_ids);
skerner@google.com93449ef2011-09-22 23:47:18 +0900824}
stuartmorgan@chromium.org925e0b72012-07-24 20:23:32 +0900825#endif // defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900826
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900827int GetMaximumPathComponentLength(const FilePath& path) {
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900828 ThreadRestrictions::AssertIOAllowed();
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900829 return pathconf(path.value().c_str(), _PC_NAME_MAX);
830}
831
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900832#if !defined(OS_ANDROID)
833// This is implemented in file_util_android.cc for that platform.
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900834bool GetShmemTempDir(bool executable, FilePath* path) {
835#if defined(OS_LINUX)
836 bool use_dev_shm = true;
837 if (executable) {
838 static const bool s_dev_shm_executable = DetermineDevShmExecutable();
839 use_dev_shm = s_dev_shm_executable;
840 }
841 if (use_dev_shm) {
842 *path = FilePath("/dev/shm");
843 return true;
844 }
845#endif
846 return GetTempDir(path);
847}
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900848#endif // !defined(OS_ANDROID)
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900849
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900850// -----------------------------------------------------------------------------
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900851
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900852namespace internal {
853
854bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
855 ThreadRestrictions::AssertIOAllowed();
856 // Windows compatibility: if to_path exists, from_path and to_path
857 // must be the same type, either both files, or both directories.
858 stat_wrapper_t to_file_info;
859 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
860 stat_wrapper_t from_file_info;
861 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
862 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
863 return false;
864 } else {
865 return false;
866 }
867 }
868
869 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
870 return true;
871
872 if (!CopyDirectory(from_path, to_path, true))
873 return false;
874
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900875 DeleteFile(from_path, true);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900876 return true;
877}
878
879#if !defined(OS_MACOSX)
880// Mac has its own implementation, this is for all other Posix systems.
881bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
882 ThreadRestrictions::AssertIOAllowed();
883 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY));
884 if (infile < 0)
885 return false;
886
887 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666));
888 if (outfile < 0) {
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900889 close(infile);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900890 return false;
891 }
892
893 const size_t kBufferSize = 32768;
894 std::vector<char> buffer(kBufferSize);
895 bool result = true;
896
897 while (result) {
898 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size()));
899 if (bytes_read < 0) {
900 result = false;
901 break;
902 }
903 if (bytes_read == 0)
904 break;
905 // Allow for partial writes
906 ssize_t bytes_written_per_read = 0;
907 do {
908 ssize_t bytes_written_partial = HANDLE_EINTR(write(
909 outfile,
910 &buffer[bytes_written_per_read],
911 bytes_read - bytes_written_per_read));
912 if (bytes_written_partial < 0) {
913 result = false;
914 break;
915 }
916 bytes_written_per_read += bytes_written_partial;
917 } while (bytes_written_per_read < bytes_read);
918 }
919
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900920 if (IGNORE_EINTR(close(infile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900921 result = false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900922 if (IGNORE_EINTR(close(outfile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900923 result = false;
924
925 return result;
926}
927#endif // !defined(OS_MACOSX)
928
929} // namespace internal
hidehikoce6251b2014-10-17 15:40:30 +0900930
931#endif // !defined(__native_client_nonsfi__)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900932} // namespace base