OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 break; | 306 break; |
307 case FTT_ACCESSED: | 307 case FTT_ACCESSED: |
308 *time = st.st_atime; | 308 *time = st.st_atime; |
309 break; | 309 break; |
310 default: | 310 default: |
311 return false; | 311 return false; |
312 } | 312 } |
313 return true; | 313 return true; |
314 } | 314 } |
315 | 315 |
316 bool UnixFilesystem::GetAppDataFolder(Pathname* path, bool per_user) { | |
317 // On macOS and iOS, there is no requirement that the path contains the | |
318 // organization. | |
319 #if !defined(WEBRTC_MAC) | |
320 RTC_DCHECK(!organization_name_.empty()); | |
321 #endif | |
322 RTC_DCHECK(!application_name_.empty()); | |
323 | |
324 // First get the base directory for app data. | |
325 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) | |
326 RTC_DCHECK(provided_app_data_folder_ != NULL); | |
327 path->SetPathname(provided_app_data_folder_, ""); | |
328 #elif defined(WEBRTC_LINUX) // && !WEBRTC_MAC && !WEBRTC_ANDROID | |
329 if (per_user) { | |
330 // We follow the recommendations in | |
331 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html | |
332 // It specifies separate directories for data and config files, but | |
333 // GetAppDataFolder() does not distinguish. We just return the config dir | |
334 // path. | |
335 const char* xdg_config_home = getenv("XDG_CONFIG_HOME"); | |
336 if (xdg_config_home) { | |
337 path->SetPathname(xdg_config_home, ""); | |
338 } else { | |
339 // XDG says to default to $HOME/.config. We also support falling back to | |
340 // other synonyms for HOME if for some reason it is not defined. | |
341 const char* homedir; | |
342 if (const char* home = getenv("HOME")) { | |
343 homedir = home; | |
344 } else if (const char* dotdir = getenv("DOTDIR")) { | |
345 homedir = dotdir; | |
346 } else if (passwd* pw = getpwuid(geteuid())) { | |
347 homedir = pw->pw_dir; | |
348 } else { | |
349 return false; | |
350 } | |
351 path->SetPathname(homedir, ""); | |
352 path->AppendFolder(".config"); | |
353 } | |
354 } else { | |
355 // XDG does not define a standard directory for writable global data. Let's | |
356 // just use this. | |
357 path->SetPathname("/var/cache/", ""); | |
358 } | |
359 #endif // !WEBRTC_MAC && !WEBRTC_LINUX | |
360 | |
361 // Now add on a sub-path for our app. | |
362 #if defined(WEBRTC_MAC) || defined(WEBRTC_ANDROID) | |
363 path->AppendFolder(organization_name_); | |
364 path->AppendFolder(application_name_); | |
365 #elif defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) | |
366 // XDG says to use a single directory level, so we concatenate the org and app | |
367 // name with a hyphen. We also do the Linuxy thing and convert to all | |
368 // lowercase with no spaces. | |
369 std::string subdir(organization_name_); | |
370 subdir.append("-"); | |
371 subdir.append(application_name_); | |
372 replace_substrs(" ", 1, "", 0, &subdir); | |
373 std::transform(subdir.begin(), subdir.end(), subdir.begin(), ::tolower); | |
374 path->AppendFolder(subdir); | |
375 #endif | |
376 if (!CreateFolder(*path, 0700)) { | |
377 return false; | |
378 } | |
379 #if !defined(__native_client__) | |
380 // If the folder already exists, it may have the wrong mode or be owned by | |
381 // someone else, both of which are security problems. Setting the mode | |
382 // avoids both issues since it will fail if the path is not owned by us. | |
383 if (0 != ::chmod(path->pathname().c_str(), 0700)) { | |
384 LOG_ERR(LS_ERROR) << "Can't set mode on " << path; | |
385 return false; | |
386 } | |
387 #endif | |
388 return true; | |
389 } | |
390 | |
391 bool UnixFilesystem::GetAppTempFolder(Pathname* path) { | 316 bool UnixFilesystem::GetAppTempFolder(Pathname* path) { |
392 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) | 317 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
393 RTC_DCHECK(provided_app_temp_folder_ != NULL); | 318 RTC_DCHECK(provided_app_temp_folder_ != NULL); |
394 path->SetPathname(provided_app_temp_folder_); | 319 path->SetPathname(provided_app_temp_folder_); |
395 return true; | 320 return true; |
396 #else | 321 #else |
397 RTC_DCHECK(!application_name_.empty()); | 322 RTC_DCHECK(!application_name_.empty()); |
398 // TODO: Consider whether we are worried about thread safety. | 323 // TODO: Consider whether we are worried about thread safety. |
399 if (app_temp_path_ != NULL && strlen(app_temp_path_) > 0) { | 324 if (app_temp_path_ != NULL && strlen(app_temp_path_) > 0) { |
400 path->SetPathname(app_temp_path_); | 325 path->SetPathname(app_temp_path_); |
(...skipping 10 matching lines...) Expand all Loading... |
411 if (!GetTemporaryFolder(*path, true, &folder)) | 336 if (!GetTemporaryFolder(*path, true, &folder)) |
412 return false; | 337 return false; |
413 | 338 |
414 delete [] app_temp_path_; | 339 delete [] app_temp_path_; |
415 app_temp_path_ = CopyString(path->pathname()); | 340 app_temp_path_ = CopyString(path->pathname()); |
416 // TODO: atexit(DeleteFolderAndContents(app_temp_path_)); | 341 // TODO: atexit(DeleteFolderAndContents(app_temp_path_)); |
417 return true; | 342 return true; |
418 #endif | 343 #endif |
419 } | 344 } |
420 | 345 |
421 bool UnixFilesystem::GetDiskFreeSpace(const Pathname& path, | |
422 int64_t* freebytes) { | |
423 #ifdef __native_client__ | |
424 return false; | |
425 #else // __native_client__ | |
426 RTC_DCHECK(NULL != freebytes); | |
427 // TODO: Consider making relative paths absolute using cwd. | |
428 // TODO: When popping off a symlink, push back on the components of the | |
429 // symlink, so we don't jump out of the target disk inadvertently. | |
430 Pathname existing_path(path.folder(), ""); | |
431 while (!existing_path.folder().empty() && IsAbsent(existing_path)) { | |
432 existing_path.SetFolder(existing_path.parent_folder()); | |
433 } | |
434 #if defined(WEBRTC_ANDROID) | |
435 struct statfs vfs; | |
436 memset(&vfs, 0, sizeof(vfs)); | |
437 if (0 != statfs(existing_path.pathname().c_str(), &vfs)) | |
438 return false; | |
439 #else | |
440 struct statvfs vfs; | |
441 memset(&vfs, 0, sizeof(vfs)); | |
442 if (0 != statvfs(existing_path.pathname().c_str(), &vfs)) | |
443 return false; | |
444 #endif // WEBRTC_ANDROID | |
445 #if defined(WEBRTC_LINUX) | |
446 *freebytes = static_cast<int64_t>(vfs.f_bsize) * vfs.f_bavail; | |
447 #elif defined(WEBRTC_MAC) | |
448 *freebytes = static_cast<int64_t>(vfs.f_frsize) * vfs.f_bavail; | |
449 #endif | |
450 | |
451 return true; | |
452 #endif // !__native_client__ | |
453 } | |
454 | |
455 char* UnixFilesystem::CopyString(const std::string& str) { | 346 char* UnixFilesystem::CopyString(const std::string& str) { |
456 size_t size = str.length() + 1; | 347 size_t size = str.length() + 1; |
457 | 348 |
458 char* buf = new char[size]; | 349 char* buf = new char[size]; |
459 if (!buf) { | 350 if (!buf) { |
460 return NULL; | 351 return NULL; |
461 } | 352 } |
462 | 353 |
463 strcpyn(buf, size, str.c_str()); | 354 strcpyn(buf, size, str.c_str()); |
464 return buf; | 355 return buf; |
465 } | 356 } |
466 | 357 |
467 } // namespace rtc | 358 } // namespace rtc |
468 | 359 |
469 #if defined(__native_client__) | 360 #if defined(__native_client__) |
470 extern "C" int __attribute__((weak)) | 361 extern "C" int __attribute__((weak)) |
471 link(const char* oldpath, const char* newpath) { | 362 link(const char* oldpath, const char* newpath) { |
472 errno = EACCES; | 363 errno = EACCES; |
473 return -1; | 364 return -1; |
474 } | 365 } |
475 #endif | 366 #endif |
OLD | NEW |