blob: 25701727306956d81c09033e2244c83fa2b3f408 [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"
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
rsesek@chromium.orgdc818ac2012-07-20 07:24:02 +090064#if defined(OS_BSD) || defined(OS_MACOSX)
skerner@google.com93449ef2011-09-22 23:47:18 +090065typedef struct stat stat_wrapper_t;
66static 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}
74#else
75typedef struct stat64 stat_wrapper_t;
76static int CallStat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090077 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090078 return stat64(path, sb);
79}
80static int CallLstat(const char *path, stat_wrapper_t *sb) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090081 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +090082 return lstat64(path, sb);
83}
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090084#if defined(OS_ANDROID)
85static int CallFstat(int fd, stat_wrapper_t *sb) {
86 ThreadRestrictions::AssertIOAllowed();
87 return fstat64(fd, sb);
88}
89#endif
skerner@google.com93449ef2011-09-22 23:47:18 +090090#endif
91
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090092// Helper for NormalizeFilePath(), defined below.
93bool RealPath(const FilePath& path, FilePath* real_path) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +090094 ThreadRestrictions::AssertIOAllowed(); // For realpath().
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090095 FilePath::CharType buf[PATH_MAX];
96 if (!realpath(path.value().c_str(), buf))
97 return false;
98
99 *real_path = FilePath(buf);
100 return true;
101}
102
skerner@google.com93449ef2011-09-22 23:47:18 +0900103// Helper for VerifyPathControlledByUser.
104bool VerifySpecificPathControlledByUser(const FilePath& path,
105 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900106 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900107 stat_wrapper_t stat_info;
108 if (CallLstat(path.value().c_str(), &stat_info) != 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900109 DPLOG(ERROR) << "Failed to get information on path "
110 << path.value();
skerner@google.com93449ef2011-09-22 23:47:18 +0900111 return false;
112 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900113
skerner@google.com93449ef2011-09-22 23:47:18 +0900114 if (S_ISLNK(stat_info.st_mode)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900115 DLOG(ERROR) << "Path " << path.value()
skerner@google.com93449ef2011-09-22 23:47:18 +0900116 << " is a symbolic link.";
117 return false;
118 }
119
120 if (stat_info.st_uid != owner_uid) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900121 DLOG(ERROR) << "Path " << path.value()
122 << " is owned by the wrong user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900123 return false;
124 }
125
skerner@chromium.org80784142011-10-18 06:30:29 +0900126 if ((stat_info.st_mode & S_IWGRP) &&
127 !ContainsKey(group_gids, stat_info.st_gid)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900128 DLOG(ERROR) << "Path " << path.value()
129 << " is writable by an unprivileged group.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900130 return false;
131 }
132
133 if (stat_info.st_mode & S_IWOTH) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900134 DLOG(ERROR) << "Path " << path.value()
135 << " is writable by any user.";
skerner@google.com93449ef2011-09-22 23:47:18 +0900136 return false;
137 }
138
139 return true;
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900140}
skerner@google.com93449ef2011-09-22 23:47:18 +0900141
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900142std::string TempFileName() {
143#if defined(OS_MACOSX)
144 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
145#endif
146
147#if defined(GOOGLE_CHROME_BUILD)
148 return std::string(".com.google.Chrome.XXXXXX");
149#else
150 return std::string(".org.chromium.Chromium.XXXXXX");
151#endif
152}
153
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900154// Creates and opens a temporary file in |directory|, returning the
155// file descriptor. |path| is set to the temporary file path.
156// This function does NOT unlink() the file.
157int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
158 ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
159 *path = directory.Append(base::TempFileName());
160 const std::string& tmpdir_string = path->value();
161 // this should be OK since mkstemp just replaces characters in place
162 char* buffer = const_cast<char*>(tmpdir_string.c_str());
163
164 return HANDLE_EINTR(mkstemp(buffer));
165}
166
167#if defined(OS_LINUX)
168// Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC.
169// This depends on the mount options used for /dev/shm, which vary among
170// different Linux distributions and possibly local configuration. It also
171// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
172// but its kernel allows mprotect with PROT_EXEC anyway.
173bool DetermineDevShmExecutable() {
174 bool result = false;
175 FilePath path;
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900176
177 ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path));
178 if (fd.is_valid()) {
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900179 DeleteFile(path, false);
180 long sysconf_result = sysconf(_SC_PAGESIZE);
181 CHECK_GE(sysconf_result, 0);
182 size_t pagesize = static_cast<size_t>(sysconf_result);
183 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900184 void *mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0);
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900185 if (mapping != MAP_FAILED) {
186 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
187 result = true;
188 munmap(mapping, pagesize);
189 }
190 }
191 return result;
192}
193#endif // defined(OS_LINUX)
194
skerner@google.com93449ef2011-09-22 23:47:18 +0900195} // namespace
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900196
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900197FilePath MakeAbsoluteFilePath(const FilePath& input) {
198 ThreadRestrictions::AssertIOAllowed();
199 char full_path[PATH_MAX];
200 if (realpath(input.value().c_str(), full_path) == NULL)
201 return FilePath();
202 return FilePath(full_path);
mark@chromium.org13aa8aa2011-04-22 13:15:13 +0900203}
mark@chromium.org8ca0d272008-09-12 02:36:23 +0900204
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900205// TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*"
206// which works both with and without the recursive flag. I'm not sure we need
207// that functionality. If not, remove from file_util_win.cc, otherwise add it
208// here.
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900209bool DeleteFile(const FilePath& path, bool recursive) {
brettw@chromium.orge9f99482013-07-02 04:41:02 +0900210 ThreadRestrictions::AssertIOAllowed();
evanm@google.com874d1672008-10-31 08:54:04 +0900211 const char* path_str = path.value().c_str();
benl@chromium.org6b6b2162009-09-08 01:39:46 +0900212 stat_wrapper_t file_info;
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900213 int test = CallLstat(path_str, &file_info);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900214 if (test != 0) {
215 // The Windows version defines this condition as success.
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900216 bool ret = (errno == ENOENT || errno == ENOTDIR);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900217 return ret;
218 }
219 if (!S_ISDIR(file_info.st_mode))
evanm@google.com874d1672008-10-31 08:54:04 +0900220 return (unlink(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900221 if (!recursive)
evanm@google.com874d1672008-10-31 08:54:04 +0900222 return (rmdir(path_str) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900223
224 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900225 std::stack<std::string> directories;
226 directories.push(path.value());
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900227 FileEnumerator traversal(path, true,
228 FileEnumerator::FILES | FileEnumerator::DIRECTORIES |
229 FileEnumerator::SHOW_SYM_LINKS);
thestig@chromium.org3217c822009-08-07 06:23:07 +0900230 for (FilePath current = traversal.Next(); success && !current.empty();
231 current = traversal.Next()) {
brettw@chromium.org56946722013-06-08 13:53:36 +0900232 if (traversal.GetInfo().IsDirectory())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900233 directories.push(current.value());
234 else
235 success = (unlink(current.value().c_str()) == 0);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900236 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900237
238 while (success && !directories.empty()) {
239 FilePath dir = FilePath(directories.top());
240 directories.pop();
241 success = (rmdir(dir.value().c_str()) == 0);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900242 }
243 return success;
244}
245
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900246bool ReplaceFile(const FilePath& from_path,
247 const FilePath& to_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900248 File::Error* error) {
brettw@chromium.org0878fea2013-07-02 08:07:36 +0900249 ThreadRestrictions::AssertIOAllowed();
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900250 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
251 return true;
252 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900253 *error = File::OSErrorToFileError(errno);
dgrogan@chromium.org38fc56d2013-05-09 07:02:36 +0900254 return false;
phajdan.jr@chromium.orgd86bea02009-05-20 02:21:07 +0900255}
256
dbeam@chromium.org85aa52a2013-05-08 14:46:20 +0900257bool CopyDirectory(const FilePath& from_path,
258 const FilePath& to_path,
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900259 bool recursive) {
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900260 ThreadRestrictions::AssertIOAllowed();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900261 // Some old callers of CopyDirectory want it to support wildcards.
262 // After some discussion, we decided to fix those callers.
263 // Break loudly here if anyone tries to do this.
evanm@google.com874d1672008-10-31 08:54:04 +0900264 DCHECK(to_path.value().find('*') == std::string::npos);
265 DCHECK(from_path.value().find('*') == std::string::npos);
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900266
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900267 if (from_path.value().size() >= PATH_MAX) {
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900268 return false;
269 }
270
thestig@chromium.org3217c822009-08-07 06:23:07 +0900271 // This function does not properly handle destinations within the source
272 FilePath real_to_path = to_path;
brettw@chromium.org10b64122013-07-12 02:36:07 +0900273 if (PathExists(real_to_path)) {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900274 real_to_path = MakeAbsoluteFilePath(real_to_path);
275 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900276 return false;
277 } else {
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900278 real_to_path = MakeAbsoluteFilePath(real_to_path.DirName());
279 if (real_to_path.empty())
thestig@chromium.org3217c822009-08-07 06:23:07 +0900280 return false;
281 }
brettw@chromium.org99b198e2013-04-12 14:17:15 +0900282 FilePath real_from_path = MakeAbsoluteFilePath(from_path);
283 if (real_from_path.empty())
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900284 return false;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900285 if (real_to_path.value().size() >= real_from_path.value().size() &&
286 real_to_path.value().compare(0, real_from_path.value().size(),
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900287 real_from_path.value()) == 0) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900288 return false;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900289 }
thestig@chromium.org3217c822009-08-07 06:23:07 +0900290
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900291 int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900292 if (recursive)
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900293 traverse_type |= FileEnumerator::DIRECTORIES;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900294 FileEnumerator traversal(from_path, recursive, traverse_type);
295
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900296 // We have to mimic windows behavior here. |to_path| may not exist yet,
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900297 // start the loop with |to_path|.
brettw@chromium.org56946722013-06-08 13:53:36 +0900298 struct stat from_stat;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900299 FilePath current = from_path;
brettw@chromium.org56946722013-06-08 13:53:36 +0900300 if (stat(from_path.value().c_str(), &from_stat) < 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900301 DLOG(ERROR) << "CopyDirectory() couldn't stat source directory: "
302 << from_path.value() << " errno = " << errno;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900303 return false;
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900304 }
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900305 struct stat to_path_stat;
306 FilePath from_path_base = from_path;
307 if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 &&
308 S_ISDIR(to_path_stat.st_mode)) {
309 // If the destination already exists and is a directory, then the
310 // top level of source needs to be copied.
311 from_path_base = from_path.DirName();
312 }
313
314 // The Windows version of this function assumes that non-recursive calls
315 // will always have a directory for from_path.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900316 // TODO(maruel): This is not necessary anymore.
brettw@chromium.org56946722013-06-08 13:53:36 +0900317 DCHECK(recursive || S_ISDIR(from_stat.st_mode));
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900318
maruel@chromium.org0a177fa2014-02-06 04:55:52 +0900319 bool success = true;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900320 while (success && !current.empty()) {
aedla@chromium.orgfef1a202013-01-30 20:38:02 +0900321 // current is the source path, including from_path, so append
322 // the suffix after from_path to to_path to create the target_path.
323 FilePath target_path(to_path);
324 if (from_path_base != current) {
325 if (!from_path_base.AppendRelativePath(current, &target_path)) {
326 success = false;
327 break;
328 }
phajdan.jr@chromium.orgecf50752009-01-14 03:57:46 +0900329 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900330
brettw@chromium.org56946722013-06-08 13:53:36 +0900331 if (S_ISDIR(from_stat.st_mode)) {
332 if (mkdir(target_path.value().c_str(), from_stat.st_mode & 01777) != 0 &&
thestig@chromium.org3217c822009-08-07 06:23:07 +0900333 errno != EEXIST) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900334 DLOG(ERROR) << "CopyDirectory() couldn't create directory: "
335 << target_path.value() << " errno = " << errno;
thestig@chromium.org3217c822009-08-07 06:23:07 +0900336 success = false;
337 }
brettw@chromium.org56946722013-06-08 13:53:36 +0900338 } else if (S_ISREG(from_stat.st_mode)) {
thestig@chromium.org3217c822009-08-07 06:23:07 +0900339 if (!CopyFile(current, target_path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900340 DLOG(ERROR) << "CopyDirectory() couldn't create file: "
341 << target_path.value();
thestig@chromium.org3217c822009-08-07 06:23:07 +0900342 success = false;
343 }
344 } else {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900345 DLOG(WARNING) << "CopyDirectory() skipping non-regular file: "
346 << current.value();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900347 }
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900348
thestig@chromium.org3217c822009-08-07 06:23:07 +0900349 current = traversal.Next();
brettw@chromium.org56946722013-06-08 13:53:36 +0900350 if (!current.empty())
351 from_stat = traversal.GetInfo().stat();
evanm@google.com5c1d39b2008-09-19 04:15:54 +0900352 }
353
thestig@chromium.org3217c822009-08-07 06:23:07 +0900354 return success;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900355}
356
brettw@chromium.org10b64122013-07-12 02:36:07 +0900357bool PathExists(const FilePath& path) {
358 ThreadRestrictions::AssertIOAllowed();
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +0900359#if defined(OS_ANDROID)
360 if (path.IsContentUri()) {
361 return ContentUriExists(path);
362 }
363#endif
brettw@chromium.org10b64122013-07-12 02:36:07 +0900364 return access(path.value().c_str(), F_OK) == 0;
365}
366
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900367bool PathIsWritable(const FilePath& path) {
368 ThreadRestrictions::AssertIOAllowed();
369 return access(path.value().c_str(), W_OK) == 0;
370}
371
372bool DirectoryExists(const FilePath& path) {
373 ThreadRestrictions::AssertIOAllowed();
374 stat_wrapper_t file_info;
375 if (CallStat(path.value().c_str(), &file_info) == 0)
376 return S_ISDIR(file_info.st_mode);
377 return false;
378}
379
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900380bool ReadFromFD(int fd, char* buffer, size_t bytes) {
381 size_t total_read = 0;
382 while (total_read < bytes) {
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900383 ssize_t bytes_read =
384 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read));
385 if (bytes_read <= 0)
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900386 break;
agl@chromium.orgd263ad72009-05-02 06:37:31 +0900387 total_read += bytes_read;
phajdan.jr@chromium.org23725932009-04-23 21:38:08 +0900388 }
389 return total_read == bytes;
390}
391
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900392bool CreateSymbolicLink(const FilePath& target_path,
393 const FilePath& symlink_path) {
394 DCHECK(!symlink_path.empty());
395 DCHECK(!target_path.empty());
396 return ::symlink(target_path.value().c_str(),
397 symlink_path.value().c_str()) != -1;
398}
399
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900400bool ReadSymbolicLink(const FilePath& symlink_path, FilePath* target_path) {
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900401 DCHECK(!symlink_path.empty());
402 DCHECK(target_path);
403 char buf[PATH_MAX];
404 ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf));
405
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900406 if (count <= 0) {
407 target_path->clear();
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900408 return false;
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +0900409 }
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900410
411 *target_path = FilePath(FilePath::StringType(buf, count));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900412 return true;
413}
414
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900415bool GetPosixFilePermissions(const FilePath& path, int* mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900416 ThreadRestrictions::AssertIOAllowed();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900417 DCHECK(mode);
418
419 stat_wrapper_t file_info;
420 // Uses stat(), because on symbolic link, lstat() does not return valid
421 // permission bits in st_mode
422 if (CallStat(path.value().c_str(), &file_info) != 0)
423 return false;
424
425 *mode = file_info.st_mode & FILE_PERMISSION_MASK;
426 return true;
427}
428
429bool SetPosixFilePermissions(const FilePath& path,
430 int mode) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900431 ThreadRestrictions::AssertIOAllowed();
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900432 DCHECK((mode & ~FILE_PERMISSION_MASK) == 0);
433
434 // Calls stat() so that we can preserve the higher bits like S_ISGID.
435 stat_wrapper_t stat_buf;
436 if (CallStat(path.value().c_str(), &stat_buf) != 0)
437 return false;
438
439 // Clears the existing permission bits, and adds the new ones.
440 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK;
441 updated_mode_bits |= mode & FILE_PERMISSION_MASK;
442
443 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0)
444 return false;
445
446 return true;
447}
448
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900449#if !defined(OS_MACOSX)
450// This is implemented in file_util_mac.mm for Mac.
451bool GetTempDir(FilePath* path) {
452 const char* tmp = getenv("TMPDIR");
453 if (tmp) {
454 *path = FilePath(tmp);
455 } else {
456#if defined(OS_ANDROID)
457 return PathService::Get(base::DIR_CACHE, path);
458#else
459 *path = FilePath("/tmp");
460#endif
461 }
462 return true;
463}
464#endif // !defined(OS_MACOSX)
465
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900466#if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900467FilePath GetHomeDir() {
468#if defined(OS_CHROMEOS)
469 if (SysInfo::IsRunningOnChromeOS())
470 return FilePath("/home/chronos/user");
471#endif
472
473 const char* home_dir = getenv("HOME");
474 if (home_dir && home_dir[0])
475 return FilePath(home_dir);
476
477#if defined(OS_ANDROID)
478 DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
479#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
brettw@chromium.org49de1af2014-02-20 05:34:23 +0900480 // g_get_home_dir calls getpwent, which can fall through to LDAP calls so
481 // this may do I/O. However, it should be rare that $HOME is not defined and
482 // this is typically called from the path service which has no threading
483 // restrictions. The path service will cache the result which limits the
484 // badness of blocking on I/O. As a result, we don't have a thread
485 // restriction here.
brettw@chromium.org83c44c82013-12-03 03:55:49 +0900486 home_dir = g_get_home_dir();
487 if (home_dir && home_dir[0])
488 return FilePath(home_dir);
489#endif
490
491 FilePath rv;
492 if (GetTempDir(&rv))
493 return rv;
494
495 // Last resort.
496 return FilePath("/tmp");
497}
498#endif // !defined(OS_MACOSX)
499
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +0900500bool CreateTemporaryFile(FilePath* path) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900501 ThreadRestrictions::AssertIOAllowed(); // For call to close().
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900502 FilePath directory;
503 if (!GetTempDir(&directory))
504 return false;
505 int fd = CreateAndOpenFdForTemporaryFile(directory, path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900506 if (fd < 0)
507 return false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900508 close(fd);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900509 return true;
510}
511
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900512FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
513 int fd = CreateAndOpenFdForTemporaryFile(dir, path);
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900514 if (fd < 0)
515 return NULL;
516
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900517 FILE* file = fdopen(fd, "a+");
518 if (!file)
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900519 close(fd);
phajdan.jr@chromium.orgae25ba22011-04-19 04:05:53 +0900520 return file;
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +0900521}
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900522
523bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900524 ThreadRestrictions::AssertIOAllowed(); // For call to close().
dumi@chromium.org13e715d2009-09-12 05:06:27 +0900525 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file);
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900526 return ((fd >= 0) && !IGNORE_EINTR(close(fd)));
jcampan@chromium.orgbf29e602008-10-11 03:50:32 +0900527}
528
skerner@chromium.orge4432392010-05-01 02:00:09 +0900529static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir,
530 const FilePath::StringType& name_tmpl,
531 FilePath* new_dir) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900532 ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp().
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900533 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos)
534 << "Directory name template must contain \"XXXXXX\".";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900535
536 FilePath sub_dir = base_dir.Append(name_tmpl);
537 std::string sub_dir_string = sub_dir.value();
538
539 // this should be OK since mkdtemp just replaces characters in place
540 char* buffer = const_cast<char*>(sub_dir_string.c_str());
541 char* dtemp = mkdtemp(buffer);
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900542 if (!dtemp) {
543 DPLOG(ERROR) << "mkdtemp";
skerner@chromium.orge4432392010-05-01 02:00:09 +0900544 return false;
evan@chromium.org01ec22c2010-07-29 06:00:51 +0900545 }
skerner@chromium.orge4432392010-05-01 02:00:09 +0900546 *new_dir = FilePath(dtemp);
547 return true;
548}
549
550bool CreateTemporaryDirInDir(const FilePath& base_dir,
551 const FilePath::StringType& prefix,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +0900552 FilePath* new_dir) {
skerner@chromium.orge4432392010-05-01 02:00:09 +0900553 FilePath::StringType mkdtemp_template = prefix;
554 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX"));
555 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir);
556}
557
erikkay@google.comcce83822008-12-24 05:20:10 +0900558bool CreateNewTempDirectory(const FilePath::StringType& prefix,
559 FilePath* new_temp_path) {
estade@chromium.orgf474a1b2008-11-11 09:01:38 +0900560 FilePath tmpdir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900561 if (!GetTempDir(&tmpdir))
562 return false;
skerner@chromium.orge4432392010-05-01 02:00:09 +0900563
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900564 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900565}
566
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900567bool CreateDirectoryAndGetError(const FilePath& full_path,
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900568 File::Error* error) {
brettw@chromium.org738669a2013-12-04 05:08:54 +0900569 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
evanm@google.com874d1672008-10-31 08:54:04 +0900570 std::vector<FilePath> subpaths;
571
572 // Collect a list of all parent directories.
573 FilePath last_path = full_path;
574 subpaths.push_back(full_path);
575 for (FilePath path = full_path.DirName();
576 path.value() != last_path.value(); path = path.DirName()) {
577 subpaths.push_back(path);
578 last_path = path;
579 }
580
581 // Iterate through the parents and create the missing ones.
582 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
583 i != subpaths.rend(); ++i) {
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900584 if (DirectoryExists(*i))
585 continue;
586 if (mkdir(i->value().c_str(), 0700) == 0)
587 continue;
588 // Mkdir failed, but it might have failed with EEXIST, or some other error
589 // due to the the directory appearing out of thin air. This can occur if
590 // two processes are trying to create the same file system tree at the same
591 // time. Check to see if it exists and make sure it is a directory.
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900592 int saved_errno = errno;
593 if (!DirectoryExists(*i)) {
594 if (error)
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900595 *error = File::OSErrorToFileError(saved_errno);
thestig@chromium.org2e7eebc2010-03-18 06:39:42 +0900596 return false;
dgrogan@chromium.orgf7728132013-06-11 12:50:25 +0900597 }
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900598 }
599 return true;
600}
601
brettw@chromium.org70684242013-12-05 03:22:49 +0900602bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
603 FilePath real_path_result;
604 if (!RealPath(path, &real_path_result))
605 return false;
606
607 // To be consistant with windows, fail if |real_path_result| is a
608 // directory.
609 stat_wrapper_t file_info;
610 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
611 S_ISDIR(file_info.st_mode))
612 return false;
613
614 *normalized_path = real_path_result;
615 return true;
616}
617
brettw@chromium.orga9154032013-12-05 05:56:49 +0900618// TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks
619// correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948
620bool IsLink(const FilePath& file_path) {
621 stat_wrapper_t st;
622 // If we can't lstat the file, it's safe to assume that the file won't at
623 // least be a 'followable' link.
624 if (CallLstat(file_path.value().c_str(), &st) != 0)
625 return false;
626
627 if (S_ISLNK(st.st_mode))
628 return true;
629 else
630 return false;
631}
632
rvargas@chromium.orgb005b382014-01-07 19:06:58 +0900633bool GetFileInfo(const FilePath& file_path, File::Info* results) {
brettw@chromium.orga9154032013-12-05 05:56:49 +0900634 stat_wrapper_t file_info;
635#if defined(OS_ANDROID)
636 if (file_path.IsContentUri()) {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900637 ScopedFD fd(OpenContentUriForRead(file_path));
638 if (!fd.is_valid())
brettw@chromium.orga9154032013-12-05 05:56:49 +0900639 return false;
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +0900640 if (CallFstat(fd.get(), &file_info) != 0)
brettw@chromium.orga9154032013-12-05 05:56:49 +0900641 return false;
642 } else {
643#endif // defined(OS_ANDROID)
644 if (CallStat(file_path.value().c_str(), &file_info) != 0)
645 return false;
646#if defined(OS_ANDROID)
647 }
648#endif // defined(OS_ANDROID)
649 results->is_directory = S_ISDIR(file_info.st_mode);
650 results->size = file_info.st_size;
r.c.ladan@gmail.com733a5022014-03-11 13:11:02 +0900651#if defined(OS_MACOSX) || (defined(OS_FREEBSD) && __FreeBSD_version < 900000)
brettw@chromium.orga9154032013-12-05 05:56:49 +0900652 results->last_modified = Time::FromTimeSpec(file_info.st_mtimespec);
653 results->last_accessed = Time::FromTimeSpec(file_info.st_atimespec);
654 results->creation_time = Time::FromTimeSpec(file_info.st_ctimespec);
655#elif defined(OS_ANDROID)
656 results->last_modified = Time::FromTimeT(file_info.st_mtime);
657 results->last_accessed = Time::FromTimeT(file_info.st_atime);
658 results->creation_time = Time::FromTimeT(file_info.st_ctime);
659#else
660 results->last_modified = Time::FromTimeSpec(file_info.st_mtim);
661 results->last_accessed = Time::FromTimeSpec(file_info.st_atim);
662 results->creation_time = Time::FromTimeSpec(file_info.st_ctim);
663#endif
664 return true;
665}
666
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900667FILE* OpenFile(const FilePath& filename, const char* mode) {
668 ThreadRestrictions::AssertIOAllowed();
669 FILE* result = NULL;
670 do {
671 result = fopen(filename.value().c_str(), mode);
672 } while (!result && errno == EINTR);
673 return result;
674}
675
676int ReadFile(const FilePath& filename, char* data, int size) {
677 ThreadRestrictions::AssertIOAllowed();
678 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY));
679 if (fd < 0)
680 return -1;
681
682 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size));
683 if (int ret = IGNORE_EINTR(close(fd)) < 0)
684 return ret;
685 return bytes_read;
686}
687
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900688int WriteFile(const FilePath& filename, const char* data, int size) {
689 ThreadRestrictions::AssertIOAllowed();
690 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
691 if (fd < 0)
692 return -1;
693
694 int bytes_written = WriteFileDescriptor(fd, data, size);
695 if (int ret = IGNORE_EINTR(close(fd)) < 0)
696 return ret;
697 return bytes_written;
698}
699
700int WriteFileDescriptor(const int fd, const char* data, int size) {
701 // Allow for partial writes.
702 ssize_t bytes_written_total = 0;
703 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
704 bytes_written_total += bytes_written_partial) {
705 bytes_written_partial =
706 HANDLE_EINTR(write(fd, data + bytes_written_total,
707 size - bytes_written_total));
708 if (bytes_written_partial < 0)
709 return -1;
710 }
711
712 return bytes_written_total;
713}
714
brettw@chromium.org14b3aa22014-03-12 05:59:02 +0900715int AppendToFile(const FilePath& filename, const char* data, int size) {
716 ThreadRestrictions::AssertIOAllowed();
717 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
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
727// Gets the current working directory for the process.
728bool GetCurrentDirectory(FilePath* dir) {
729 // getcwd can return ENOENT, which implies it checks against the disk.
730 ThreadRestrictions::AssertIOAllowed();
731
732 char system_buffer[PATH_MAX] = "";
733 if (!getcwd(system_buffer, sizeof(system_buffer))) {
734 NOTREACHED();
735 return false;
736 }
737 *dir = FilePath(system_buffer);
738 return true;
739}
740
741// Sets the current working directory for the process.
742bool SetCurrentDirectory(const FilePath& path) {
743 ThreadRestrictions::AssertIOAllowed();
744 int ret = chdir(path.value().c_str());
745 return !ret;
746}
747
skerner@google.com93449ef2011-09-22 23:47:18 +0900748bool VerifyPathControlledByUser(const FilePath& base,
749 const FilePath& path,
750 uid_t owner_uid,
skerner@chromium.org80784142011-10-18 06:30:29 +0900751 const std::set<gid_t>& group_gids) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900752 if (base != path && !base.IsParent(path)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900753 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \""
754 << base.value() << "\", path = \"" << path.value() << "\"";
skerner@google.com93449ef2011-09-22 23:47:18 +0900755 return false;
756 }
757
758 std::vector<FilePath::StringType> base_components;
759 std::vector<FilePath::StringType> path_components;
760
761 base.GetComponents(&base_components);
762 path.GetComponents(&path_components);
763
764 std::vector<FilePath::StringType>::const_iterator ib, ip;
765 for (ib = base_components.begin(), ip = path_components.begin();
766 ib != base_components.end(); ++ib, ++ip) {
767 // |base| must be a subpath of |path|, so all components should match.
768 // If these CHECKs fail, look at the test that base is a parent of
769 // path at the top of this function.
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900770 DCHECK(ip != path_components.end());
771 DCHECK(*ip == *ib);
skerner@google.com93449ef2011-09-22 23:47:18 +0900772 }
773
774 FilePath current_path = base;
skerner@chromium.org80784142011-10-18 06:30:29 +0900775 if (!VerifySpecificPathControlledByUser(current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900776 return false;
777
778 for (; ip != path_components.end(); ++ip) {
779 current_path = current_path.Append(*ip);
skerner@chromium.org80784142011-10-18 06:30:29 +0900780 if (!VerifySpecificPathControlledByUser(
781 current_path, owner_uid, group_gids))
skerner@google.com93449ef2011-09-22 23:47:18 +0900782 return false;
783 }
784 return true;
785}
786
qsr@chromium.org4ab5de92012-07-09 23:40:39 +0900787#if defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900788bool VerifyPathControlledByAdmin(const FilePath& path) {
789 const unsigned kRootUid = 0;
790 const FilePath kFileSystemRoot("/");
791
792 // The name of the administrator group on mac os.
skerner@chromium.org80784142011-10-18 06:30:29 +0900793 const char* const kAdminGroupNames[] = {
794 "admin",
795 "wheel"
796 };
skerner@google.com93449ef2011-09-22 23:47:18 +0900797
798 // Reading the groups database may touch the file system.
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900799 ThreadRestrictions::AssertIOAllowed();
skerner@google.com93449ef2011-09-22 23:47:18 +0900800
skerner@chromium.org80784142011-10-18 06:30:29 +0900801 std::set<gid_t> allowed_group_ids;
802 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) {
803 struct group *group_record = getgrnam(kAdminGroupNames[i]);
804 if (!group_record) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900805 DPLOG(ERROR) << "Could not get the group ID of group \""
806 << kAdminGroupNames[i] << "\".";
skerner@chromium.org80784142011-10-18 06:30:29 +0900807 continue;
808 }
809
810 allowed_group_ids.insert(group_record->gr_gid);
skerner@google.com93449ef2011-09-22 23:47:18 +0900811 }
812
813 return VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +0900814 kFileSystemRoot, path, kRootUid, allowed_group_ids);
skerner@google.com93449ef2011-09-22 23:47:18 +0900815}
stuartmorgan@chromium.org925e0b72012-07-24 20:23:32 +0900816#endif // defined(OS_MACOSX) && !defined(OS_IOS)
skerner@google.com93449ef2011-09-22 23:47:18 +0900817
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900818int GetMaximumPathComponentLength(const FilePath& path) {
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900819 ThreadRestrictions::AssertIOAllowed();
kinaba@chromium.orgbbe80ba2013-02-21 12:24:08 +0900820 return pathconf(path.value().c_str(), _PC_NAME_MAX);
821}
822
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900823#if !defined(OS_ANDROID)
824// This is implemented in file_util_android.cc for that platform.
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900825bool GetShmemTempDir(bool executable, FilePath* path) {
826#if defined(OS_LINUX)
827 bool use_dev_shm = true;
828 if (executable) {
829 static const bool s_dev_shm_executable = DetermineDevShmExecutable();
830 use_dev_shm = s_dev_shm_executable;
831 }
832 if (use_dev_shm) {
833 *path = FilePath("/dev/shm");
834 return true;
835 }
836#endif
837 return GetTempDir(path);
838}
viettrungluu@chromium.org5c328d52014-03-21 06:33:39 +0900839#endif // !defined(OS_ANDROID)
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +0900840
brettw@chromium.orgaa82a772014-03-14 02:26:21 +0900841// -----------------------------------------------------------------------------
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900842
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900843namespace internal {
844
845bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
846 ThreadRestrictions::AssertIOAllowed();
847 // Windows compatibility: if to_path exists, from_path and to_path
848 // must be the same type, either both files, or both directories.
849 stat_wrapper_t to_file_info;
850 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
851 stat_wrapper_t from_file_info;
852 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
853 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
854 return false;
855 } else {
856 return false;
857 }
858 }
859
860 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
861 return true;
862
863 if (!CopyDirectory(from_path, to_path, true))
864 return false;
865
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900866 DeleteFile(from_path, true);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900867 return true;
868}
869
870#if !defined(OS_MACOSX)
871// Mac has its own implementation, this is for all other Posix systems.
872bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
873 ThreadRestrictions::AssertIOAllowed();
874 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY));
875 if (infile < 0)
876 return false;
877
878 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666));
879 if (outfile < 0) {
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900880 close(infile);
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900881 return false;
882 }
883
884 const size_t kBufferSize = 32768;
885 std::vector<char> buffer(kBufferSize);
886 bool result = true;
887
888 while (result) {
889 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size()));
890 if (bytes_read < 0) {
891 result = false;
892 break;
893 }
894 if (bytes_read == 0)
895 break;
896 // Allow for partial writes
897 ssize_t bytes_written_per_read = 0;
898 do {
899 ssize_t bytes_written_partial = HANDLE_EINTR(write(
900 outfile,
901 &buffer[bytes_written_per_read],
902 bytes_read - bytes_written_per_read));
903 if (bytes_written_partial < 0) {
904 result = false;
905 break;
906 }
907 bytes_written_per_read += bytes_written_partial;
908 } while (bytes_written_per_read < bytes_read);
909 }
910
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900911 if (IGNORE_EINTR(close(infile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900912 result = false;
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900913 if (IGNORE_EINTR(close(outfile)) < 0)
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +0900914 result = false;
915
916 return result;
917}
918#endif // !defined(OS_MACOSX)
919
920} // namespace internal
921} // namespace base