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