| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/unixfilesystem.h" | 11 #include "webrtc/base/unixfilesystem.h" |
| 12 | 12 |
| 13 #include <errno.h> | 13 #include <errno.h> |
| 14 #include <fcntl.h> | 14 #include <fcntl.h> |
| 15 #include <stdlib.h> | 15 #include <stdlib.h> |
| 16 #include <sys/stat.h> | 16 #include <sys/stat.h> |
| 17 #include <unistd.h> | 17 #include <unistd.h> |
| 18 | 18 |
| 19 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) | 19 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 20 #include <CoreFoundation/CoreFoundation.h> | 20 #include <Carbon/Carbon.h> |
| 21 #include <IOKit/IOCFBundle.h> | 21 #include <IOKit/IOCFBundle.h> |
| 22 #include <sys/statvfs.h> | 22 #include <sys/statvfs.h> |
| 23 #include "webrtc/base/macutils.h" | 23 #include "webrtc/base/macutils.h" |
| 24 #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) | 24 #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) |
| 25 | 25 |
| 26 #if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) | 26 #if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 27 #include <sys/types.h> | 27 #include <sys/types.h> |
| 28 #if defined(WEBRTC_ANDROID) | 28 #if defined(WEBRTC_ANDROID) |
| 29 #include <sys/statfs.h> | 29 #include <sys/statfs.h> |
| 30 #elif !defined(__native_client__) | 30 #elif !defined(__native_client__) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 43 #if defined(__native_client__) && !defined(__GLIBC__) | 43 #if defined(__native_client__) && !defined(__GLIBC__) |
| 44 #include <sys/syslimits.h> | 44 #include <sys/syslimits.h> |
| 45 #endif | 45 #endif |
| 46 | 46 |
| 47 #include "webrtc/base/arraysize.h" | 47 #include "webrtc/base/arraysize.h" |
| 48 #include "webrtc/base/fileutils.h" | 48 #include "webrtc/base/fileutils.h" |
| 49 #include "webrtc/base/pathutils.h" | 49 #include "webrtc/base/pathutils.h" |
| 50 #include "webrtc/base/stream.h" | 50 #include "webrtc/base/stream.h" |
| 51 #include "webrtc/base/stringutils.h" | 51 #include "webrtc/base/stringutils.h" |
| 52 | 52 |
| 53 #if defined(WEBRTC_MAC) | 53 #if defined(WEBRTC_IOS) |
| 54 // Defined in applefilesystem.mm. No header file to discourage use | 54 // Defined in iosfilesystem.mm. No header file to discourage use |
| 55 // elsewhere; other places should use GetApp{Data,Temp}Folder() in | 55 // elsewhere; other places should use GetApp{Data,Temp}Folder() in |
| 56 // this file. Don't copy/paste. I mean it. | 56 // this file. Don't copy/paste. I mean it. |
| 57 char* AppleDataDirectory(); | 57 char* IOSDataDirectory(); |
| 58 char* AppleTempDirectory(); | 58 char* IOSTempDirectory(); |
| 59 void AppleAppName(rtc::Pathname* path); | 59 void IOSAppName(rtc::Pathname* path); |
| 60 #endif | 60 #endif |
| 61 | 61 |
| 62 namespace rtc { | 62 namespace rtc { |
| 63 | 63 |
| 64 #if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS) | 64 #if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS) |
| 65 char* UnixFilesystem::app_temp_path_ = NULL; | 65 char* UnixFilesystem::app_temp_path_ = NULL; |
| 66 #else | 66 #else |
| 67 char* UnixFilesystem::provided_app_data_folder_ = NULL; | 67 char* UnixFilesystem::provided_app_data_folder_ = NULL; |
| 68 char* UnixFilesystem::provided_app_temp_folder_ = NULL; | 68 char* UnixFilesystem::provided_app_temp_folder_ = NULL; |
| 69 | 69 |
| 70 void UnixFilesystem::SetAppDataFolder(const std::string& folder) { | 70 void UnixFilesystem::SetAppDataFolder(const std::string& folder) { |
| 71 delete [] provided_app_data_folder_; | 71 delete [] provided_app_data_folder_; |
| 72 provided_app_data_folder_ = CopyString(folder); | 72 provided_app_data_folder_ = CopyString(folder); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void UnixFilesystem::SetAppTempFolder(const std::string& folder) { | 75 void UnixFilesystem::SetAppTempFolder(const std::string& folder) { |
| 76 delete [] provided_app_temp_folder_; | 76 delete [] provided_app_temp_folder_; |
| 77 provided_app_temp_folder_ = CopyString(folder); | 77 provided_app_temp_folder_ = CopyString(folder); |
| 78 } | 78 } |
| 79 #endif | 79 #endif |
| 80 | 80 |
| 81 UnixFilesystem::UnixFilesystem() { | 81 UnixFilesystem::UnixFilesystem() { |
| 82 #if defined(WEBRTC_IOS) | 82 #if defined(WEBRTC_IOS) |
| 83 if (!provided_app_data_folder_) | 83 if (!provided_app_data_folder_) |
| 84 provided_app_data_folder_ = AppleDataDirectory(); | 84 provided_app_data_folder_ = IOSDataDirectory(); |
| 85 if (!provided_app_temp_folder_) | 85 if (!provided_app_temp_folder_) |
| 86 provided_app_temp_folder_ = AppleTempDirectory(); | 86 provided_app_temp_folder_ = IOSTempDirectory(); |
| 87 #endif | 87 #endif |
| 88 } | 88 } |
| 89 | 89 |
| 90 UnixFilesystem::~UnixFilesystem() {} | 90 UnixFilesystem::~UnixFilesystem() {} |
| 91 | 91 |
| 92 bool UnixFilesystem::CreateFolder(const Pathname &path, mode_t mode) { | 92 bool UnixFilesystem::CreateFolder(const Pathname &path, mode_t mode) { |
| 93 std::string pathname(path.pathname()); | 93 std::string pathname(path.pathname()); |
| 94 int len = pathname.length(); | 94 int len = pathname.length(); |
| 95 if ((len == 0) || (pathname[len - 1] != '/')) | 95 if ((len == 0) || (pathname[len - 1] != '/')) |
| 96 return false; | 96 return false; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 if (!IsFolder(folder)) { | 164 if (!IsFolder(folder)) { |
| 165 ASSERT(IsFolder(folder)); | 165 ASSERT(IsFolder(folder)); |
| 166 return false; | 166 return false; |
| 167 } | 167 } |
| 168 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1); | 168 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1); |
| 169 return ::rmdir(no_slash.c_str()) == 0; | 169 return ::rmdir(no_slash.c_str()) == 0; |
| 170 } | 170 } |
| 171 | 171 |
| 172 bool UnixFilesystem::GetTemporaryFolder(Pathname &pathname, bool create, | 172 bool UnixFilesystem::GetTemporaryFolder(Pathname &pathname, bool create, |
| 173 const std::string *append) { | 173 const std::string *append) { |
| 174 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) | 174 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 175 FSRef fr; |
| 176 if (0 != FSFindFolder(kOnAppropriateDisk, kTemporaryFolderType, |
| 177 kCreateFolder, &fr)) |
| 178 return false; |
| 179 unsigned char buffer[NAME_MAX+1]; |
| 180 if (0 != FSRefMakePath(&fr, buffer, arraysize(buffer))) |
| 181 return false; |
| 182 pathname.SetPathname(reinterpret_cast<char*>(buffer), ""); |
| 183 #elif defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
| 175 ASSERT(provided_app_temp_folder_ != NULL); | 184 ASSERT(provided_app_temp_folder_ != NULL); |
| 176 pathname.SetPathname(provided_app_temp_folder_, ""); | 185 pathname.SetPathname(provided_app_temp_folder_, ""); |
| 177 #else | 186 #else // !WEBRTC_MAC || WEBRTC_IOS && !WEBRTC_ANDROID |
| 178 if (const char* tmpdir = getenv("TMPDIR")) { | 187 if (const char* tmpdir = getenv("TMPDIR")) { |
| 179 pathname.SetPathname(tmpdir, ""); | 188 pathname.SetPathname(tmpdir, ""); |
| 180 } else if (const char* tmp = getenv("TMP")) { | 189 } else if (const char* tmp = getenv("TMP")) { |
| 181 pathname.SetPathname(tmp, ""); | 190 pathname.SetPathname(tmp, ""); |
| 182 } else { | 191 } else { |
| 183 #ifdef P_tmpdir | 192 #ifdef P_tmpdir |
| 184 pathname.SetPathname(P_tmpdir, ""); | 193 pathname.SetPathname(P_tmpdir, ""); |
| 185 #else // !P_tmpdir | 194 #else // !P_tmpdir |
| 186 pathname.SetPathname("/tmp/", ""); | 195 pathname.SetPathname("/tmp/", ""); |
| 187 #endif // !P_tmpdir | 196 #endif // !P_tmpdir |
| 188 } | 197 } |
| 189 #endif // defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) | 198 #endif // !WEBRTC_MAC || WEBRTC_IOS && !WEBRTC_ANDROID |
| 190 if (append) { | 199 if (append) { |
| 191 ASSERT(!append->empty()); | 200 ASSERT(!append->empty()); |
| 192 pathname.AppendFolder(*append); | 201 pathname.AppendFolder(*append); |
| 193 } | 202 } |
| 194 return !create || CreateFolder(pathname); | 203 return !create || CreateFolder(pathname); |
| 195 } | 204 } |
| 196 | 205 |
| 197 std::string UnixFilesystem::TempFilename(const Pathname &dir, | 206 std::string UnixFilesystem::TempFilename(const Pathname &dir, |
| 198 const std::string &prefix) { | 207 const std::string &prefix) { |
| 199 int len = dir.pathname().size() + prefix.size() + 2 + 6; | 208 int len = dir.pathname().size() + prefix.size() + 2 + 6; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 case FTT_ACCESSED: | 350 case FTT_ACCESSED: |
| 342 *time = st.st_atime; | 351 *time = st.st_atime; |
| 343 break; | 352 break; |
| 344 default: | 353 default: |
| 345 return false; | 354 return false; |
| 346 } | 355 } |
| 347 return true; | 356 return true; |
| 348 } | 357 } |
| 349 | 358 |
| 350 bool UnixFilesystem::GetAppPathname(Pathname* path) { | 359 bool UnixFilesystem::GetAppPathname(Pathname* path) { |
| 351 #if defined(__native_client__) | 360 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 361 ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 362 CFDictionaryRef procinfo = ProcessInformationCopyDictionary(&psn, |
| 363 kProcessDictionaryIncludeAllInformationMask); |
| 364 if (NULL == procinfo) |
| 365 return false; |
| 366 CFStringRef cfpath = (CFStringRef) CFDictionaryGetValue(procinfo, |
| 367 kIOBundleExecutableKey); |
| 368 std::string path8; |
| 369 bool success = ToUtf8(cfpath, &path8); |
| 370 CFRelease(procinfo); |
| 371 if (success) |
| 372 path->SetPathname(path8); |
| 373 return success; |
| 374 #elif defined(__native_client__) |
| 352 return false; | 375 return false; |
| 353 #elif defined(WEBRTC_MAC) | 376 #elif WEBRTC_IOS |
| 354 AppleAppName(path); | 377 IOSAppName(path); |
| 355 return true; | 378 return true; |
| 356 #else // WEBRTC_MAC && !defined(WEBRTC_IOS) | 379 #else // WEBRTC_MAC && !defined(WEBRTC_IOS) |
| 357 char buffer[PATH_MAX + 2]; | 380 char buffer[PATH_MAX + 2]; |
| 358 ssize_t len = readlink("/proc/self/exe", buffer, arraysize(buffer) - 1); | 381 ssize_t len = readlink("/proc/self/exe", buffer, arraysize(buffer) - 1); |
| 359 if ((len <= 0) || (len == PATH_MAX + 1)) | 382 if ((len <= 0) || (len == PATH_MAX + 1)) |
| 360 return false; | 383 return false; |
| 361 buffer[len] = '\0'; | 384 buffer[len] = '\0'; |
| 362 path->SetPathname(buffer); | 385 path->SetPathname(buffer); |
| 363 return true; | 386 return true; |
| 364 #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) | 387 #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 | 565 |
| 543 } // namespace rtc | 566 } // namespace rtc |
| 544 | 567 |
| 545 #if defined(__native_client__) | 568 #if defined(__native_client__) |
| 546 extern "C" int __attribute__((weak)) | 569 extern "C" int __attribute__((weak)) |
| 547 link(const char* oldpath, const char* newpath) { | 570 link(const char* oldpath, const char* newpath) { |
| 548 errno = EACCES; | 571 errno = EACCES; |
| 549 return -1; | 572 return -1; |
| 550 } | 573 } |
| 551 #endif | 574 #endif |
| OLD | NEW |