blob: 557c478774db6d38490f9e0de81c3ef7b9a75504 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/base/unixfilesystem.h"
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdlib.h>
16#include <sys/stat.h>
17#include <unistd.h>
18
19#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
kthelgasond5472242016-09-09 03:19:48 -070020#include <CoreServices/CoreServices.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#include <IOKit/IOCFBundle.h>
22#include <sys/statvfs.h>
23#include "webrtc/base/macutils.h"
24#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
25
26#if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
27#include <sys/types.h>
28#if defined(WEBRTC_ANDROID)
29#include <sys/statfs.h>
30#elif !defined(__native_client__)
31#include <sys/statvfs.h>
32#endif // !defined(__native_client__)
33#include <limits.h>
34#include <pwd.h>
35#include <stdio.h>
36#endif // WEBRTC_POSIX && !WEBRTC_MAC || WEBRTC_IOS
37
38#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
39#include <ctype.h>
40#include <algorithm>
41#endif
42
43#if defined(__native_client__) && !defined(__GLIBC__)
44#include <sys/syslimits.h>
45#endif
46
tfarina5237aaf2015-11-10 23:44:30 -080047#include "webrtc/base/arraysize.h"
nisseede5da42017-01-12 05:15:36 -080048#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049#include "webrtc/base/fileutils.h"
50#include "webrtc/base/pathutils.h"
51#include "webrtc/base/stream.h"
52#include "webrtc/base/stringutils.h"
53
kthelgasond5472242016-09-09 03:19:48 -070054#if defined(WEBRTC_MAC)
55// Defined in applefilesystem.mm. No header file to discourage use
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056// elsewhere; other places should use GetApp{Data,Temp}Folder() in
57// this file. Don't copy/paste. I mean it.
kthelgasond5472242016-09-09 03:19:48 -070058char* AppleDataDirectory();
59char* AppleTempDirectory();
60void AppleAppName(rtc::Pathname* path);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061#endif
62
63namespace rtc {
64
erikchen2ca8d5c2016-10-05 16:04:33 -070065#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_MAC)
deadbeef37f5ecf2017-02-27 14:06:41 -080066char* UnixFilesystem::app_temp_path_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067#else
deadbeef37f5ecf2017-02-27 14:06:41 -080068char* UnixFilesystem::provided_app_data_folder_ = nullptr;
69char* UnixFilesystem::provided_app_temp_folder_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070
71void UnixFilesystem::SetAppDataFolder(const std::string& folder) {
72 delete [] provided_app_data_folder_;
73 provided_app_data_folder_ = CopyString(folder);
74}
75
76void UnixFilesystem::SetAppTempFolder(const std::string& folder) {
77 delete [] provided_app_temp_folder_;
78 provided_app_temp_folder_ = CopyString(folder);
79}
80#endif
81
82UnixFilesystem::UnixFilesystem() {
erikchen2ca8d5c2016-10-05 16:04:33 -070083#if defined(WEBRTC_MAC)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084 if (!provided_app_data_folder_)
kthelgasond5472242016-09-09 03:19:48 -070085 provided_app_data_folder_ = AppleDataDirectory();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 if (!provided_app_temp_folder_)
kthelgasond5472242016-09-09 03:19:48 -070087 provided_app_temp_folder_ = AppleTempDirectory();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088#endif
89}
90
91UnixFilesystem::~UnixFilesystem() {}
92
93bool UnixFilesystem::CreateFolder(const Pathname &path, mode_t mode) {
94 std::string pathname(path.pathname());
95 int len = pathname.length();
96 if ((len == 0) || (pathname[len - 1] != '/'))
97 return false;
98
99 struct stat st;
100 int res = ::stat(pathname.c_str(), &st);
101 if (res == 0) {
102 // Something exists at this location, check if it is a directory
103 return S_ISDIR(st.st_mode) != 0;
104 } else if (errno != ENOENT) {
105 // Unexpected error
106 return false;
107 }
108
109 // Directory doesn't exist, look up one directory level
110 do {
111 --len;
112 } while ((len > 0) && (pathname[len - 1] != '/'));
113
114 if (!CreateFolder(Pathname(pathname.substr(0, len)), mode)) {
115 return false;
116 }
117
118 LOG(LS_INFO) << "Creating folder: " << pathname;
119 return (0 == ::mkdir(pathname.c_str(), mode));
120}
121
122bool UnixFilesystem::CreateFolder(const Pathname &path) {
123 return CreateFolder(path, 0755);
124}
125
126FileStream *UnixFilesystem::OpenFile(const Pathname &filename,
127 const std::string &mode) {
128 FileStream *fs = new FileStream();
deadbeef37f5ecf2017-02-27 14:06:41 -0800129 if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), nullptr)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 delete fs;
deadbeef37f5ecf2017-02-27 14:06:41 -0800131 fs = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132 }
133 return fs;
134}
135
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136bool UnixFilesystem::DeleteFile(const Pathname &filename) {
137 LOG(LS_INFO) << "Deleting file:" << filename.pathname();
138
139 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -0800140 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141 return false;
142 }
143 return ::unlink(filename.pathname().c_str()) == 0;
144}
145
146bool UnixFilesystem::DeleteEmptyFolder(const Pathname &folder) {
147 LOG(LS_INFO) << "Deleting folder" << folder.pathname();
148
149 if (!IsFolder(folder)) {
nisseede5da42017-01-12 05:15:36 -0800150 RTC_DCHECK(IsFolder(folder));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 return false;
152 }
153 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1);
154 return ::rmdir(no_slash.c_str()) == 0;
155}
156
157bool UnixFilesystem::GetTemporaryFolder(Pathname &pathname, bool create,
158 const std::string *append) {
erikchen2ca8d5c2016-10-05 16:04:33 -0700159#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
deadbeef37f5ecf2017-02-27 14:06:41 -0800160 RTC_DCHECK(provided_app_temp_folder_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161 pathname.SetPathname(provided_app_temp_folder_, "");
kthelgasond5472242016-09-09 03:19:48 -0700162#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163 if (const char* tmpdir = getenv("TMPDIR")) {
164 pathname.SetPathname(tmpdir, "");
165 } else if (const char* tmp = getenv("TMP")) {
166 pathname.SetPathname(tmp, "");
167 } else {
168#ifdef P_tmpdir
169 pathname.SetPathname(P_tmpdir, "");
170#else // !P_tmpdir
171 pathname.SetPathname("/tmp/", "");
172#endif // !P_tmpdir
173 }
kthelgasond5472242016-09-09 03:19:48 -0700174#endif // defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 if (append) {
nisseede5da42017-01-12 05:15:36 -0800176 RTC_DCHECK(!append->empty());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 pathname.AppendFolder(*append);
178 }
179 return !create || CreateFolder(pathname);
180}
181
182std::string UnixFilesystem::TempFilename(const Pathname &dir,
183 const std::string &prefix) {
184 int len = dir.pathname().size() + prefix.size() + 2 + 6;
185 char *tempname = new char[len];
186
187 snprintf(tempname, len, "%s/%sXXXXXX", dir.pathname().c_str(),
188 prefix.c_str());
189 int fd = ::mkstemp(tempname);
190 if (fd != -1)
191 ::close(fd);
192 std::string ret(tempname);
193 delete[] tempname;
194
195 return ret;
196}
197
198bool UnixFilesystem::MoveFile(const Pathname &old_path,
199 const Pathname &new_path) {
200 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -0800201 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202 return false;
203 }
204 LOG(LS_VERBOSE) << "Moving " << old_path.pathname()
205 << " to " << new_path.pathname();
206 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -0700207 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000208 }
209 return true;
210}
211
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212bool UnixFilesystem::IsFolder(const Pathname &path) {
213 struct stat st;
214 if (stat(path.pathname().c_str(), &st) < 0)
215 return false;
216 return S_ISDIR(st.st_mode);
217}
218
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219bool UnixFilesystem::IsTemporaryPath(const Pathname& pathname) {
erikchen2ca8d5c2016-10-05 16:04:33 -0700220#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
deadbeef37f5ecf2017-02-27 14:06:41 -0800221 RTC_DCHECK(provided_app_temp_folder_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222#endif
223
224 const char* const kTempPrefixes[] = {
erikchen2ca8d5c2016-10-05 16:04:33 -0700225#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226 provided_app_temp_folder_,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
228 "/private/tmp/", "/private/var/tmp/", "/private/var/folders/",
229#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
erikchen2ca8d5c2016-10-05 16:04:33 -0700230#else
231 "/tmp/", "/var/tmp/",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232#endif // WEBRTC_ANDROID || WEBRTC_IOS
233 };
tfarina5237aaf2015-11-10 23:44:30 -0800234 for (size_t i = 0; i < arraysize(kTempPrefixes); ++i) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 if (0 == strncmp(pathname.pathname().c_str(), kTempPrefixes[i],
236 strlen(kTempPrefixes[i])))
237 return true;
238 }
239 return false;
240}
241
242bool UnixFilesystem::IsFile(const Pathname& pathname) {
243 struct stat st;
244 int res = ::stat(pathname.pathname().c_str(), &st);
245 // Treat symlinks, named pipes, etc. all as files.
246 return res == 0 && !S_ISDIR(st.st_mode);
247}
248
249bool UnixFilesystem::IsAbsent(const Pathname& pathname) {
250 struct stat st;
251 int res = ::stat(pathname.pathname().c_str(), &st);
252 // Note: we specifically maintain ENOTDIR as an error, because that implies
253 // that you could not call CreateFolder(pathname).
254 return res != 0 && ENOENT == errno;
255}
256
257bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) {
258 struct stat st;
259 if (::stat(pathname.pathname().c_str(), &st) != 0)
260 return false;
261 *size = st.st_size;
262 return true;
263}
264
265bool UnixFilesystem::GetFileTime(const Pathname& path, FileTimeType which,
266 time_t* time) {
267 struct stat st;
268 if (::stat(path.pathname().c_str(), &st) != 0)
269 return false;
270 switch (which) {
271 case FTT_CREATED:
272 *time = st.st_ctime;
273 break;
274 case FTT_MODIFIED:
275 *time = st.st_mtime;
276 break;
277 case FTT_ACCESSED:
278 *time = st.st_atime;
279 break;
280 default:
281 return false;
282 }
283 return true;
284}
285
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286bool UnixFilesystem::GetAppTempFolder(Pathname* path) {
erikchen2ca8d5c2016-10-05 16:04:33 -0700287#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
deadbeef37f5ecf2017-02-27 14:06:41 -0800288 RTC_DCHECK(provided_app_temp_folder_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 path->SetPathname(provided_app_temp_folder_);
290 return true;
291#else
nisseede5da42017-01-12 05:15:36 -0800292 RTC_DCHECK(!application_name_.empty());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293 // TODO: Consider whether we are worried about thread safety.
deadbeef37f5ecf2017-02-27 14:06:41 -0800294 if (app_temp_path_ != nullptr && strlen(app_temp_path_) > 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 path->SetPathname(app_temp_path_);
296 return true;
297 }
298
299 // Create a random directory as /tmp/<appname>-<pid>-<timestamp>
300 char buffer[128];
tfarina5237aaf2015-11-10 23:44:30 -0800301 sprintfn(buffer, arraysize(buffer), "-%d-%d",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 static_cast<int>(getpid()),
303 static_cast<int>(time(0)));
304 std::string folder(application_name_);
305 folder.append(buffer);
306 if (!GetTemporaryFolder(*path, true, &folder))
307 return false;
308
309 delete [] app_temp_path_;
310 app_temp_path_ = CopyString(path->pathname());
311 // TODO: atexit(DeleteFolderAndContents(app_temp_path_));
312 return true;
313#endif
314}
315
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316char* UnixFilesystem::CopyString(const std::string& str) {
317 size_t size = str.length() + 1;
318
319 char* buf = new char[size];
320 if (!buf) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800321 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322 }
323
324 strcpyn(buf, size, str.c_str());
325 return buf;
326}
327
328} // namespace rtc
329
330#if defined(__native_client__)
331extern "C" int __attribute__((weak))
332link(const char* oldpath, const char* newpath) {
333 errno = EACCES;
334 return -1;
335}
336#endif