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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/unixfilesystem.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include <sys/stat.h> |
| 14 | #include <unistd.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
| 17 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 18 | #include <CoreServices/CoreServices.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | #include <IOKit/IOCFBundle.h> |
| 20 | #include <sys/statvfs.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 21 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/macutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) |
| 24 | |
| 25 | #if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 26 | #include <sys/types.h> |
| 27 | #if defined(WEBRTC_ANDROID) |
| 28 | #include <sys/statfs.h> |
| 29 | #elif !defined(__native_client__) |
| 30 | #include <sys/statvfs.h> |
| 31 | #endif // !defined(__native_client__) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #endif // WEBRTC_POSIX && !WEBRTC_MAC || WEBRTC_IOS |
| 34 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | #if defined(__native_client__) && !defined(__GLIBC__) |
| 36 | #include <sys/syslimits.h> |
| 37 | #endif |
| 38 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "rtc_base/checks.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 40 | #include "rtc_base/logging.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | namespace rtc { |
| 43 | |
nisse | a0c8887 | 2017-08-24 02:20:46 -0700 | [diff] [blame] | 44 | UnixFilesystem::UnixFilesystem() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | |
| 46 | UnixFilesystem::~UnixFilesystem() {} |
| 47 | |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 48 | bool UnixFilesystem::DeleteFile(const std::string& filename) { |
| 49 | RTC_LOG(LS_INFO) << "Deleting file:" << filename; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | |
| 51 | if (!IsFile(filename)) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 52 | RTC_DCHECK(IsFile(filename)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 55 | return ::unlink(filename.c_str()) == 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 58 | bool UnixFilesystem::MoveFile(const std::string& old_path, |
| 59 | const std::string& new_path) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | if (!IsFile(old_path)) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 61 | RTC_DCHECK(IsFile(old_path)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 64 | RTC_LOG(LS_VERBOSE) << "Moving " << old_path << " to " << new_path; |
| 65 | if (rename(old_path.c_str(), new_path.c_str()) != 0) { |
nisse | 7b3ce5b | 2017-03-23 10:54:16 -0700 | [diff] [blame] | 66 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 71 | bool UnixFilesystem::IsFile(const std::string& pathname) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | struct stat st; |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 73 | int res = ::stat(pathname.c_str(), &st); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | // Treat symlinks, named pipes, etc. all as files. |
| 75 | return res == 0 && !S_ISDIR(st.st_mode); |
| 76 | } |
| 77 | |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 78 | bool UnixFilesystem::GetFileSize(const std::string& pathname, size_t* size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | struct stat st; |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 80 | if (::stat(pathname.c_str(), &st) != 0) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | return false; |
| 82 | *size = st.st_size; |
| 83 | return true; |
| 84 | } |
| 85 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | } // namespace rtc |
| 87 | |
| 88 | #if defined(__native_client__) |
| 89 | extern "C" int __attribute__((weak)) |
| 90 | link(const char* oldpath, const char* newpath) { |
| 91 | errno = EACCES; |
| 92 | return -1; |
| 93 | } |
| 94 | #endif |