blob: a48aca1a33d24b9d83cfa03535d8a14233053339 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#include "rtc_base/unixfilesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
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 10:19:4820#include <CoreServices/CoreServices.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2621#include <IOKit/IOCFBundle.h>
22#include <sys/statvfs.h>
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/macutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2624#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 04:47:3147#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:2653
henrike@webrtc.orgf0488722014-05-13 18:00:2654namespace rtc {
55
nissea0c88872017-08-24 09:20:4656UnixFilesystem::UnixFilesystem() {}
henrike@webrtc.orgf0488722014-05-13 18:00:2657
58UnixFilesystem::~UnixFilesystem() {}
59
henrike@webrtc.orgf0488722014-05-13 18:00:2660bool UnixFilesystem::DeleteFile(const Pathname &filename) {
Mirko Bonadei675513b2017-11-09 10:09:2561 RTC_LOG(LS_INFO) << "Deleting file:" << filename.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:2662
63 if (!IsFile(filename)) {
nisseede5da42017-01-12 13:15:3664 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:2665 return false;
66 }
67 return ::unlink(filename.pathname().c_str()) == 0;
68}
69
henrike@webrtc.orgf0488722014-05-13 18:00:2670bool UnixFilesystem::MoveFile(const Pathname &old_path,
71 const Pathname &new_path) {
72 if (!IsFile(old_path)) {
nisseede5da42017-01-12 13:15:3673 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:2674 return false;
75 }
Mirko Bonadei675513b2017-11-09 10:09:2576 RTC_LOG(LS_VERBOSE) << "Moving " << old_path.pathname() << " to "
77 << new_path.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:2678 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 17:54:1679 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:2680 }
81 return true;
82}
83
henrike@webrtc.orgf0488722014-05-13 18:00:2684bool UnixFilesystem::IsFolder(const Pathname &path) {
85 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:2691bool 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
henrike@webrtc.orgf0488722014-05-13 18:00:2698bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) {
99 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:26106} // 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