blob: 023c34c8c943f873d8cbfaf125f5d71b89c8530e [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"
50#include "rtc_base/pathutils.h"
51#include "rtc_base/stream.h"
52#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054namespace rtc {
55
nissea0c88872017-08-24 02:20:46 -070056UnixFilesystem::UnixFilesystem() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057
58UnixFilesystem::~UnixFilesystem() {}
59
Yves Gerey665174f2018-06-19 15:03:05 +020060bool UnixFilesystem::DeleteFile(const Pathname& filename) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010061 RTC_LOG(LS_INFO) << "Deleting file:" << filename.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062
63 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -080064 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 return false;
66 }
67 return ::unlink(filename.pathname().c_str()) == 0;
68}
69
Yves Gerey665174f2018-06-19 15:03:05 +020070bool UnixFilesystem::MoveFile(const Pathname& old_path,
71 const Pathname& new_path) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -080073 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 return false;
75 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010076 RTC_LOG(LS_VERBOSE) << "Moving " << old_path.pathname() << " to "
77 << new_path.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -070079 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 }
81 return true;
82}
83
Yves Gerey665174f2018-06-19 15:03:05 +020084bool UnixFilesystem::IsFolder(const Pathname& path) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 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.orgf0488722014-05-13 18:00:26 +000091bool 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
Yves Gerey665174f2018-06-19 15:03:05 +020098bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t* size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 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.orgf0488722014-05-13 18:00:26 +0000106} // namespace rtc
107
108#if defined(__native_client__)
109extern "C" int __attribute__((weak))
110link(const char* oldpath, const char* newpath) {
111 errno = EACCES;
112 return -1;
113}
114#endif