blob: 8732d477a3c4d95b594fe19f15410c2d71e17a29 [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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060bool 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070std::string UnixFilesystem::TempFilename(const Pathname &dir,
71 const std::string &prefix) {
72 int len = dir.pathname().size() + prefix.size() + 2 + 6;
73 char *tempname = new char[len];
74
75 snprintf(tempname, len, "%s/%sXXXXXX", dir.pathname().c_str(),
76 prefix.c_str());
77 int fd = ::mkstemp(tempname);
78 if (fd != -1)
79 ::close(fd);
80 std::string ret(tempname);
81 delete[] tempname;
82
83 return ret;
84}
85
86bool UnixFilesystem::MoveFile(const Pathname &old_path,
87 const Pathname &new_path) {
88 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -080089 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090 return false;
91 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010092 RTC_LOG(LS_VERBOSE) << "Moving " << old_path.pathname() << " to "
93 << new_path.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
nisse7b3ce5b2017-03-23 10:54:16 -070095 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 }
97 return true;
98}
99
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100bool UnixFilesystem::IsFolder(const Pathname &path) {
101 struct stat st;
102 if (stat(path.pathname().c_str(), &st) < 0)
103 return false;
104 return S_ISDIR(st.st_mode);
105}
106
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107bool UnixFilesystem::IsFile(const Pathname& pathname) {
108 struct stat st;
109 int res = ::stat(pathname.pathname().c_str(), &st);
110 // Treat symlinks, named pipes, etc. all as files.
111 return res == 0 && !S_ISDIR(st.st_mode);
112}
113
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) {
115 struct stat st;
116 if (::stat(pathname.pathname().c_str(), &st) != 0)
117 return false;
118 *size = st.st_size;
119 return true;
120}
121
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122char* UnixFilesystem::CopyString(const std::string& str) {
123 size_t size = str.length() + 1;
124
125 char* buf = new char[size];
126 if (!buf) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800127 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 }
129
130 strcpyn(buf, size, str.c_str());
131 return buf;
132}
133
134} // namespace rtc
135
136#if defined(__native_client__)
137extern "C" int __attribute__((weak))
138link(const char* oldpath, const char* newpath) {
139 errno = EACCES;
140 return -1;
141}
142#endif