blob: ee9e3f069a5b954f039228c12d576347af7e4c8d [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"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042namespace rtc {
43
nissea0c88872017-08-24 02:20:46 -070044UnixFilesystem::UnixFilesystem() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045
46UnixFilesystem::~UnixFilesystem() {}
47
Niels Möller6b9dec02018-11-02 08:50:21 +010048bool UnixFilesystem::DeleteFile(const std::string& filename) {
49 RTC_LOG(LS_INFO) << "Deleting file:" << filename;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -080052 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053 return false;
54 }
Niels Möller6b9dec02018-11-02 08:50:21 +010055 return ::unlink(filename.c_str()) == 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056}
57
Niels Möller6b9dec02018-11-02 08:50:21 +010058bool UnixFilesystem::MoveFile(const std::string& old_path,
59 const std::string& new_path) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -080061 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 return false;
63 }
Niels Möller6b9dec02018-11-02 08:50:21 +010064 RTC_LOG(LS_VERBOSE) << "Moving " << old_path << " to " << new_path;
65 if (rename(old_path.c_str(), new_path.c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -070066 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 }
68 return true;
69}
70
Niels Möller6b9dec02018-11-02 08:50:21 +010071bool UnixFilesystem::IsFile(const std::string& pathname) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 struct stat st;
Niels Möller6b9dec02018-11-02 08:50:21 +010073 int res = ::stat(pathname.c_str(), &st);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 // Treat symlinks, named pipes, etc. all as files.
75 return res == 0 && !S_ISDIR(st.st_mode);
76}
77
Niels Möller6b9dec02018-11-02 08:50:21 +010078bool UnixFilesystem::GetFileSize(const std::string& pathname, size_t* size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 struct stat st;
Niels Möller6b9dec02018-11-02 08:50:21 +010080 if (::stat(pathname.c_str(), &st) != 0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 return false;
82 *size = st.st_size;
83 return true;
84}
85
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086} // namespace rtc
87
88#if defined(__native_client__)
89extern "C" int __attribute__((weak))
90link(const char* oldpath, const char* newpath) {
91 errno = EACCES;
92 return -1;
93}
94#endif