blob: ebf20c7c99fc8f8f1e59c2388f8a3c5989f2bc0b [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
kjellandere96c45b2017-06-30 10:45:21 -070011#include "webrtc/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>
kjellandere96c45b2017-06-30 10:45:21 -070023#include "webrtc/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
kjellandere96c45b2017-06-30 10:45:21 -070047#include "webrtc/rtc_base/arraysize.h"
48#include "webrtc/rtc_base/checks.h"
49#include "webrtc/rtc_base/fileutils.h"
50#include "webrtc/rtc_base/pathutils.h"
51#include "webrtc/rtc_base/stream.h"
52#include "webrtc/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
60bool UnixFilesystem::CreateFolder(const Pathname &path, mode_t mode) {
61 std::string pathname(path.pathname());
62 int len = pathname.length();
63 if ((len == 0) || (pathname[len - 1] != '/'))
64 return false;
65
66 struct stat st;
67 int res = ::stat(pathname.c_str(), &st);
68 if (res == 0) {
69 // Something exists at this location, check if it is a directory
70 return S_ISDIR(st.st_mode) != 0;
71 } else if (errno != ENOENT) {
72 // Unexpected error
73 return false;
74 }
75
76 // Directory doesn't exist, look up one directory level
77 do {
78 --len;
79 } while ((len > 0) && (pathname[len - 1] != '/'));
80
81 if (!CreateFolder(Pathname(pathname.substr(0, len)), mode)) {
82 return false;
83 }
84
85 LOG(LS_INFO) << "Creating folder: " << pathname;
86 return (0 == ::mkdir(pathname.c_str(), mode));
87}
88
89bool UnixFilesystem::CreateFolder(const Pathname &path) {
90 return CreateFolder(path, 0755);
91}
92
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093bool UnixFilesystem::DeleteFile(const Pathname &filename) {
94 LOG(LS_INFO) << "Deleting file:" << filename.pathname();
95
96 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -080097 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 return false;
99 }
100 return ::unlink(filename.pathname().c_str()) == 0;
101}
102
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103std::string UnixFilesystem::TempFilename(const Pathname &dir,
104 const std::string &prefix) {
105 int len = dir.pathname().size() + prefix.size() + 2 + 6;
106 char *tempname = new char[len];
107
108 snprintf(tempname, len, "%s/%sXXXXXX", dir.pathname().c_str(),
109 prefix.c_str());
110 int fd = ::mkstemp(tempname);
111 if (fd != -1)
112 ::close(fd);
113 std::string ret(tempname);
114 delete[] tempname;
115
116 return ret;
117}
118
119bool UnixFilesystem::MoveFile(const Pathname &old_path,
120 const Pathname &new_path) {
121 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -0800122 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 return false;
124 }
125 LOG(LS_VERBOSE) << "Moving " << old_path.pathname()
126 << " to " << new_path.pathname();
127 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -0700128 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 }
130 return true;
131}
132
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133bool UnixFilesystem::IsFolder(const Pathname &path) {
134 struct stat st;
135 if (stat(path.pathname().c_str(), &st) < 0)
136 return false;
137 return S_ISDIR(st.st_mode);
138}
139
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140bool UnixFilesystem::IsFile(const Pathname& pathname) {
141 struct stat st;
142 int res = ::stat(pathname.pathname().c_str(), &st);
143 // Treat symlinks, named pipes, etc. all as files.
144 return res == 0 && !S_ISDIR(st.st_mode);
145}
146
147bool UnixFilesystem::IsAbsent(const Pathname& pathname) {
148 struct stat st;
149 int res = ::stat(pathname.pathname().c_str(), &st);
150 // Note: we specifically maintain ENOTDIR as an error, because that implies
151 // that you could not call CreateFolder(pathname).
152 return res != 0 && ENOENT == errno;
153}
154
155bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) {
156 struct stat st;
157 if (::stat(pathname.pathname().c_str(), &st) != 0)
158 return false;
159 *size = st.st_size;
160 return true;
161}
162
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163char* UnixFilesystem::CopyString(const std::string& str) {
164 size_t size = str.length() + 1;
165
166 char* buf = new char[size];
167 if (!buf) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800168 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169 }
170
171 strcpyn(buf, size, str.c_str());
172 return buf;
173}
174
175} // namespace rtc
176
177#if defined(__native_client__)
178extern "C" int __attribute__((weak))
179link(const char* oldpath, const char* newpath) {
180 errno = EACCES;
181 return -1;
182}
183#endif