blob: 7e7dc052300fde54dc71f36681d783dc14776b17 [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
5#include "base/file_util.h"
6
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"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090036#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090037#include "base/memory/scoped_ptr.h"
38#include "base/memory/singleton.h"
nileshagrawal@chromium.org62001b92012-05-18 05:09:06 +090039#include "base/path_service.h"
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090040#include "base/posix/eintr_wrapper.h"
skerner@chromium.org80784142011-10-18 06:30:29 +090041#include "base/stl_util.h"
avi@chromium.org94bd5732013-06-11 22:36:37 +090042#include "base/strings/string_util.h"
43#include "base/strings/stringprintf.h"
brettw@chromium.org1f67a912013-02-08 04:18:03 +090044#include "base/strings/sys_string_conversions.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090045#include "base/strings/utf_string_conversions.h"
stevenjb@chromium.org861313b2013-09-28 04:28:24 +090046#include "base/sys_info.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090047#include "base/threading/thread_restrictions.h"
avi@chromium.orgb039e8b2013-06-28 09:49:07 +090048#include "base/time/time.h"
estade@chromium.org868ecbc2009-06-24 12:29:26 +090049
michaelbai@google.com2251c622011-06-22 07:34:50 +090050#if defined(OS_ANDROID)
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090051#include "base/android/content_uri_utils.h"
michaelbai@google.com2251c622011-06-22 07:34:50 +090052#include "base/os_compat_android.h"
53#endif
54
qsr@chromium.org4ab5de92012-07-09 23:40:39 +090055#if !defined(OS_IOS)
56#include <grp.h>
57#endif
58
brettw@chromium.org99b198e2013-04-12 14:17:15 +090059namespace base {
60
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090061namespace {
62
rsesek@chromium.orgdc818ac2012-07-20 07:24:02 +090063#if defined(OS_BSD) || defined(OS_MACOSX)
skerner@google.com93449ef2011-09-22 23:47:18 +090064typedef struct stat stat_wrapper_t;
65static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090066 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090067 return stat(path, sb);
68}
69static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090070 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090071 return lstat(path, sb);
72}
73#else
74typedef struct stat64 stat_wrapper_t;
75static 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}
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090083#if defined(OS_ANDROID)
84static int CallFstat(int fd, stat_wrapper_t *sb) {
85 ThreadRestrictions::AssertIOAllowed();
86 return fstat64(fd, sb);
87}
88#endif
skerner@google.com93449ef2011-09-22 23:47:18 +090089#endif
90
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090091// Helper for NormalizeFilePath(), defined below.
92bool RealPath(const FilePath& path, FilePath* real_path) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090093 ThreadRestrictions::AssertIOAllowed(); // For realpath().
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090094 FilePath::CharType buf[PATH_MAX];
95 if (!realpath(path.value().c_str(), buf))
96 return false;
97
98 *real_path = FilePath(buf);
99 return true;
100}
101
skerner@google.com93449ef2011-09-22 23:47:18 +0900102// Helper for VerifyPathControlledByUser.
103bool VerifySpecificPathControlledByUser(const FilePath& path,
104 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900105 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900106 stat_wrapper_t stat_info;
107 if (CallLstat(path.value().c_str(), &stat_info) != 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900108 DPLOG(ERROR) << "Failed to get information on path "
109 << path.value();
skerner@google.com93449ef2011-09-22 23:47:18 +0900110 return false;
111 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900112
skerner@google.com93449ef2011-09-22 23:47:18 +0900113 if (S_ISLNK(stat_info.st_mode)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900114 DLOG(ERROR) << "Path " << path.value()
skerner@google.com93449ef2011-09-22 23:47:18 +0900115 << " is a symbolic link.";
116 return false;
117 }
118
119 if (stat_info.st_uid != owner_uid) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900120 DLOG(ERROR) << "Path " << path.value()
121 << " is owned by the wrong user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900122 return false;
123 }
124
skerner@chromium.org80784142011-10-18 06:30:29 +0900125 if ((stat_info.st_mode & S_IWGRP) &&
126 !ContainsKey(group_gids, stat_info.st_gid)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900127 DLOG(ERROR) << "Path " << path.value()
128 << " is writable by an unprivileged group.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900129 return false;
130 }
131
132 if (stat_info.st_mode & S_IWOTH) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900133 DLOG(ERROR) << "Path " << path.value()
134 << " is writable by any user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900135 return false;
136 }
137
138 return true;
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900139}
skerner@google.com93449ef2011-09-22 23:47:18 +0900140
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900141std::string TempFileName() {
142#if defined(OS_MACOSX)
143 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
144#endif
145
146#if defined(GOOGLE_CHROME_BUILD)
147 return std::string(".com.google.Chrome.XXXXXX");
148#else
149 return std::string(".org.chromium.Chromium.XXXXXX");
150#endif
151}
152
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900153// Creates and opens a temporary file in |directory|, returning the
154// file descriptor. |path| is set to the temporary file path.
155// This function does NOT unlink() the file.
156int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
157 ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
158 *path = directory.Append(base::TempFileName());
159 const std::string& tmpdir_string = path->value();
160 // this should be OK since mkstemp just replaces characters in place
161 char* buffer = const_cast<char*>(tmpdir_string.c_str());
162
163 return HANDLE_EINTR(mkstemp(buffer));
164}
165
166#if defined(OS_LINUX)
167// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
168// This depends on the mount options used for /dev/shm, which vary among
169// different Linux distributions and possibly local configuration. It also
170// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
171// but its kernel allows mprotect with PROT_EXEC anyway.
172bool DetermineDevShmExecutable() {
173 bool result = false;
174 FilePath path;
175 int fd = CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path);
176 if (fd >= 0) {
177 file_util::ScopedFD shm_fd_closer(&fd);
178 DeleteFile(path, false);
179 long sysconf_result = sysconf(_SC_PAGESIZE);
180 CHECK_GE(sysconf_result, 0);
181 size_t pagesize = static_cast<size_t>(sysconf_result);
182 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
183 void *mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
184 if (mapping != MAP_FAILED) {
185 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
186 result = true;
187 munmap(mapping, pagesize);
188 }
189 }
190 return result;
191}
192#endif // defined(OS_LINUX)
193
skerner@google.com93449ef2011-09-22 23:47:18 +0900194} // namespace
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900195
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900196FilePath MakeAbsoluteFilePath(const FilePath& input) {
197 ThreadRestrictions::AssertIOAllowed();
198 char full_path[PATH_MAX];
199 if (realpath(input.value().c_str(), full_path) == NULL)
200 return FilePath();
201 return FilePath(full_path);
mark@chromium.org13aa8aa2011-04-22 13:15:13 +0900202}
mark@chromium.org8ca0d272008-09-12 02:36:23 +0900203
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900204// TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*"
205// which works both with and without the recursive flag. I'm not sure we need
206// that functionality. If not, remove from file_util_win.cc, otherwise add it
207// here.
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900208bool DeleteFile(const FilePath& path, bool recursive) {
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900209 ThreadRestrictions::AssertIOAllowed();
evanm@google.com874d1672008-10-31 08:54:04 +0900210 const char* path_str = path.value().c_str();
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900211 stat_wrapper_t file_info;
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900212 int test = CallLstat(path_str, &file_info);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900213 if (test != 0) {
214 // The Windows version defines this condition as success.
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900215 bool ret = (errno == ENOENT || errno == ENOTDIR);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900216 return ret;
217 }
218 if (!S_ISDIR(file_info.st_mode))
evanm@google.com874d1672008-10-31 08:54:04 +0900219 return (unlink(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900220 if (!recursive)
evanm@google.com874d1672008-10-31 08:54:04 +0900221 return (rmdir(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900222
223 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900224 std::stack<std::string> directories;
225 directories.push(path.value());
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900226 FileEnumerator traversal(path, true,
227 FileEnumerator::FILES | FileEnumerator::DIRECTORIES |
228 FileEnumerator::SHOW_SYM_LINKS);
thestig@chromium.org3217c822009-08-07 06:23:07 +0900229 for (FilePath current = traversal.Next(); success && !current.empty();
230 current = traversal.Next()) {
brettw@chromium.org56946722013-06-08 13:53:36 +0900231 if (traversal.GetInfo().IsDirectory())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900232 directories.push(current.value());
233 else
234 success = (unlink(current.value().c_str()) == 0);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900235 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900236
237 while (success && !directories.empty()) {
238 FilePath dir = FilePath(directories.top());
239 directories.pop();
240 success = (rmdir(dir.value().c_str()) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900241 }
242 return success;
243}
244
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900245bool ReplaceFile(const FilePath& from_path,
246 const FilePath& to_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900247 File::Error* error) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900248 ThreadRestrictions::AssertIOAllowed();
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900249 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
250 return true;
251 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900252 *error = File::OSErrorToFileError(errno);
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900253 return false;
phajdan.jr@chromium.orgd86bea02009-05-20 02:21:07 +0900254}
255
dbeam@chromium.org85aa52a2013-05-08 14:46:20 +0900256bool CopyDirectory(const FilePath& from_path,
257 const FilePath& to_path,
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900258 bool recursive) {
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900259 ThreadRestrictions::AssertIOAllowed();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900260 // Some old callers of CopyDirectory want it to support wildcards.
261 // After some discussion, we decided to fix those callers.
262 // Break loudly here if anyone tries to do this.
evanm@google.com874d1672008-10-31 08:54:04 +0900263 DCHECK(to_path.value().find('*') == std::string::npos);
264 DCHECK(from_path.value().find('*') == std::string::npos);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900265
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900266 if (from_path.value().size() >= PATH_MAX) {
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900267 return false;
268 }
269
thestig@chromium.org3217c822009-08-07 06:23:07 +0900270 // This function does not properly handle destinations within the source
271 FilePath real_to_path = to_path;
brettw@chromium.org10b64122013-07-12 02:36:07 +0900272 if (PathExists(real_to_path)) {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900273 real_to_path = MakeAbsoluteFilePath(real_to_path);
274 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900275 return false;
276 } else {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900277 real_to_path = MakeAbsoluteFilePath(real_to_path.DirName());
278 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900279 return false;
280 }
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900281 FilePath real_from_path = MakeAbsoluteFilePath(from_path);
282 if (real_from_path.empty())
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900283 return false;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900284 if (real_to_path.value().size() >= real_from_path.value().size() &&
285 real_to_path.value().compare(0, real_from_path.value().size(),
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900286 real_from_path.value()) == 0) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900287 return false;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900288 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900289
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900290 int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900291 if (recursive)
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900292 traverse_type |= FileEnumerator::DIRECTORIES;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900293 FileEnumerator traversal(from_path, recursive, traverse_type);
294
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900295 // We have to mimic windows behavior here. |to_path| may not exist yet,
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900296 // start the loop with |to_path|.
brettw@chromium.org56946722013-06-08 13:53:36 +0900297 struct stat from_stat;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900298 FilePath current = from_path;
brettw@chromium.org56946722013-06-08 13:53:36 +0900299 if (stat(from_path.value().c_str(), &from_stat) < 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900300 DLOG(ERROR) << "CopyDirectory() couldn't stat source directory: "
301 << from_path.value() << " errno = " << errno;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900302 return false;
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900303 }
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900304 struct stat to_path_stat;
305 FilePath from_path_base = from_path;
306 if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 &&
307 S_ISDIR(to_path_stat.st_mode)) {
308 // If the destination already exists and is a directory, then the
309 // top level of source needs to be copied.
310 from_path_base = from_path.DirName();
311 }
312
313 // The Windows version of this function assumes that non-recursive calls
314 // will always have a directory for from_path.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900315 // TODO(maruel): This is not necessary anymore.
brettw@chromium.org56946722013-06-08 13:53:36 +0900316 DCHECK(recursive || S_ISDIR(from_stat.st_mode));
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900317
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900318 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900319 while (success && !current.empty()) {
aedla@chromium.orgfef1a202013-01-30 20:38:02 +0900320 // current is the source path, including from_path, so append
321 // the suffix after from_path to to_path to create the target_path.
322 FilePath target_path(to_path);
323 if (from_path_base != current) {
324 if (!from_path_base.AppendRelativePath(current, &target_path)) {
325 success = false;
326 break;
327 }
phajdan.jr@chromium.orgecf50752009-01-14 03:57:46 +0900328 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900329
brettw@chromium.org56946722013-06-08 13:53:36 +0900330 if (S_ISDIR(from_stat.st_mode)) {
331 if (mkdir(target_path.value().c_str(), from_stat.st_mode & 01777) != 0 &&
thestig@chromium.org3217c822009-08-07 06:23:07 +0900332 errno != EEXIST) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900333 DLOG(ERROR) << "CopyDirectory() couldn't create directory: "
334 << target_path.value() << " errno = " << errno;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900335 success = false;
336 }
brettw@chromium.org56946722013-06-08 13:53:36 +0900337 } else if (S_ISREG(from_stat.st_mode)) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900338 if (!CopyFile(current, target_path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900339 DLOG(ERROR) << "CopyDirectory() couldn't create file: "
340 << target_path.value();
thestig@chromium.org3217c822009-08-07 06:23:07 +0900341 success = false;
342 }
343 } else {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900344 DLOG(WARNING) << "CopyDirectory() skipping non-regular file: "
345 << current.value();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900346 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900347
thestig@chromium.org3217c822009-08-07 06:23:07 +0900348 current = traversal.Next();
brettw@chromium.org56946722013-06-08 13:53:36 +0900349 if (!current.empty())
350 from_stat = traversal.GetInfo().stat();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900351 }
352
thestig@chromium.org3217c822009-08-07 06:23:07 +0900353 return success;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900354}
355
brettw@chromium.org10b64122013-07-12 02:36:07 +0900356bool PathExists(const FilePath& path) {
357 ThreadRestrictions::AssertIOAllowed();
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +0900358#if defined(OS_ANDROID)
359 if (path.IsContentUri()) {
360 return ContentUriExists(path);
361 }
362#endif
brettw@chromium.org10b64122013-07-12 02:36:07 +0900363 return access(path.value().c_str(), F_OK) == 0;
364}
365
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900366bool PathIsWritable(const FilePath& path) {
367 ThreadRestrictions::AssertIOAllowed();
368 return access(path.value().c_str(), W_OK) == 0;
369}
370
371bool DirectoryExists(const FilePath& path) {
372 ThreadRestrictions::AssertIOAllowed();
373 stat_wrapper_t file_info;
374 if (CallStat(path.value().c_str(), &file_info) == 0)
375 return S_ISDIR(file_info.st_mode);
376 return false;
377}
378
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900379bool ReadFromFD(int fd, char* buffer, size_t bytes) {
380 size_t total_read = 0;
381 while (total_read < bytes) {
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900382 ssize_t bytes_read =
383 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read));
384 if (bytes_read <= 0)
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900385 break;
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900386 total_read += bytes_read;
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900387 }
388 return total_read == bytes;
389}
390
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
465#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
466// This is implemented in file_util_mac.mm and file_util_android.cc for those
467// platforms.
468bool GetShmemTempDir(bool executable, FilePath* path) {
469#if defined(OS_LINUX)
470 bool use_dev_shm = true;
471 if (executable) {
472 static const bool s_dev_shm_executable = DetermineDevShmExecutable();
473 use_dev_shm = s_dev_shm_executable;
474 }
475 if (use_dev_shm) {
476 *path = FilePath("/dev/shm");
477 return true;
478 }
479#endif
480 return GetTempDir(path);
481}
482#endif // !defined(OS_MACOSX) && !defined(OS_ANDROID)
483
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900484#if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900485FilePath GetHomeDir() {
486#if defined(OS_CHROMEOS)
487 if (SysInfo::IsRunningOnChromeOS())
488 return FilePath("/home/chronos/user");
489#endif
490
491 const char* home_dir = getenv("HOME");
492 if (home_dir && home_dir[0])
493 return FilePath(home_dir);
494
495#if defined(OS_ANDROID)
496 DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
497#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900498 // g_get_home_dir calls getpwent, which can fall through to LDAP calls so
499 // this may do I/O. However, it should be rare that $HOME is not defined and
500 // this is typically called from the path service which has no threading
501 // restrictions. The path service will cache the result which limits the
502 // badness of blocking on I/O. As a result, we don't have a thread
503 // restriction here.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900504 home_dir = g_get_home_dir();
505 if (home_dir && home_dir[0])
506 return FilePath(home_dir);
507#endif
508
509 FilePath rv;
510 if (GetTempDir(&rv))
511 return rv;
512
513 // Last resort.
514 return FilePath("/tmp");
515}
516#endif // !defined(OS_MACOSX)
517
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900518bool CreateTemporaryFile(FilePath* path) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900519 ThreadRestrictions::AssertIOAllowed(); // For call to close().
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900520 FilePath directory;
521 if (!GetTempDir(&directory))
522 return false;
523 int fd = CreateAndOpenFdForTemporaryFile(directory, path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900524 if (fd < 0)
525 return false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900526 close(fd);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900527 return true;
528}
529
mcgrathr@chromium.org569a4232011-12-07 03:07:05 +0900530FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900531 FilePath directory;
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900532 if (!GetShmemTempDir(executable, &directory))
evan@chromium.org2abe0b42010-06-11 07:56:23 +0900533 return NULL;
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900534
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900535 return CreateAndOpenTemporaryFileInDir(directory, path);
536}
537
538FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
539 int fd = CreateAndOpenFdForTemporaryFile(dir, path);
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900540 if (fd < 0)
541 return NULL;
542
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900543 FILE* file = fdopen(fd, "a+");
544 if (!file)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900545 close(fd);
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900546 return file;
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900547}
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900548
549bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900550 ThreadRestrictions::AssertIOAllowed(); // For call to close().
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900551 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file);
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900552 return ((fd >= 0) && !IGNORE_EINTR(close(fd)));
jcampan@chromium.orgbf29e602008-10-11 03:50:32 +0900553}
554
skerner@chromium.orge4432392010-05-01 02:00:09 +0900555static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
556 const FilePath::StringType& name_tmpl,
557 FilePath* new_dir) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900558 ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900559 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
560 << "Directory name template must contain \"XXXXXX\".";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900561
562 FilePath sub_dir = base_dir.Append(name_tmpl);
563 std::string sub_dir_string = sub_dir.value();
564
565 // this should be OK since mkdtemp just replaces characters in place
566 char* buffer = const_cast<char*>(sub_dir_string.c_str());
567 char* dtemp = mkdtemp(buffer);
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900568 if (!dtemp) {
569 DPLOG(ERROR) << "mkdtemp";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900570 return false;
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900571 }
skerner@chromium.orge4432392010-05-01 02:00:09 +0900572 *new_dir = FilePath(dtemp);
573 return true;
574}
575
576bool CreateTemporaryDirInDir(const FilePath& base_dir,
577 const FilePath::StringType& prefix,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +0900578 FilePath* new_dir) {
skerner@chromium.orge4432392010-05-01 02:00:09 +0900579 FilePath::StringType mkdtemp_template = prefix;
580 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX"));
581 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir);
582}
583
erikkay@google.comcce83822008-12-24 05:20:10 +0900584bool CreateNewTempDirectory(const FilePath::StringType& prefix,
585 FilePath* new_temp_path) {
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900586 FilePath tmpdir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900587 if (!GetTempDir(&tmpdir))
588 return false;
skerner@chromium.orge4432392010-05-01 02:00:09 +0900589
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900590 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900591}
592
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900593bool CreateDirectoryAndGetError(const FilePath& full_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900594 File::Error* error) {
brettw@chromium.org738669a2013-12-04 05:08:54 +0900595 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
evanm@google.com874d1672008-10-31 08:54:04 +0900596 std::vector<FilePath> subpaths;
597
598 // Collect a list of all parent directories.
599 FilePath last_path = full_path;
600 subpaths.push_back(full_path);
601 for (FilePath path = full_path.DirName();
602 path.value() != last_path.value(); path = path.DirName()) {
603 subpaths.push_back(path);
604 last_path = path;
605 }
606
607 // Iterate through the parents and create the missing ones.
608 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
609 i != subpaths.rend(); ++i) {
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900610 if (DirectoryExists(*i))
611 continue;
612 if (mkdir(i->value().c_str(), 0700) == 0)
613 continue;
614 // Mkdir failed, but it might have failed with EEXIST, or some other error
615 // due to the the directory appearing out of thin air. This can occur if
616 // two processes are trying to create the same file system tree at the same
617 // time. Check to see if it exists and make sure it is a directory.
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900618 int saved_errno = errno;
619 if (!DirectoryExists(*i)) {
620 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900621 *error = File::OSErrorToFileError(saved_errno);
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900622 return false;
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900623 }
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900624 }
625 return true;
626}
627
brettw@chromium.org70684242013-12-05 03:22:49 +0900628bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
629 FilePath real_path_result;
630 if (!RealPath(path, &real_path_result))
631 return false;
632
633 // To be consistant with windows, fail if |real_path_result| is a
634 // directory.
635 stat_wrapper_t file_info;
636 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
637 S_ISDIR(file_info.st_mode))
638 return false;
639
640 *normalized_path = real_path_result;
641 return true;
642}
643
brettw@chromium.orga9154032013-12-05 05:56:49 +0900644// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
645// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
646bool IsLink(const FilePath& file_path) {
647 stat_wrapper_t st;
648 // If we can't lstat the file, it's safe to assume that the file won't at
649 // least be a 'followable' link.
650 if (CallLstat(file_path.value().c_str(), &st) != 0)
651 return false;
652
653 if (S_ISLNK(st.st_mode))
654 return true;
655 else
656 return false;
657}
658
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900659bool GetFileInfo(const FilePath& file_path, File::Info* results) {
brettw@chromium.orga9154032013-12-05 05:56:49 +0900660 stat_wrapper_t file_info;
661#if defined(OS_ANDROID)
662 if (file_path.IsContentUri()) {
663 int fd = OpenContentUriForRead(file_path);
664 if (fd < 0)
665 return false;
666 file_util::ScopedFD scoped_fd(&fd);
667 if (CallFstat(fd, &file_info) != 0)
668 return false;
669 } else {
670#endif // defined(OS_ANDROID)
671 if (CallStat(file_path.value().c_str(), &file_info) != 0)
672 return false;
673#if defined(OS_ANDROID)
674 }
675#endif // defined(OS_ANDROID)
676 results->is_directory = S_ISDIR(file_info.st_mode);
677 results->size = file_info.st_size;
r.c.ladan@gmail.com733a5022014-03-11 13:11:02 +0900678#if defined(OS_MACOSX) || (defined(OS_FREEBSD) && __FreeBSD_version < 900000)
brettw@chromium.orga9154032013-12-05 05:56:49 +0900679 results->last_modified = Time::FromTimeSpec(file_info.st_mtimespec);
680 results->last_accessed = Time::FromTimeSpec(file_info.st_atimespec);
681 results->creation_time = Time::FromTimeSpec(file_info.st_ctimespec);
682#elif defined(OS_ANDROID)
683 results->last_modified = Time::FromTimeT(file_info.st_mtime);
684 results->last_accessed = Time::FromTimeT(file_info.st_atime);
685 results->creation_time = Time::FromTimeT(file_info.st_ctime);
686#else
687 results->last_modified = Time::FromTimeSpec(file_info.st_mtim);
688 results->last_accessed = Time::FromTimeSpec(file_info.st_atim);
689 results->creation_time = Time::FromTimeSpec(file_info.st_ctim);
690#endif
691 return true;
692}
693
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900694FILE* OpenFile(const FilePath& filename, const char* mode) {
695 ThreadRestrictions::AssertIOAllowed();
696 FILE* result = NULL;
697 do {
698 result = fopen(filename.value().c_str(), mode);
699 } while (!result && errno == EINTR);
700 return result;
701}
702
703int ReadFile(const FilePath& filename, char* data, int size) {
704 ThreadRestrictions::AssertIOAllowed();
705 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY));
706 if (fd < 0)
707 return -1;
708
709 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size));
710 if (int ret = IGNORE_EINTR(close(fd)) < 0)
711 return ret;
712 return bytes_read;
713}
714
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900715int WriteFile(const FilePath& filename, const char* data, int size) {
716 ThreadRestrictions::AssertIOAllowed();
717 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
718 if (fd < 0)
719 return -1;
720
721 int bytes_written = WriteFileDescriptor(fd, data, size);
722 if (int ret = IGNORE_EINTR(close(fd)) < 0)
723 return ret;
724 return bytes_written;
725}
726
727int WriteFileDescriptor(const int fd, const char* data, int size) {
728 // Allow for partial writes.
729 ssize_t bytes_written_total = 0;
730 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
731 bytes_written_total += bytes_written_partial) {
732 bytes_written_partial =
733 HANDLE_EINTR(write(fd, data + bytes_written_total,
734 size - bytes_written_total));
735 if (bytes_written_partial < 0)
736 return -1;
737 }
738
739 return bytes_written_total;
740}
741
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900742int AppendToFile(const FilePath& filename, const char* data, int size) {
743 ThreadRestrictions::AssertIOAllowed();
744 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
745 if (fd < 0)
746 return -1;
747
748 int bytes_written = WriteFileDescriptor(fd, data, size);
749 if (int ret = IGNORE_EINTR(close(fd)) < 0)
750 return ret;
751 return bytes_written;
752}
753
754// Gets the current working directory for the process.
755bool GetCurrentDirectory(FilePath* dir) {
756 // getcwd can return ENOENT, which implies it checks against the disk.
757 ThreadRestrictions::AssertIOAllowed();
758
759 char system_buffer[PATH_MAX] = "";
760 if (!getcwd(system_buffer, sizeof(system_buffer))) {
761 NOTREACHED();
762 return false;
763 }
764 *dir = FilePath(system_buffer);
765 return true;
766}
767
768// Sets the current working directory for the process.
769bool SetCurrentDirectory(const FilePath& path) {
770 ThreadRestrictions::AssertIOAllowed();
771 int ret = chdir(path.value().c_str());
772 return !ret;
773}
774
skerner@google.com93449ef2011-09-22 23:47:18 +0900775bool VerifyPathControlledByUser(const FilePath& base,
776 const FilePath& path,
777 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900778 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900779 if (base != path && !base.IsParent(path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900780 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \""
781 << base.value() << "\", path = \"" << path.value() << "\"";
skerner@google.com93449ef2011-09-22 23:47:18 +0900782 return false;
783 }
784
785 std::vector<FilePath::StringType> base_components;
786 std::vector<FilePath::StringType> path_components;
787
788 base.GetComponents(&base_components);
789 path.GetComponents(&path_components);
790
791 std::vector<FilePath::StringType>::const_iterator ib, ip;
792 for (ib = base_components.begin(), ip = path_components.begin();
793 ib != base_components.end(); ++ib, ++ip) {
794 // |base| must be a subpath of |path|, so all components should match.
795 // If these CHECKs fail, look at the test that base is a parent of
796 // path at the top of this function.
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900797 DCHECK(ip != path_components.end());
798 DCHECK(*ip == *ib);
skerner@google.com93449ef2011-09-22 23:47:18 +0900799 }
800
801 FilePath current_path = base;
skerner@chromium.org80784142011-10-18 06:30:29 +0900802 if (!VerifySpecificPathControlledByUser(current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900803 return false;
804
805 for (; ip != path_components.end(); ++ip) {
806 current_path = current_path.Append(*ip);
skerner@chromium.org80784142011-10-18 06:30:29 +0900807 if (!VerifySpecificPathControlledByUser(
808 current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900809 return false;
810 }
811 return true;
812}
813
qsr@chromium.org4ab5de92012-07-09 23:40:39 +0900814#if defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900815bool VerifyPathControlledByAdmin(const FilePath& path) {
816 const unsigned kRootUid = 0;
817 const FilePath kFileSystemRoot("/");
818
819 // The name of the administrator group on mac os.
skerner@chromium.org80784142011-10-18 06:30:29 +0900820 const char* const kAdminGroupNames[] = {
821 "admin",
822 "wheel"
823 };
skerner@google.com93449ef2011-09-22 23:47:18 +0900824
825 // Reading the groups database may touch the file system.
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900826 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +0900827
skerner@chromium.org80784142011-10-18 06:30:29 +0900828 std::set<gid_t> allowed_group_ids;
829 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) {
830 struct group *group_record = getgrnam(kAdminGroupNames[i]);
831 if (!group_record) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900832 DPLOG(ERROR) << "Could not get the group ID of group \""
833 << kAdminGroupNames[i] << "\".";
skerner@chromium.org80784142011-10-18 06:30:29 +0900834 continue;
835 }
836
837 allowed_group_ids.insert(group_record->gr_gid);
skerner@google.com93449ef2011-09-22 23:47:18 +0900838 }
839
840 return VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +0900841 kFileSystemRoot, path, kRootUid, allowed_group_ids);
skerner@google.com93449ef2011-09-22 23:47:18 +0900842}
stuartmorgan@chromium.org925e0b72012-07-24 20:23:32 +0900843#endif // defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900844
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900845int GetMaximumPathComponentLength(const FilePath& path) {
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900846 ThreadRestrictions::AssertIOAllowed();
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900847 return pathconf(path.value().c_str(), _PC_NAME_MAX);
848}
849
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
930} // namespace base