blob: 490141248646807093be3e387a8e4d83b2e9d62b [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
Henrik Kjellander00725112017-06-30 15:14:45 +020011#include "webrtc/base/unixfilesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
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>
Henrik Kjellander00725112017-06-30 15:14:45 +020023#include "webrtc/base/macutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#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
Henrik Kjellander00725112017-06-30 15:14:45 +020047#include "webrtc/base/arraysize.h"
48#include "webrtc/base/checks.h"
49#include "webrtc/base/fileutils.h"
50#include "webrtc/base/pathutils.h"
51#include "webrtc/base/stream.h"
52#include "webrtc/base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053
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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126bool UnixFilesystem::DeleteFile(const Pathname &filename) {
127 LOG(LS_INFO) << "Deleting file:" << filename.pathname();
128
129 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -0800130 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131 return false;
132 }
133 return ::unlink(filename.pathname().c_str()) == 0;
134}
135
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136bool UnixFilesystem::GetTemporaryFolder(Pathname &pathname, bool create,
137 const std::string *append) {
erikchen2ca8d5c2016-10-05 16:04:33 -0700138#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
deadbeef37f5ecf2017-02-27 14:06:41 -0800139 RTC_DCHECK(provided_app_temp_folder_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140 pathname.SetPathname(provided_app_temp_folder_, "");
kthelgasond5472242016-09-09 03:19:48 -0700141#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142 if (const char* tmpdir = getenv("TMPDIR")) {
143 pathname.SetPathname(tmpdir, "");
144 } else if (const char* tmp = getenv("TMP")) {
145 pathname.SetPathname(tmp, "");
146 } else {
147#ifdef P_tmpdir
148 pathname.SetPathname(P_tmpdir, "");
149#else // !P_tmpdir
150 pathname.SetPathname("/tmp/", "");
151#endif // !P_tmpdir
152 }
kthelgasond5472242016-09-09 03:19:48 -0700153#endif // defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 if (append) {
nisseede5da42017-01-12 05:15:36 -0800155 RTC_DCHECK(!append->empty());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000156 pathname.AppendFolder(*append);
157 }
158 return !create || CreateFolder(pathname);
159}
160
161std::string UnixFilesystem::TempFilename(const Pathname &dir,
162 const std::string &prefix) {
163 int len = dir.pathname().size() + prefix.size() + 2 + 6;
164 char *tempname = new char[len];
165
166 snprintf(tempname, len, "%s/%sXXXXXX", dir.pathname().c_str(),
167 prefix.c_str());
168 int fd = ::mkstemp(tempname);
169 if (fd != -1)
170 ::close(fd);
171 std::string ret(tempname);
172 delete[] tempname;
173
174 return ret;
175}
176
177bool UnixFilesystem::MoveFile(const Pathname &old_path,
178 const Pathname &new_path) {
179 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -0800180 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000181 return false;
182 }
183 LOG(LS_VERBOSE) << "Moving " << old_path.pathname()
184 << " to " << new_path.pathname();
185 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -0700186 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 }
188 return true;
189}
190
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191bool UnixFilesystem::IsFolder(const Pathname &path) {
192 struct stat st;
193 if (stat(path.pathname().c_str(), &st) < 0)
194 return false;
195 return S_ISDIR(st.st_mode);
196}
197
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198bool UnixFilesystem::IsFile(const Pathname& pathname) {
199 struct stat st;
200 int res = ::stat(pathname.pathname().c_str(), &st);
201 // Treat symlinks, named pipes, etc. all as files.
202 return res == 0 && !S_ISDIR(st.st_mode);
203}
204
205bool UnixFilesystem::IsAbsent(const Pathname& pathname) {
206 struct stat st;
207 int res = ::stat(pathname.pathname().c_str(), &st);
208 // Note: we specifically maintain ENOTDIR as an error, because that implies
209 // that you could not call CreateFolder(pathname).
210 return res != 0 && ENOENT == errno;
211}
212
213bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) {
214 struct stat st;
215 if (::stat(pathname.pathname().c_str(), &st) != 0)
216 return false;
217 *size = st.st_size;
218 return true;
219}
220
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221char* UnixFilesystem::CopyString(const std::string& str) {
222 size_t size = str.length() + 1;
223
224 char* buf = new char[size];
225 if (!buf) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800226 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227 }
228
229 strcpyn(buf, size, str.c_str());
230 return buf;
231}
232
233} // namespace rtc
234
235#if defined(__native_client__)
236extern "C" int __attribute__((weak))
237link(const char* oldpath, const char* newpath) {
238 errno = EACCES;
239 return -1;
240}
241#endif