henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/unixfilesystem.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 10:19:48 | [diff] [blame] | 20 | #include <CoreServices/CoreServices.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 21 | #include <IOKit/IOCFBundle.h> |
| 22 | #include <sys/statvfs.h> |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "rtc_base/macutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 47 | #include "rtc_base/arraysize.h" |
| 48 | #include "rtc_base/checks.h" |
| 49 | #include "rtc_base/fileutils.h" |
| 50 | #include "rtc_base/pathutils.h" |
| 51 | #include "rtc_base/stream.h" |
| 52 | #include "rtc_base/stringutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 53 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 54 | namespace rtc { |
| 55 | |
nisse | a0c8887 | 2017-08-24 09:20:46 | [diff] [blame] | 56 | UnixFilesystem::UnixFilesystem() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 57 | |
| 58 | UnixFilesystem::~UnixFilesystem() {} |
| 59 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 60 | bool UnixFilesystem::DeleteFile(const Pathname &filename) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 61 | RTC_LOG(LS_INFO) << "Deleting file:" << filename.pathname(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 62 | |
| 63 | if (!IsFile(filename)) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 64 | RTC_DCHECK(IsFile(filename)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | return ::unlink(filename.pathname().c_str()) == 0; |
| 68 | } |
| 69 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 70 | bool UnixFilesystem::MoveFile(const Pathname &old_path, |
| 71 | const Pathname &new_path) { |
| 72 | if (!IsFile(old_path)) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 73 | RTC_DCHECK(IsFile(old_path)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 74 | return false; |
| 75 | } |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 76 | RTC_LOG(LS_VERBOSE) << "Moving " << old_path.pathname() << " to " |
| 77 | << new_path.pathname(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 78 | if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) { |
nisse | 7b3ce5b | 2017-03-23 17:54:16 | [diff] [blame] | 79 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 84 | bool UnixFilesystem::IsFolder(const Pathname &path) { |
| 85 | struct stat st; |
| 86 | if (stat(path.pathname().c_str(), &st) < 0) |
| 87 | return false; |
| 88 | return S_ISDIR(st.st_mode); |
| 89 | } |
| 90 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 91 | bool UnixFilesystem::IsFile(const Pathname& pathname) { |
| 92 | struct stat st; |
| 93 | int res = ::stat(pathname.pathname().c_str(), &st); |
| 94 | // Treat symlinks, named pipes, etc. all as files. |
| 95 | return res == 0 && !S_ISDIR(st.st_mode); |
| 96 | } |
| 97 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 98 | bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) { |
| 99 | struct stat st; |
| 100 | if (::stat(pathname.pathname().c_str(), &st) != 0) |
| 101 | return false; |
| 102 | *size = st.st_size; |
| 103 | return true; |
| 104 | } |
| 105 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 106 | } // namespace rtc |
| 107 | |
| 108 | #if defined(__native_client__) |
| 109 | extern "C" int __attribute__((weak)) |
| 110 | link(const char* oldpath, const char* newpath) { |
| 111 | errno = EACCES; |
| 112 | return -1; |
| 113 | } |
| 114 | #endif |