blob: 818cb8a9d730dc37bafdebe4bd660e21e46761f9 [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
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)
kthelgasond5472242016-09-09 03:19:48 -070020#include <CoreServices/CoreServices.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#include <IOKit/IOCFBundle.h>
22#include <sys/statvfs.h>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/macutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#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 Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/arraysize.h"
48#include "rtc_base/checks.h"
49#include "rtc_base/fileutils.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020050#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "rtc_base/pathutils.h"
52#include "rtc_base/stream.h"
53#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055namespace rtc {
56
nissea0c88872017-08-24 02:20:46 -070057UnixFilesystem::UnixFilesystem() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058
59UnixFilesystem::~UnixFilesystem() {}
60
Yves Gerey665174f2018-06-19 15:03:05 +020061bool UnixFilesystem::DeleteFile(const Pathname& filename) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010062 RTC_LOG(LS_INFO) << "Deleting file:" << filename.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
64 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -080065 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 return false;
67 }
68 return ::unlink(filename.pathname().c_str()) == 0;
69}
70
Yves Gerey665174f2018-06-19 15:03:05 +020071bool UnixFilesystem::MoveFile(const Pathname& old_path,
72 const Pathname& new_path) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -080074 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 return false;
76 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_VERBOSE) << "Moving " << old_path.pathname() << " to "
78 << new_path.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -070080 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 }
82 return true;
83}
84
Yves Gerey665174f2018-06-19 15:03:05 +020085bool UnixFilesystem::IsFolder(const Pathname& path) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 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.orgf0488722014-05-13 18:00:26 +000092bool 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 Gerey665174f2018-06-19 15:03:05 +020099bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t* size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 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.orgf0488722014-05-13 18:00:26 +0000107} // namespace rtc
108
109#if defined(__native_client__)
110extern "C" int __attribute__((weak))
111link(const char* oldpath, const char* newpath) {
112 errno = EACCES;
113 return -1;
114}
115#endif