blob: 2a941e2b74674dc12901044e043c3c3014260d92 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/unixfilesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include <sys/stat.h>
14#include <unistd.h>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
17#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
kthelgasond5472242016-09-09 03:19:48 -070018#include <CoreServices/CoreServices.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#include <IOKit/IOCFBundle.h>
20#include <sys/statvfs.h>
Yves Gerey988cc082018-10-23 12:03:01 +020021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/macutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023#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.orgf0488722014-05-13 18:00:26 +000032#include <stdio.h>
33#endif // WEBRTC_POSIX && !WEBRTC_MAC || WEBRTC_IOS
34
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035#if defined(__native_client__) && !defined(__GLIBC__)
36#include <sys/syslimits.h>
37#endif
38
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "rtc_base/checks.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020040#include "rtc_base/logging.h"
Qingsi Wang2039ee72018-11-02 16:30:10 +000041#include "rtc_base/pathutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043namespace rtc {
44
nissea0c88872017-08-24 02:20:46 -070045UnixFilesystem::UnixFilesystem() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
47UnixFilesystem::~UnixFilesystem() {}
48
Qingsi Wang2039ee72018-11-02 16:30:10 +000049bool UnixFilesystem::DeleteFile(const Pathname& filename) {
50 RTC_LOG(LS_INFO) << "Deleting file:" << filename.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051
52 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -080053 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054 return false;
55 }
Qingsi Wang2039ee72018-11-02 16:30:10 +000056 return ::unlink(filename.pathname().c_str()) == 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057}
58
Qingsi Wang2039ee72018-11-02 16:30:10 +000059bool UnixFilesystem::MoveFile(const Pathname& old_path,
60 const Pathname& new_path) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -080062 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 return false;
64 }
Qingsi Wang2039ee72018-11-02 16:30:10 +000065 RTC_LOG(LS_VERBOSE) << "Moving " << old_path.pathname() << " to "
66 << new_path.pathname();
67 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -070068 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 }
70 return true;
71}
72
Qingsi Wang2039ee72018-11-02 16:30:10 +000073bool UnixFilesystem::IsFolder(const Pathname& path) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 struct stat st;
Qingsi Wang2039ee72018-11-02 16:30:10 +000075 if (stat(path.pathname().c_str(), &st) < 0)
76 return false;
77 return S_ISDIR(st.st_mode);
78}
79
80bool UnixFilesystem::IsFile(const Pathname& pathname) {
81 struct stat st;
82 int res = ::stat(pathname.pathname().c_str(), &st);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 // Treat symlinks, named pipes, etc. all as files.
84 return res == 0 && !S_ISDIR(st.st_mode);
85}
86
Qingsi Wang2039ee72018-11-02 16:30:10 +000087bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t* size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088 struct stat st;
Qingsi Wang2039ee72018-11-02 16:30:10 +000089 if (::stat(pathname.pathname().c_str(), &st) != 0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090 return false;
91 *size = st.st_size;
92 return true;
93}
94
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095} // namespace rtc
96
97#if defined(__native_client__)
98extern "C" int __attribute__((weak))
99link(const char* oldpath, const char* newpath) {
100 errno = EACCES;
101 return -1;
102}
103#endif