henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "webrtc/base/unixfilesystem.h" |
| 12 | |
| 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) |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 20 | #include <CoreServices/CoreServices.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | #include <IOKit/IOCFBundle.h> |
| 22 | #include <sys/statvfs.h> |
| 23 | #include "webrtc/base/macutils.h" |
| 24 | #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 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 47 | #include "webrtc/base/arraysize.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | #include "webrtc/base/fileutils.h" |
| 49 | #include "webrtc/base/pathutils.h" |
| 50 | #include "webrtc/base/stream.h" |
| 51 | #include "webrtc/base/stringutils.h" |
| 52 | |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 53 | #if defined(WEBRTC_MAC) |
| 54 | // Defined in applefilesystem.mm. No header file to discourage use |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | // elsewhere; other places should use GetApp{Data,Temp}Folder() in |
| 56 | // this file. Don't copy/paste. I mean it. |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 57 | char* AppleDataDirectory(); |
| 58 | char* AppleTempDirectory(); |
| 59 | void AppleAppName(rtc::Pathname* path); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | namespace rtc { |
| 63 | |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 64 | #if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | char* UnixFilesystem::app_temp_path_ = NULL; |
| 66 | #else |
| 67 | char* UnixFilesystem::provided_app_data_folder_ = NULL; |
| 68 | char* UnixFilesystem::provided_app_temp_folder_ = NULL; |
| 69 | |
| 70 | void UnixFilesystem::SetAppDataFolder(const std::string& folder) { |
| 71 | delete [] provided_app_data_folder_; |
| 72 | provided_app_data_folder_ = CopyString(folder); |
| 73 | } |
| 74 | |
| 75 | void UnixFilesystem::SetAppTempFolder(const std::string& folder) { |
| 76 | delete [] provided_app_temp_folder_; |
| 77 | provided_app_temp_folder_ = CopyString(folder); |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | UnixFilesystem::UnixFilesystem() { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 82 | #if defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | if (!provided_app_data_folder_) |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 84 | provided_app_data_folder_ = AppleDataDirectory(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | if (!provided_app_temp_folder_) |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 86 | provided_app_temp_folder_ = AppleTempDirectory(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | #endif |
| 88 | } |
| 89 | |
| 90 | UnixFilesystem::~UnixFilesystem() {} |
| 91 | |
| 92 | bool UnixFilesystem::CreateFolder(const Pathname &path, mode_t mode) { |
| 93 | std::string pathname(path.pathname()); |
| 94 | int len = pathname.length(); |
| 95 | if ((len == 0) || (pathname[len - 1] != '/')) |
| 96 | return false; |
| 97 | |
| 98 | struct stat st; |
| 99 | int res = ::stat(pathname.c_str(), &st); |
| 100 | if (res == 0) { |
| 101 | // Something exists at this location, check if it is a directory |
| 102 | return S_ISDIR(st.st_mode) != 0; |
| 103 | } else if (errno != ENOENT) { |
| 104 | // Unexpected error |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // Directory doesn't exist, look up one directory level |
| 109 | do { |
| 110 | --len; |
| 111 | } while ((len > 0) && (pathname[len - 1] != '/')); |
| 112 | |
| 113 | if (!CreateFolder(Pathname(pathname.substr(0, len)), mode)) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | LOG(LS_INFO) << "Creating folder: " << pathname; |
| 118 | return (0 == ::mkdir(pathname.c_str(), mode)); |
| 119 | } |
| 120 | |
| 121 | bool UnixFilesystem::CreateFolder(const Pathname &path) { |
| 122 | return CreateFolder(path, 0755); |
| 123 | } |
| 124 | |
| 125 | FileStream *UnixFilesystem::OpenFile(const Pathname &filename, |
| 126 | const std::string &mode) { |
| 127 | FileStream *fs = new FileStream(); |
| 128 | if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), NULL)) { |
| 129 | delete fs; |
| 130 | fs = NULL; |
| 131 | } |
| 132 | return fs; |
| 133 | } |
| 134 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | bool UnixFilesystem::DeleteFile(const Pathname &filename) { |
| 136 | LOG(LS_INFO) << "Deleting file:" << filename.pathname(); |
| 137 | |
| 138 | if (!IsFile(filename)) { |
| 139 | ASSERT(IsFile(filename)); |
| 140 | return false; |
| 141 | } |
| 142 | return ::unlink(filename.pathname().c_str()) == 0; |
| 143 | } |
| 144 | |
| 145 | bool UnixFilesystem::DeleteEmptyFolder(const Pathname &folder) { |
| 146 | LOG(LS_INFO) << "Deleting folder" << folder.pathname(); |
| 147 | |
| 148 | if (!IsFolder(folder)) { |
| 149 | ASSERT(IsFolder(folder)); |
| 150 | return false; |
| 151 | } |
| 152 | std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1); |
| 153 | return ::rmdir(no_slash.c_str()) == 0; |
| 154 | } |
| 155 | |
| 156 | bool UnixFilesystem::GetTemporaryFolder(Pathname &pathname, bool create, |
| 157 | const std::string *append) { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 158 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | ASSERT(provided_app_temp_folder_ != NULL); |
| 160 | pathname.SetPathname(provided_app_temp_folder_, ""); |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 161 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | if (const char* tmpdir = getenv("TMPDIR")) { |
| 163 | pathname.SetPathname(tmpdir, ""); |
| 164 | } else if (const char* tmp = getenv("TMP")) { |
| 165 | pathname.SetPathname(tmp, ""); |
| 166 | } else { |
| 167 | #ifdef P_tmpdir |
| 168 | pathname.SetPathname(P_tmpdir, ""); |
| 169 | #else // !P_tmpdir |
| 170 | pathname.SetPathname("/tmp/", ""); |
| 171 | #endif // !P_tmpdir |
| 172 | } |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 173 | #endif // defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 174 | if (append) { |
| 175 | ASSERT(!append->empty()); |
| 176 | pathname.AppendFolder(*append); |
| 177 | } |
| 178 | return !create || CreateFolder(pathname); |
| 179 | } |
| 180 | |
| 181 | std::string UnixFilesystem::TempFilename(const Pathname &dir, |
| 182 | const std::string &prefix) { |
| 183 | int len = dir.pathname().size() + prefix.size() + 2 + 6; |
| 184 | char *tempname = new char[len]; |
| 185 | |
| 186 | snprintf(tempname, len, "%s/%sXXXXXX", dir.pathname().c_str(), |
| 187 | prefix.c_str()); |
| 188 | int fd = ::mkstemp(tempname); |
| 189 | if (fd != -1) |
| 190 | ::close(fd); |
| 191 | std::string ret(tempname); |
| 192 | delete[] tempname; |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | bool UnixFilesystem::MoveFile(const Pathname &old_path, |
| 198 | const Pathname &new_path) { |
| 199 | if (!IsFile(old_path)) { |
| 200 | ASSERT(IsFile(old_path)); |
| 201 | return false; |
| 202 | } |
| 203 | LOG(LS_VERBOSE) << "Moving " << old_path.pathname() |
| 204 | << " to " << new_path.pathname(); |
| 205 | if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) { |
| 206 | if (errno != EXDEV) |
| 207 | return false; |
| 208 | if (!CopyFile(old_path, new_path)) |
| 209 | return false; |
| 210 | if (!DeleteFile(old_path)) |
| 211 | return false; |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 216 | bool UnixFilesystem::IsFolder(const Pathname &path) { |
| 217 | struct stat st; |
| 218 | if (stat(path.pathname().c_str(), &st) < 0) |
| 219 | return false; |
| 220 | return S_ISDIR(st.st_mode); |
| 221 | } |
| 222 | |
| 223 | bool UnixFilesystem::CopyFile(const Pathname &old_path, |
| 224 | const Pathname &new_path) { |
| 225 | LOG(LS_VERBOSE) << "Copying " << old_path.pathname() |
| 226 | << " to " << new_path.pathname(); |
| 227 | char buf[256]; |
| 228 | size_t len; |
| 229 | |
| 230 | StreamInterface *source = OpenFile(old_path, "rb"); |
| 231 | if (!source) |
| 232 | return false; |
| 233 | |
| 234 | StreamInterface *dest = OpenFile(new_path, "wb"); |
| 235 | if (!dest) { |
| 236 | delete source; |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | while (source->Read(buf, sizeof(buf), &len, NULL) == SR_SUCCESS) |
| 241 | dest->Write(buf, len, NULL, NULL); |
| 242 | |
| 243 | delete source; |
| 244 | delete dest; |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | bool UnixFilesystem::IsTemporaryPath(const Pathname& pathname) { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 249 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 250 | ASSERT(provided_app_temp_folder_ != NULL); |
| 251 | #endif |
| 252 | |
| 253 | const char* const kTempPrefixes[] = { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 254 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 255 | provided_app_temp_folder_, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 257 | "/private/tmp/", "/private/var/tmp/", "/private/var/folders/", |
| 258 | #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 259 | #else |
| 260 | "/tmp/", "/var/tmp/", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 261 | #endif // WEBRTC_ANDROID || WEBRTC_IOS |
| 262 | }; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 263 | for (size_t i = 0; i < arraysize(kTempPrefixes); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 264 | if (0 == strncmp(pathname.pathname().c_str(), kTempPrefixes[i], |
| 265 | strlen(kTempPrefixes[i]))) |
| 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | bool UnixFilesystem::IsFile(const Pathname& pathname) { |
| 272 | struct stat st; |
| 273 | int res = ::stat(pathname.pathname().c_str(), &st); |
| 274 | // Treat symlinks, named pipes, etc. all as files. |
| 275 | return res == 0 && !S_ISDIR(st.st_mode); |
| 276 | } |
| 277 | |
| 278 | bool UnixFilesystem::IsAbsent(const Pathname& pathname) { |
| 279 | struct stat st; |
| 280 | int res = ::stat(pathname.pathname().c_str(), &st); |
| 281 | // Note: we specifically maintain ENOTDIR as an error, because that implies |
| 282 | // that you could not call CreateFolder(pathname). |
| 283 | return res != 0 && ENOENT == errno; |
| 284 | } |
| 285 | |
| 286 | bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) { |
| 287 | struct stat st; |
| 288 | if (::stat(pathname.pathname().c_str(), &st) != 0) |
| 289 | return false; |
| 290 | *size = st.st_size; |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | bool UnixFilesystem::GetFileTime(const Pathname& path, FileTimeType which, |
| 295 | time_t* time) { |
| 296 | struct stat st; |
| 297 | if (::stat(path.pathname().c_str(), &st) != 0) |
| 298 | return false; |
| 299 | switch (which) { |
| 300 | case FTT_CREATED: |
| 301 | *time = st.st_ctime; |
| 302 | break; |
| 303 | case FTT_MODIFIED: |
| 304 | *time = st.st_mtime; |
| 305 | break; |
| 306 | case FTT_ACCESSED: |
| 307 | *time = st.st_atime; |
| 308 | break; |
| 309 | default: |
| 310 | return false; |
| 311 | } |
| 312 | return true; |
| 313 | } |
| 314 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 315 | bool UnixFilesystem::GetAppDataFolder(Pathname* path, bool per_user) { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 316 | // On macOS and iOS, there is no requirement that the path contains the |
| 317 | // organization. |
| 318 | #if !defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 319 | ASSERT(!organization_name_.empty()); |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 320 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 321 | ASSERT(!application_name_.empty()); |
| 322 | |
| 323 | // First get the base directory for app data. |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 324 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 325 | ASSERT(provided_app_data_folder_ != NULL); |
| 326 | path->SetPathname(provided_app_data_folder_, ""); |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 327 | #elif defined(WEBRTC_LINUX) // && !WEBRTC_MAC && !WEBRTC_ANDROID |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 328 | if (per_user) { |
| 329 | // We follow the recommendations in |
| 330 | // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 331 | // It specifies separate directories for data and config files, but |
| 332 | // GetAppDataFolder() does not distinguish. We just return the config dir |
| 333 | // path. |
| 334 | const char* xdg_config_home = getenv("XDG_CONFIG_HOME"); |
| 335 | if (xdg_config_home) { |
| 336 | path->SetPathname(xdg_config_home, ""); |
| 337 | } else { |
| 338 | // XDG says to default to $HOME/.config. We also support falling back to |
| 339 | // other synonyms for HOME if for some reason it is not defined. |
| 340 | const char* homedir; |
| 341 | if (const char* home = getenv("HOME")) { |
| 342 | homedir = home; |
| 343 | } else if (const char* dotdir = getenv("DOTDIR")) { |
| 344 | homedir = dotdir; |
| 345 | } else if (passwd* pw = getpwuid(geteuid())) { |
| 346 | homedir = pw->pw_dir; |
| 347 | } else { |
| 348 | return false; |
| 349 | } |
| 350 | path->SetPathname(homedir, ""); |
| 351 | path->AppendFolder(".config"); |
| 352 | } |
| 353 | } else { |
| 354 | // XDG does not define a standard directory for writable global data. Let's |
| 355 | // just use this. |
| 356 | path->SetPathname("/var/cache/", ""); |
| 357 | } |
| 358 | #endif // !WEBRTC_MAC && !WEBRTC_LINUX |
| 359 | |
| 360 | // Now add on a sub-path for our app. |
| 361 | #if defined(WEBRTC_MAC) || defined(WEBRTC_ANDROID) |
| 362 | path->AppendFolder(organization_name_); |
| 363 | path->AppendFolder(application_name_); |
| 364 | #elif defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
| 365 | // XDG says to use a single directory level, so we concatenate the org and app |
| 366 | // name with a hyphen. We also do the Linuxy thing and convert to all |
| 367 | // lowercase with no spaces. |
| 368 | std::string subdir(organization_name_); |
| 369 | subdir.append("-"); |
| 370 | subdir.append(application_name_); |
| 371 | replace_substrs(" ", 1, "", 0, &subdir); |
| 372 | std::transform(subdir.begin(), subdir.end(), subdir.begin(), ::tolower); |
| 373 | path->AppendFolder(subdir); |
| 374 | #endif |
| 375 | if (!CreateFolder(*path, 0700)) { |
| 376 | return false; |
| 377 | } |
| 378 | #if !defined(__native_client__) |
| 379 | // If the folder already exists, it may have the wrong mode or be owned by |
| 380 | // someone else, both of which are security problems. Setting the mode |
| 381 | // avoids both issues since it will fail if the path is not owned by us. |
| 382 | if (0 != ::chmod(path->pathname().c_str(), 0700)) { |
| 383 | LOG_ERR(LS_ERROR) << "Can't set mode on " << path; |
| 384 | return false; |
| 385 | } |
| 386 | #endif |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | bool UnixFilesystem::GetAppTempFolder(Pathname* path) { |
erikchen | 2ca8d5c | 2016-10-05 16:04:33 -0700 | [diff] [blame] | 391 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 392 | ASSERT(provided_app_temp_folder_ != NULL); |
| 393 | path->SetPathname(provided_app_temp_folder_); |
| 394 | return true; |
| 395 | #else |
| 396 | ASSERT(!application_name_.empty()); |
| 397 | // TODO: Consider whether we are worried about thread safety. |
| 398 | if (app_temp_path_ != NULL && strlen(app_temp_path_) > 0) { |
| 399 | path->SetPathname(app_temp_path_); |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | // Create a random directory as /tmp/<appname>-<pid>-<timestamp> |
| 404 | char buffer[128]; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 405 | sprintfn(buffer, arraysize(buffer), "-%d-%d", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 406 | static_cast<int>(getpid()), |
| 407 | static_cast<int>(time(0))); |
| 408 | std::string folder(application_name_); |
| 409 | folder.append(buffer); |
| 410 | if (!GetTemporaryFolder(*path, true, &folder)) |
| 411 | return false; |
| 412 | |
| 413 | delete [] app_temp_path_; |
| 414 | app_temp_path_ = CopyString(path->pathname()); |
| 415 | // TODO: atexit(DeleteFolderAndContents(app_temp_path_)); |
| 416 | return true; |
| 417 | #endif |
| 418 | } |
| 419 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 420 | bool UnixFilesystem::GetDiskFreeSpace(const Pathname& path, |
| 421 | int64_t* freebytes) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 422 | #ifdef __native_client__ |
| 423 | return false; |
| 424 | #else // __native_client__ |
| 425 | ASSERT(NULL != freebytes); |
| 426 | // TODO: Consider making relative paths absolute using cwd. |
| 427 | // TODO: When popping off a symlink, push back on the components of the |
| 428 | // symlink, so we don't jump out of the target disk inadvertently. |
| 429 | Pathname existing_path(path.folder(), ""); |
| 430 | while (!existing_path.folder().empty() && IsAbsent(existing_path)) { |
| 431 | existing_path.SetFolder(existing_path.parent_folder()); |
| 432 | } |
| 433 | #if defined(WEBRTC_ANDROID) |
| 434 | struct statfs vfs; |
| 435 | memset(&vfs, 0, sizeof(vfs)); |
| 436 | if (0 != statfs(existing_path.pathname().c_str(), &vfs)) |
| 437 | return false; |
| 438 | #else |
| 439 | struct statvfs vfs; |
| 440 | memset(&vfs, 0, sizeof(vfs)); |
| 441 | if (0 != statvfs(existing_path.pathname().c_str(), &vfs)) |
| 442 | return false; |
| 443 | #endif // WEBRTC_ANDROID |
| 444 | #if defined(WEBRTC_LINUX) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 445 | *freebytes = static_cast<int64_t>(vfs.f_bsize) * vfs.f_bavail; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 446 | #elif defined(WEBRTC_MAC) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 447 | *freebytes = static_cast<int64_t>(vfs.f_frsize) * vfs.f_bavail; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 448 | #endif |
| 449 | |
| 450 | return true; |
| 451 | #endif // !__native_client__ |
| 452 | } |
| 453 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 454 | char* UnixFilesystem::CopyString(const std::string& str) { |
| 455 | size_t size = str.length() + 1; |
| 456 | |
| 457 | char* buf = new char[size]; |
| 458 | if (!buf) { |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | strcpyn(buf, size, str.c_str()); |
| 463 | return buf; |
| 464 | } |
| 465 | |
| 466 | } // namespace rtc |
| 467 | |
| 468 | #if defined(__native_client__) |
| 469 | extern "C" int __attribute__((weak)) |
| 470 | link(const char* oldpath, const char* newpath) { |
| 471 | errno = EACCES; |
| 472 | return -1; |
| 473 | } |
| 474 | #endif |