blob: 07c21d1430f94527d4c3faf87078913bb265ab2e [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
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090062namespace {
63
rvargas@chromium.org527ea312014-04-05 11:39:18 +090064#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090065static 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}
rvargas@chromium.org527ea312014-04-05 11:39:18 +090073#else // defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
skerner@google.com93449ef2011-09-22 23:47:18 +090074static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090075 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090076 return stat64(path, sb);
77}
78static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090079 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090080 return lstat64(path, sb);
81}
thestig@chromium.org5b29c252014-04-29 20:21:50 +090082#endif // !(defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL))
skerner@google.com93449ef2011-09-22 23:47:18 +090083
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090084// Helper for NormalizeFilePath(), defined below.
85bool RealPath(const FilePath& path, FilePath* real_path) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090086 ThreadRestrictions::AssertIOAllowed(); // For realpath().
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090087 FilePath::CharType buf[PATH_MAX];
88 if (!realpath(path.value().c_str(), buf))
89 return false;
90
91 *real_path = FilePath(buf);
92 return true;
93}
94
skerner@google.com93449ef2011-09-22 23:47:18 +090095// Helper for VerifyPathControlledByUser.
96bool VerifySpecificPathControlledByUser(const FilePath& path,
97 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +090098 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +090099 stat_wrapper_t stat_info;
100 if (CallLstat(path.value().c_str(), &stat_info) != 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900101 DPLOG(ERROR) << "Failed to get information on path "
102 << path.value();
skerner@google.com93449ef2011-09-22 23:47:18 +0900103 return false;
104 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900105
skerner@google.com93449ef2011-09-22 23:47:18 +0900106 if (S_ISLNK(stat_info.st_mode)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900107 DLOG(ERROR) << "Path " << path.value()
skerner@google.com93449ef2011-09-22 23:47:18 +0900108 << " is a symbolic link.";
109 return false;
110 }
111
112 if (stat_info.st_uid != owner_uid) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900113 DLOG(ERROR) << "Path " << path.value()
114 << " is owned by the wrong user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900115 return false;
116 }
117
skerner@chromium.org80784142011-10-18 06:30:29 +0900118 if ((stat_info.st_mode & S_IWGRP) &&
119 !ContainsKey(group_gids, stat_info.st_gid)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900120 DLOG(ERROR) << "Path " << path.value()
121 << " is writable by an unprivileged group.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900122 return false;
123 }
124
125 if (stat_info.st_mode & S_IWOTH) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900126 DLOG(ERROR) << "Path " << path.value()
127 << " is writable by any user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900128 return false;
129 }
130
131 return true;
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900132}
skerner@google.com93449ef2011-09-22 23:47:18 +0900133
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900134std::string TempFileName() {
135#if defined(OS_MACOSX)
136 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
137#endif
138
139#if defined(GOOGLE_CHROME_BUILD)
140 return std::string(".com.google.Chrome.XXXXXX");
141#else
142 return std::string(".org.chromium.Chromium.XXXXXX");
143#endif
144}
145
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900146// Creates and opens a temporary file in |directory|, returning the
147// file descriptor. |path| is set to the temporary file path.
148// This function does NOT unlink() the file.
149int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
150 ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
151 *path = directory.Append(base::TempFileName());
152 const std::string& tmpdir_string = path->value();
153 // this should be OK since mkstemp just replaces characters in place
154 char* buffer = const_cast<char*>(tmpdir_string.c_str());
155
156 return HANDLE_EINTR(mkstemp(buffer));
157}
158
159#if defined(OS_LINUX)
160// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
161// This depends on the mount options used for /dev/shm, which vary among
162// different Linux distributions and possibly local configuration. It also
163// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
164// but its kernel allows mprotect with PROT_EXEC anyway.
165bool DetermineDevShmExecutable() {
166 bool result = false;
167 FilePath path;
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900168
169 ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path));
170 if (fd.is_valid()) {
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900171 DeleteFile(path, false);
172 long sysconf_result = sysconf(_SC_PAGESIZE);
173 CHECK_GE(sysconf_result, 0);
174 size_t pagesize = static_cast<size_t>(sysconf_result);
175 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
thestig@chromium.org5b29c252014-04-29 20:21:50 +0900176 void* mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0);
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900177 if (mapping != MAP_FAILED) {
178 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
179 result = true;
180 munmap(mapping, pagesize);
181 }
182 }
183 return result;
184}
185#endif // defined(OS_LINUX)
186
skerner@google.com93449ef2011-09-22 23:47:18 +0900187} // namespace
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900188
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900189FilePath MakeAbsoluteFilePath(const FilePath& input) {
190 ThreadRestrictions::AssertIOAllowed();
191 char full_path[PATH_MAX];
192 if (realpath(input.value().c_str(), full_path) == NULL)
193 return FilePath();
194 return FilePath(full_path);
mark@chromium.org13aa8aa2011-04-22 13:15:13 +0900195}
mark@chromium.org8ca0d272008-09-12 02:36:23 +0900196
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900197// TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*"
198// which works both with and without the recursive flag. I'm not sure we need
199// that functionality. If not, remove from file_util_win.cc, otherwise add it
200// here.
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900201bool DeleteFile(const FilePath& path, bool recursive) {
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900202 ThreadRestrictions::AssertIOAllowed();
evanm@google.com874d1672008-10-31 08:54:04 +0900203 const char* path_str = path.value().c_str();
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900204 stat_wrapper_t file_info;
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900205 int test = CallLstat(path_str, &file_info);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900206 if (test != 0) {
207 // The Windows version defines this condition as success.
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900208 bool ret = (errno == ENOENT || errno == ENOTDIR);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900209 return ret;
210 }
211 if (!S_ISDIR(file_info.st_mode))
evanm@google.com874d1672008-10-31 08:54:04 +0900212 return (unlink(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900213 if (!recursive)
evanm@google.com874d1672008-10-31 08:54:04 +0900214 return (rmdir(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900215
216 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900217 std::stack<std::string> directories;
218 directories.push(path.value());
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900219 FileEnumerator traversal(path, true,
220 FileEnumerator::FILES | FileEnumerator::DIRECTORIES |
221 FileEnumerator::SHOW_SYM_LINKS);
thestig@chromium.org3217c822009-08-07 06:23:07 +0900222 for (FilePath current = traversal.Next(); success && !current.empty();
223 current = traversal.Next()) {
brettw@chromium.org56946722013-06-08 13:53:36 +0900224 if (traversal.GetInfo().IsDirectory())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900225 directories.push(current.value());
226 else
227 success = (unlink(current.value().c_str()) == 0);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900228 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900229
230 while (success && !directories.empty()) {
231 FilePath dir = FilePath(directories.top());
232 directories.pop();
233 success = (rmdir(dir.value().c_str()) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900234 }
235 return success;
236}
237
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900238bool ReplaceFile(const FilePath& from_path,
239 const FilePath& to_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900240 File::Error* error) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900241 ThreadRestrictions::AssertIOAllowed();
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900242 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
243 return true;
244 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900245 *error = File::OSErrorToFileError(errno);
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900246 return false;
phajdan.jr@chromium.orgd86bea02009-05-20 02:21:07 +0900247}
248
dbeam@chromium.org85aa52a2013-05-08 14:46:20 +0900249bool CopyDirectory(const FilePath& from_path,
250 const FilePath& to_path,
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900251 bool recursive) {
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900252 ThreadRestrictions::AssertIOAllowed();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900253 // Some old callers of CopyDirectory want it to support wildcards.
254 // After some discussion, we decided to fix those callers.
255 // Break loudly here if anyone tries to do this.
evanm@google.com874d1672008-10-31 08:54:04 +0900256 DCHECK(to_path.value().find('*') == std::string::npos);
257 DCHECK(from_path.value().find('*') == std::string::npos);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900258
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900259 if (from_path.value().size() >= PATH_MAX) {
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900260 return false;
261 }
262
thestig@chromium.org3217c822009-08-07 06:23:07 +0900263 // This function does not properly handle destinations within the source
264 FilePath real_to_path = to_path;
brettw@chromium.org10b64122013-07-12 02:36:07 +0900265 if (PathExists(real_to_path)) {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900266 real_to_path = MakeAbsoluteFilePath(real_to_path);
267 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900268 return false;
269 } else {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900270 real_to_path = MakeAbsoluteFilePath(real_to_path.DirName());
271 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900272 return false;
273 }
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900274 FilePath real_from_path = MakeAbsoluteFilePath(from_path);
275 if (real_from_path.empty())
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900276 return false;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900277 if (real_to_path.value().size() >= real_from_path.value().size() &&
278 real_to_path.value().compare(0, real_from_path.value().size(),
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900279 real_from_path.value()) == 0) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900280 return false;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900281 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900282
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900283 int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900284 if (recursive)
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900285 traverse_type |= FileEnumerator::DIRECTORIES;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900286 FileEnumerator traversal(from_path, recursive, traverse_type);
287
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900288 // We have to mimic windows behavior here. |to_path| may not exist yet,
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900289 // start the loop with |to_path|.
brettw@chromium.org56946722013-06-08 13:53:36 +0900290 struct stat from_stat;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900291 FilePath current = from_path;
brettw@chromium.org56946722013-06-08 13:53:36 +0900292 if (stat(from_path.value().c_str(), &from_stat) < 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900293 DLOG(ERROR) << "CopyDirectory() couldn't stat source directory: "
294 << from_path.value() << " errno = " << errno;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900295 return false;
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900296 }
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900297 struct stat to_path_stat;
298 FilePath from_path_base = from_path;
299 if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 &&
300 S_ISDIR(to_path_stat.st_mode)) {
301 // If the destination already exists and is a directory, then the
302 // top level of source needs to be copied.
303 from_path_base = from_path.DirName();
304 }
305
306 // The Windows version of this function assumes that non-recursive calls
307 // will always have a directory for from_path.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900308 // TODO(maruel): This is not necessary anymore.
brettw@chromium.org56946722013-06-08 13:53:36 +0900309 DCHECK(recursive || S_ISDIR(from_stat.st_mode));
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900310
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900311 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900312 while (success && !current.empty()) {
aedla@chromium.orgfef1a202013-01-30 20:38:02 +0900313 // current is the source path, including from_path, so append
314 // the suffix after from_path to to_path to create the target_path.
315 FilePath target_path(to_path);
316 if (from_path_base != current) {
317 if (!from_path_base.AppendRelativePath(current, &target_path)) {
318 success = false;
319 break;
320 }
phajdan.jr@chromium.orgecf50752009-01-14 03:57:46 +0900321 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900322
brettw@chromium.org56946722013-06-08 13:53:36 +0900323 if (S_ISDIR(from_stat.st_mode)) {
324 if (mkdir(target_path.value().c_str(), from_stat.st_mode & 01777) != 0 &&
thestig@chromium.org3217c822009-08-07 06:23:07 +0900325 errno != EEXIST) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900326 DLOG(ERROR) << "CopyDirectory() couldn't create directory: "
327 << target_path.value() << " errno = " << errno;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900328 success = false;
329 }
brettw@chromium.org56946722013-06-08 13:53:36 +0900330 } else if (S_ISREG(from_stat.st_mode)) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900331 if (!CopyFile(current, target_path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900332 DLOG(ERROR) << "CopyDirectory() couldn't create file: "
333 << target_path.value();
thestig@chromium.org3217c822009-08-07 06:23:07 +0900334 success = false;
335 }
336 } else {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900337 DLOG(WARNING) << "CopyDirectory() skipping non-regular file: "
338 << current.value();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900339 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900340
thestig@chromium.org3217c822009-08-07 06:23:07 +0900341 current = traversal.Next();
brettw@chromium.org56946722013-06-08 13:53:36 +0900342 if (!current.empty())
343 from_stat = traversal.GetInfo().stat();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900344 }
345
thestig@chromium.org3217c822009-08-07 06:23:07 +0900346 return success;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900347}
348
brettw@chromium.org10b64122013-07-12 02:36:07 +0900349bool PathExists(const FilePath& path) {
350 ThreadRestrictions::AssertIOAllowed();
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +0900351#if defined(OS_ANDROID)
352 if (path.IsContentUri()) {
353 return ContentUriExists(path);
354 }
355#endif
brettw@chromium.org10b64122013-07-12 02:36:07 +0900356 return access(path.value().c_str(), F_OK) == 0;
357}
358
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900359bool PathIsWritable(const FilePath& path) {
360 ThreadRestrictions::AssertIOAllowed();
361 return access(path.value().c_str(), W_OK) == 0;
362}
363
364bool DirectoryExists(const FilePath& path) {
365 ThreadRestrictions::AssertIOAllowed();
366 stat_wrapper_t file_info;
367 if (CallStat(path.value().c_str(), &file_info) == 0)
368 return S_ISDIR(file_info.st_mode);
369 return false;
370}
371
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900372bool ReadFromFD(int fd, char* buffer, size_t bytes) {
373 size_t total_read = 0;
374 while (total_read < bytes) {
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900375 ssize_t bytes_read =
376 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read));
377 if (bytes_read <= 0)
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900378 break;
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900379 total_read += bytes_read;
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900380 }
381 return total_read == bytes;
382}
383
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900384bool CreateSymbolicLink(const FilePath& target_path,
385 const FilePath& symlink_path) {
386 DCHECK(!symlink_path.empty());
387 DCHECK(!target_path.empty());
388 return ::symlink(target_path.value().c_str(),
389 symlink_path.value().c_str()) != -1;
390}
391
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900392bool ReadSymbolicLink(const FilePath& symlink_path, FilePath* target_path) {
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900393 DCHECK(!symlink_path.empty());
394 DCHECK(target_path);
395 char buf[PATH_MAX];
396 ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf));
397
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900398 if (count <= 0) {
399 target_path->clear();
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900400 return false;
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900401 }
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900402
403 *target_path = FilePath(FilePath::StringType(buf, count));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900404 return true;
405}
406
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900407bool GetPosixFilePermissions(const FilePath& path, int* mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900408 ThreadRestrictions::AssertIOAllowed();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900409 DCHECK(mode);
410
411 stat_wrapper_t file_info;
412 // Uses stat(), because on symbolic link, lstat() does not return valid
413 // permission bits in st_mode
414 if (CallStat(path.value().c_str(), &file_info) != 0)
415 return false;
416
417 *mode = file_info.st_mode & FILE_PERMISSION_MASK;
418 return true;
419}
420
421bool SetPosixFilePermissions(const FilePath& path,
422 int mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900423 ThreadRestrictions::AssertIOAllowed();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900424 DCHECK((mode & ~FILE_PERMISSION_MASK) == 0);
425
426 // Calls stat() so that we can preserve the higher bits like S_ISGID.
427 stat_wrapper_t stat_buf;
428 if (CallStat(path.value().c_str(), &stat_buf) != 0)
429 return false;
430
431 // Clears the existing permission bits, and adds the new ones.
432 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK;
433 updated_mode_bits |= mode & FILE_PERMISSION_MASK;
434
435 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0)
436 return false;
437
438 return true;
439}
440
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900441#if !defined(OS_MACOSX)
442// This is implemented in file_util_mac.mm for Mac.
443bool GetTempDir(FilePath* path) {
444 const char* tmp = getenv("TMPDIR");
445 if (tmp) {
446 *path = FilePath(tmp);
447 } else {
448#if defined(OS_ANDROID)
449 return PathService::Get(base::DIR_CACHE, path);
450#else
451 *path = FilePath("/tmp");
452#endif
453 }
454 return true;
455}
456#endif // !defined(OS_MACOSX)
457
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900458#if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900459FilePath GetHomeDir() {
460#if defined(OS_CHROMEOS)
nkostylev@chromium.org45c656f2014-05-27 09:12:33 +0900461 if (SysInfo::IsRunningOnChromeOS()) {
nkostylev@chromium.orgcbf27562014-06-04 06:01:57 +0900462 // On Chrome OS chrome::DIR_USER_DATA is overridden with a primary user
463 // homedir once it becomes available. Return / as the safe option.
nkostylev@chromium.org45c656f2014-05-27 09:12:33 +0900464 return FilePath("/");
465 }
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900466#endif
467
468 const char* home_dir = getenv("HOME");
469 if (home_dir && home_dir[0])
470 return FilePath(home_dir);
471
472#if defined(OS_ANDROID)
473 DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
474#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900475 // g_get_home_dir calls getpwent, which can fall through to LDAP calls so
476 // this may do I/O. However, it should be rare that $HOME is not defined and
477 // this is typically called from the path service which has no threading
478 // restrictions. The path service will cache the result which limits the
479 // badness of blocking on I/O. As a result, we don't have a thread
480 // restriction here.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900481 home_dir = g_get_home_dir();
482 if (home_dir && home_dir[0])
483 return FilePath(home_dir);
484#endif
485
486 FilePath rv;
487 if (GetTempDir(&rv))
488 return rv;
489
490 // Last resort.
491 return FilePath("/tmp");
492}
493#endif // !defined(OS_MACOSX)
494
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900495bool CreateTemporaryFile(FilePath* path) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900496 ThreadRestrictions::AssertIOAllowed(); // For call to close().
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900497 FilePath directory;
498 if (!GetTempDir(&directory))
499 return false;
500 int fd = CreateAndOpenFdForTemporaryFile(directory, path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900501 if (fd < 0)
502 return false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900503 close(fd);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900504 return true;
505}
506
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900507FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
508 int fd = CreateAndOpenFdForTemporaryFile(dir, path);
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900509 if (fd < 0)
510 return NULL;
511
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900512 FILE* file = fdopen(fd, "a+");
513 if (!file)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900514 close(fd);
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900515 return file;
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900516}
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900517
518bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900519 ThreadRestrictions::AssertIOAllowed(); // For call to close().
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900520 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file);
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900521 return ((fd >= 0) && !IGNORE_EINTR(close(fd)));
jcampan@chromium.orgbf29e602008-10-11 03:50:32 +0900522}
523
skerner@chromium.orge4432392010-05-01 02:00:09 +0900524static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
525 const FilePath::StringType& name_tmpl,
526 FilePath* new_dir) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900527 ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900528 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
529 << "Directory name template must contain \"XXXXXX\".";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900530
531 FilePath sub_dir = base_dir.Append(name_tmpl);
532 std::string sub_dir_string = sub_dir.value();
533
534 // this should be OK since mkdtemp just replaces characters in place
535 char* buffer = const_cast<char*>(sub_dir_string.c_str());
536 char* dtemp = mkdtemp(buffer);
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900537 if (!dtemp) {
538 DPLOG(ERROR) << "mkdtemp";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900539 return false;
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900540 }
skerner@chromium.orge4432392010-05-01 02:00:09 +0900541 *new_dir = FilePath(dtemp);
542 return true;
543}
544
545bool CreateTemporaryDirInDir(const FilePath& base_dir,
546 const FilePath::StringType& prefix,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +0900547 FilePath* new_dir) {
skerner@chromium.orge4432392010-05-01 02:00:09 +0900548 FilePath::StringType mkdtemp_template = prefix;
549 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX"));
550 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir);
551}
552
erikkay@google.comcce83822008-12-24 05:20:10 +0900553bool CreateNewTempDirectory(const FilePath::StringType& prefix,
554 FilePath* new_temp_path) {
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900555 FilePath tmpdir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900556 if (!GetTempDir(&tmpdir))
557 return false;
skerner@chromium.orge4432392010-05-01 02:00:09 +0900558
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900559 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900560}
561
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900562bool CreateDirectoryAndGetError(const FilePath& full_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900563 File::Error* error) {
brettw@chromium.org738669a2013-12-04 05:08:54 +0900564 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
evanm@google.com874d1672008-10-31 08:54:04 +0900565 std::vector<FilePath> subpaths;
566
567 // Collect a list of all parent directories.
568 FilePath last_path = full_path;
569 subpaths.push_back(full_path);
570 for (FilePath path = full_path.DirName();
571 path.value() != last_path.value(); path = path.DirName()) {
572 subpaths.push_back(path);
573 last_path = path;
574 }
575
576 // Iterate through the parents and create the missing ones.
577 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
578 i != subpaths.rend(); ++i) {
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900579 if (DirectoryExists(*i))
580 continue;
581 if (mkdir(i->value().c_str(), 0700) == 0)
582 continue;
583 // Mkdir failed, but it might have failed with EEXIST, or some other error
584 // due to the the directory appearing out of thin air. This can occur if
585 // two processes are trying to create the same file system tree at the same
586 // time. Check to see if it exists and make sure it is a directory.
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900587 int saved_errno = errno;
588 if (!DirectoryExists(*i)) {
589 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900590 *error = File::OSErrorToFileError(saved_errno);
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900591 return false;
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900592 }
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900593 }
594 return true;
595}
596
brettw@chromium.org70684242013-12-05 03:22:49 +0900597bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
598 FilePath real_path_result;
599 if (!RealPath(path, &real_path_result))
600 return false;
601
602 // To be consistant with windows, fail if |real_path_result| is a
603 // directory.
604 stat_wrapper_t file_info;
605 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
606 S_ISDIR(file_info.st_mode))
607 return false;
608
609 *normalized_path = real_path_result;
610 return true;
611}
612
brettw@chromium.orga9154032013-12-05 05:56:49 +0900613// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
614// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
615bool IsLink(const FilePath& file_path) {
616 stat_wrapper_t st;
617 // If we can't lstat the file, it's safe to assume that the file won't at
618 // least be a 'followable' link.
619 if (CallLstat(file_path.value().c_str(), &st) != 0)
620 return false;
621
622 if (S_ISLNK(st.st_mode))
623 return true;
624 else
625 return false;
626}
627
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900628bool GetFileInfo(const FilePath& file_path, File::Info* results) {
brettw@chromium.orga9154032013-12-05 05:56:49 +0900629 stat_wrapper_t file_info;
630#if defined(OS_ANDROID)
631 if (file_path.IsContentUri()) {
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900632 File file = OpenContentUriForRead(file_path);
633 if (!file.IsValid())
brettw@chromium.orga9154032013-12-05 05:56:49 +0900634 return false;
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +0900635 return file.GetInfo(results);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900636 } else {
637#endif // defined(OS_ANDROID)
638 if (CallStat(file_path.value().c_str(), &file_info) != 0)
639 return false;
640#if defined(OS_ANDROID)
641 }
642#endif // defined(OS_ANDROID)
rvargas@chromium.org527ea312014-04-05 11:39:18 +0900643
644 results->FromStat(file_info);
brettw@chromium.orga9154032013-12-05 05:56:49 +0900645 return true;
646}
647
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900648FILE* OpenFile(const FilePath& filename, const char* mode) {
649 ThreadRestrictions::AssertIOAllowed();
650 FILE* result = NULL;
651 do {
652 result = fopen(filename.value().c_str(), mode);
653 } while (!result && errno == EINTR);
654 return result;
655}
656
rvargas@chromium.org43d4a8e2014-06-10 20:19:50 +0900657// NaCl doesn't implement system calls to open files directly.
658#if !defined(OS_NACL)
659FILE* FileToFILE(File file, const char* mode) {
660 FILE* stream = fdopen(file.GetPlatformFile(), mode);
661 if (stream)
662 file.TakePlatformFile();
663 return stream;
664}
665#endif // !defined(OS_NACL)
666
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900667int ReadFile(const FilePath& filename, char* data, int max_size) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900668 ThreadRestrictions::AssertIOAllowed();
669 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY));
670 if (fd < 0)
671 return -1;
672
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900673 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, max_size));
674 if (IGNORE_EINTR(close(fd)) < 0)
675 return -1;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900676 return bytes_read;
677}
678
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900679int WriteFile(const FilePath& filename, const char* data, int size) {
680 ThreadRestrictions::AssertIOAllowed();
aurimas@chromium.orgb4b9d512014-06-04 04:35:06 +0900681 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0640));
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900682 if (fd < 0)
683 return -1;
684
685 int bytes_written = WriteFileDescriptor(fd, data, size);
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900686 if (IGNORE_EINTR(close(fd)) < 0)
687 return -1;
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900688 return bytes_written;
689}
690
691int WriteFileDescriptor(const int fd, const char* data, int size) {
692 // Allow for partial writes.
693 ssize_t bytes_written_total = 0;
694 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
695 bytes_written_total += bytes_written_partial) {
696 bytes_written_partial =
697 HANDLE_EINTR(write(fd, data + bytes_written_total,
698 size - bytes_written_total));
699 if (bytes_written_partial < 0)
700 return -1;
701 }
702
703 return bytes_written_total;
704}
705
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900706int AppendToFile(const FilePath& filename, const char* data, int size) {
707 ThreadRestrictions::AssertIOAllowed();
708 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
709 if (fd < 0)
710 return -1;
711
712 int bytes_written = WriteFileDescriptor(fd, data, size);
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +0900713 if (IGNORE_EINTR(close(fd)) < 0)
714 return -1;
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900715 return bytes_written;
716}
717
718// Gets the current working directory for the process.
719bool GetCurrentDirectory(FilePath* dir) {
720 // getcwd can return ENOENT, which implies it checks against the disk.
721 ThreadRestrictions::AssertIOAllowed();
722
723 char system_buffer[PATH_MAX] = "";
724 if (!getcwd(system_buffer, sizeof(system_buffer))) {
725 NOTREACHED();
726 return false;
727 }
728 *dir = FilePath(system_buffer);
729 return true;
730}
731
732// Sets the current working directory for the process.
733bool SetCurrentDirectory(const FilePath& path) {
734 ThreadRestrictions::AssertIOAllowed();
735 int ret = chdir(path.value().c_str());
736 return !ret;
737}
738
skerner@google.com93449ef2011-09-22 23:47:18 +0900739bool VerifyPathControlledByUser(const FilePath& base,
740 const FilePath& path,
741 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900742 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900743 if (base != path && !base.IsParent(path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900744 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \""
745 << base.value() << "\", path = \"" << path.value() << "\"";
skerner@google.com93449ef2011-09-22 23:47:18 +0900746 return false;
747 }
748
749 std::vector<FilePath::StringType> base_components;
750 std::vector<FilePath::StringType> path_components;
751
752 base.GetComponents(&base_components);
753 path.GetComponents(&path_components);
754
755 std::vector<FilePath::StringType>::const_iterator ib, ip;
756 for (ib = base_components.begin(), ip = path_components.begin();
757 ib != base_components.end(); ++ib, ++ip) {
758 // |base| must be a subpath of |path|, so all components should match.
759 // If these CHECKs fail, look at the test that base is a parent of
760 // path at the top of this function.
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900761 DCHECK(ip != path_components.end());
762 DCHECK(*ip == *ib);
skerner@google.com93449ef2011-09-22 23:47:18 +0900763 }
764
765 FilePath current_path = base;
skerner@chromium.org80784142011-10-18 06:30:29 +0900766 if (!VerifySpecificPathControlledByUser(current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900767 return false;
768
769 for (; ip != path_components.end(); ++ip) {
770 current_path = current_path.Append(*ip);
skerner@chromium.org80784142011-10-18 06:30:29 +0900771 if (!VerifySpecificPathControlledByUser(
772 current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900773 return false;
774 }
775 return true;
776}
777
qsr@chromium.org4ab5de92012-07-09 23:40:39 +0900778#if defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900779bool VerifyPathControlledByAdmin(const FilePath& path) {
780 const unsigned kRootUid = 0;
781 const FilePath kFileSystemRoot("/");
782
783 // The name of the administrator group on mac os.
skerner@chromium.org80784142011-10-18 06:30:29 +0900784 const char* const kAdminGroupNames[] = {
785 "admin",
786 "wheel"
787 };
skerner@google.com93449ef2011-09-22 23:47:18 +0900788
789 // Reading the groups database may touch the file system.
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900790 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +0900791
skerner@chromium.org80784142011-10-18 06:30:29 +0900792 std::set<gid_t> allowed_group_ids;
793 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) {
794 struct group *group_record = getgrnam(kAdminGroupNames[i]);
795 if (!group_record) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900796 DPLOG(ERROR) << "Could not get the group ID of group \""
797 << kAdminGroupNames[i] << "\".";
skerner@chromium.org80784142011-10-18 06:30:29 +0900798 continue;
799 }
800
801 allowed_group_ids.insert(group_record->gr_gid);
skerner@google.com93449ef2011-09-22 23:47:18 +0900802 }
803
804 return VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +0900805 kFileSystemRoot, path, kRootUid, allowed_group_ids);
skerner@google.com93449ef2011-09-22 23:47:18 +0900806}
stuartmorgan@chromium.org925e0b72012-07-24 20:23:32 +0900807#endif // defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900808
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900809int GetMaximumPathComponentLength(const FilePath& path) {
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900810 ThreadRestrictions::AssertIOAllowed();
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900811 return pathconf(path.value().c_str(), _PC_NAME_MAX);
812}
813
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900814#if !defined(OS_ANDROID)
815// This is implemented in file_util_android.cc for that platform.
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900816bool GetShmemTempDir(bool executable, FilePath* path) {
817#if defined(OS_LINUX)
818 bool use_dev_shm = true;
819 if (executable) {
820 static const bool s_dev_shm_executable = DetermineDevShmExecutable();
821 use_dev_shm = s_dev_shm_executable;
822 }
823 if (use_dev_shm) {
824 *path = FilePath("/dev/shm");
825 return true;
826 }
827#endif
828 return GetTempDir(path);
829}
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900830#endif // !defined(OS_ANDROID)
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900831
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900832// -----------------------------------------------------------------------------
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900833
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900834namespace internal {
835
836bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
837 ThreadRestrictions::AssertIOAllowed();
838 // Windows compatibility: if to_path exists, from_path and to_path
839 // must be the same type, either both files, or both directories.
840 stat_wrapper_t to_file_info;
841 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
842 stat_wrapper_t from_file_info;
843 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
844 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
845 return false;
846 } else {
847 return false;
848 }
849 }
850
851 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
852 return true;
853
854 if (!CopyDirectory(from_path, to_path, true))
855 return false;
856
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900857 DeleteFile(from_path, true);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900858 return true;
859}
860
861#if !defined(OS_MACOSX)
862// Mac has its own implementation, this is for all other Posix systems.
863bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
864 ThreadRestrictions::AssertIOAllowed();
865 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY));
866 if (infile < 0)
867 return false;
868
869 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666));
870 if (outfile < 0) {
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900871 close(infile);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900872 return false;
873 }
874
875 const size_t kBufferSize = 32768;
876 std::vector<char> buffer(kBufferSize);
877 bool result = true;
878
879 while (result) {
880 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size()));
881 if (bytes_read < 0) {
882 result = false;
883 break;
884 }
885 if (bytes_read == 0)
886 break;
887 // Allow for partial writes
888 ssize_t bytes_written_per_read = 0;
889 do {
890 ssize_t bytes_written_partial = HANDLE_EINTR(write(
891 outfile,
892 &buffer[bytes_written_per_read],
893 bytes_read - bytes_written_per_read));
894 if (bytes_written_partial < 0) {
895 result = false;
896 break;
897 }
898 bytes_written_per_read += bytes_written_partial;
899 } while (bytes_written_per_read < bytes_read);
900 }
901
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900902 if (IGNORE_EINTR(close(infile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900903 result = false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900904 if (IGNORE_EINTR(close(outfile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900905 result = false;
906
907 return result;
908}
909#endif // !defined(OS_MACOSX)
910
911} // namespace internal
912} // namespace base