henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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) |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 20 | #include <CoreServices/CoreServices.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | #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 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 47 | #include "webrtc/base/arraysize.h" |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 48 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | #include "webrtc/base/fileutils.h" |
| 50 | #include "webrtc/base/pathutils.h" |
| 51 | #include "webrtc/base/stream.h" |
| 52 | #include "webrtc/base/stringutils.h" |
| 53 | |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 54 | #if defined(WEBRTC_MAC) |
| 55 | // Defined in applefilesystem.mm. No header file to discourage use |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | // elsewhere; other places should use GetApp{Data,Temp}Folder() in |
| 57 | // this file. Don't copy/paste. I mean it. |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 58 | char* AppleDataDirectory(); |
| 59 | char* AppleTempDirectory(); |
| 60 | void AppleAppName(rtc::Pathname* path); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
| 63 | namespace rtc { |
| 64 | |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 65 | #if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_MAC) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 66 | char* UnixFilesystem::app_temp_path_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | #else |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 68 | char* UnixFilesystem::provided_app_data_folder_ = nullptr; |
| 69 | char* UnixFilesystem::provided_app_temp_folder_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | |
| 71 | void UnixFilesystem::SetAppDataFolder(const std::string& folder) { |
| 72 | delete [] provided_app_data_folder_; |
| 73 | provided_app_data_folder_ = CopyString(folder); |
| 74 | } |
| 75 | |
| 76 | void UnixFilesystem::SetAppTempFolder(const std::string& folder) { |
| 77 | delete [] provided_app_temp_folder_; |
| 78 | provided_app_temp_folder_ = CopyString(folder); |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | UnixFilesystem::UnixFilesystem() { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 83 | #if defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | if (!provided_app_data_folder_) |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 85 | provided_app_data_folder_ = AppleDataDirectory(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | if (!provided_app_temp_folder_) |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 87 | provided_app_temp_folder_ = AppleTempDirectory(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | #endif |
| 89 | } |
| 90 | |
| 91 | UnixFilesystem::~UnixFilesystem() {} |
| 92 | |
| 93 | bool 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 | |
| 122 | bool UnixFilesystem::CreateFolder(const Pathname &path) { |
| 123 | return CreateFolder(path, 0755); |
| 124 | } |
| 125 | |
| 126 | FileStream *UnixFilesystem::OpenFile(const Pathname &filename, |
| 127 | const std::string &mode) { |
| 128 | FileStream *fs = new FileStream(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 129 | if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), nullptr)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | delete fs; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 131 | fs = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | } |
| 133 | return fs; |
| 134 | } |
| 135 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | bool UnixFilesystem::DeleteFile(const Pathname &filename) { |
| 137 | LOG(LS_INFO) << "Deleting file:" << filename.pathname(); |
| 138 | |
| 139 | if (!IsFile(filename)) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 140 | RTC_DCHECK(IsFile(filename)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | return ::unlink(filename.pathname().c_str()) == 0; |
| 144 | } |
| 145 | |
| 146 | bool UnixFilesystem::DeleteEmptyFolder(const Pathname &folder) { |
| 147 | LOG(LS_INFO) << "Deleting folder" << folder.pathname(); |
| 148 | |
| 149 | if (!IsFolder(folder)) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 150 | RTC_DCHECK(IsFolder(folder)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | 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 | |
| 157 | bool UnixFilesystem::GetTemporaryFolder(Pathname &pathname, bool create, |
| 158 | const std::string *append) { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 159 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 160 | RTC_DCHECK(provided_app_temp_folder_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 161 | pathname.SetPathname(provided_app_temp_folder_, ""); |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 162 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 163 | 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 | } |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 174 | #endif // defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | if (append) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 176 | RTC_DCHECK(!append->empty()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | pathname.AppendFolder(*append); |
| 178 | } |
| 179 | return !create || CreateFolder(pathname); |
| 180 | } |
| 181 | |
| 182 | std::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 | |
| 198 | bool UnixFilesystem::MoveFile(const Pathname &old_path, |
| 199 | const Pathname &new_path) { |
| 200 | if (!IsFile(old_path)) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 201 | RTC_DCHECK(IsFile(old_path)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | 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) { |
| 207 | if (errno != EXDEV) |
| 208 | return false; |
| 209 | if (!CopyFile(old_path, new_path)) |
| 210 | return false; |
| 211 | if (!DeleteFile(old_path)) |
| 212 | return false; |
| 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 217 | bool UnixFilesystem::IsFolder(const Pathname &path) { |
| 218 | struct stat st; |
| 219 | if (stat(path.pathname().c_str(), &st) < 0) |
| 220 | return false; |
| 221 | return S_ISDIR(st.st_mode); |
| 222 | } |
| 223 | |
| 224 | bool UnixFilesystem::CopyFile(const Pathname &old_path, |
| 225 | const Pathname &new_path) { |
| 226 | LOG(LS_VERBOSE) << "Copying " << old_path.pathname() |
| 227 | << " to " << new_path.pathname(); |
| 228 | char buf[256]; |
| 229 | size_t len; |
| 230 | |
| 231 | StreamInterface *source = OpenFile(old_path, "rb"); |
| 232 | if (!source) |
| 233 | return false; |
| 234 | |
| 235 | StreamInterface *dest = OpenFile(new_path, "wb"); |
| 236 | if (!dest) { |
| 237 | delete source; |
| 238 | return false; |
| 239 | } |
| 240 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 241 | while (source->Read(buf, sizeof(buf), &len, nullptr) == SR_SUCCESS) |
| 242 | dest->Write(buf, len, nullptr, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 243 | |
| 244 | delete source; |
| 245 | delete dest; |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | bool UnixFilesystem::IsTemporaryPath(const Pathname& pathname) { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 250 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 251 | RTC_DCHECK(provided_app_temp_folder_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 252 | #endif |
| 253 | |
| 254 | const char* const kTempPrefixes[] = { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 255 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | provided_app_temp_folder_, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 257 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 258 | "/private/tmp/", "/private/var/tmp/", "/private/var/folders/", |
| 259 | #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 260 | #else |
| 261 | "/tmp/", "/var/tmp/", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 262 | #endif // WEBRTC_ANDROID || WEBRTC_IOS |
| 263 | }; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 264 | for (size_t i = 0; i < arraysize(kTempPrefixes); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 265 | if (0 == strncmp(pathname.pathname().c_str(), kTempPrefixes[i], |
| 266 | strlen(kTempPrefixes[i]))) |
| 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | bool UnixFilesystem::IsFile(const Pathname& pathname) { |
| 273 | struct stat st; |
| 274 | int res = ::stat(pathname.pathname().c_str(), &st); |
| 275 | // Treat symlinks, named pipes, etc. all as files. |
| 276 | return res == 0 && !S_ISDIR(st.st_mode); |
| 277 | } |
| 278 | |
| 279 | bool UnixFilesystem::IsAbsent(const Pathname& pathname) { |
| 280 | struct stat st; |
| 281 | int res = ::stat(pathname.pathname().c_str(), &st); |
| 282 | // Note: we specifically maintain ENOTDIR as an error, because that implies |
| 283 | // that you could not call CreateFolder(pathname). |
| 284 | return res != 0 && ENOENT == errno; |
| 285 | } |
| 286 | |
| 287 | bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) { |
| 288 | struct stat st; |
| 289 | if (::stat(pathname.pathname().c_str(), &st) != 0) |
| 290 | return false; |
| 291 | *size = st.st_size; |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool UnixFilesystem::GetFileTime(const Pathname& path, FileTimeType which, |
| 296 | time_t* time) { |
| 297 | struct stat st; |
| 298 | if (::stat(path.pathname().c_str(), &st) != 0) |
| 299 | return false; |
| 300 | switch (which) { |
| 301 | case FTT_CREATED: |
| 302 | *time = st.st_ctime; |
| 303 | break; |
| 304 | case FTT_MODIFIED: |
| 305 | *time = st.st_mtime; |
| 306 | break; |
| 307 | case FTT_ACCESSED: |
| 308 | *time = st.st_atime; |
| 309 | break; |
| 310 | default: |
| 311 | return false; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 316 | bool UnixFilesystem::GetAppTempFolder(Pathname* path) { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 317 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 318 | RTC_DCHECK(provided_app_temp_folder_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 319 | path->SetPathname(provided_app_temp_folder_); |
| 320 | return true; |
| 321 | #else |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 322 | RTC_DCHECK(!application_name_.empty()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 323 | // TODO: Consider whether we are worried about thread safety. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 324 | if (app_temp_path_ != nullptr && strlen(app_temp_path_) > 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 325 | path->SetPathname(app_temp_path_); |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | // Create a random directory as /tmp/<appname>-<pid>-<timestamp> |
| 330 | char buffer[128]; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 331 | sprintfn(buffer, arraysize(buffer), "-%d-%d", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 332 | static_cast<int>(getpid()), |
| 333 | static_cast<int>(time(0))); |
| 334 | std::string folder(application_name_); |
| 335 | folder.append(buffer); |
| 336 | if (!GetTemporaryFolder(*path, true, &folder)) |
| 337 | return false; |
| 338 | |
| 339 | delete [] app_temp_path_; |
| 340 | app_temp_path_ = CopyString(path->pathname()); |
| 341 | // TODO: atexit(DeleteFolderAndContents(app_temp_path_)); |
| 342 | return true; |
| 343 | #endif |
| 344 | } |
| 345 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 346 | char* UnixFilesystem::CopyString(const std::string& str) { |
| 347 | size_t size = str.length() + 1; |
| 348 | |
| 349 | char* buf = new char[size]; |
| 350 | if (!buf) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 351 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | strcpyn(buf, size, str.c_str()); |
| 355 | return buf; |
| 356 | } |
| 357 | |
| 358 | } // namespace rtc |
| 359 | |
| 360 | #if defined(__native_client__) |
| 361 | extern "C" int __attribute__((weak)) |
| 362 | link(const char* oldpath, const char* newpath) { |
| 363 | errno = EACCES; |
| 364 | return -1; |
| 365 | } |
| 366 | #endif |