| 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 FileStream *UnixFilesystem::OpenFile(const Pathname &filename, | 125 FileStream *UnixFilesystem::OpenFile(const Pathname &filename, |
| 126 const std::string &mode) { | 126 const std::string &mode) { |
| 127 FileStream *fs = new FileStream(); | 127 FileStream *fs = new FileStream(); |
| 128 if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), NULL)) { | 128 if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), NULL)) { |
| 129 delete fs; | 129 delete fs; |
| 130 fs = NULL; | 130 fs = NULL; |
| 131 } | 131 } |
| 132 return fs; | 132 return fs; |
| 133 } | 133 } |
| 134 | 134 |
| 135 bool UnixFilesystem::CreatePrivateFile(const Pathname &filename) { | |
| 136 int fd = open(filename.pathname().c_str(), | |
| 137 O_RDWR | O_CREAT | O_EXCL, | |
| 138 S_IRUSR | S_IWUSR); | |
| 139 if (fd < 0) { | |
| 140 LOG_ERR(LS_ERROR) << "open() failed."; | |
| 141 return false; | |
| 142 } | |
| 143 // Don't need to keep the file descriptor. | |
| 144 if (close(fd) < 0) { | |
| 145 LOG_ERR(LS_ERROR) << "close() failed."; | |
| 146 // Continue. | |
| 147 } | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 bool UnixFilesystem::DeleteFile(const Pathname &filename) { | 135 bool UnixFilesystem::DeleteFile(const Pathname &filename) { |
| 152 LOG(LS_INFO) << "Deleting file:" << filename.pathname(); | 136 LOG(LS_INFO) << "Deleting file:" << filename.pathname(); |
| 153 | 137 |
| 154 if (!IsFile(filename)) { | 138 if (!IsFile(filename)) { |
| 155 ASSERT(IsFile(filename)); | 139 ASSERT(IsFile(filename)); |
| 156 return false; | 140 return false; |
| 157 } | 141 } |
| 158 return ::unlink(filename.pathname().c_str()) == 0; | 142 return ::unlink(filename.pathname().c_str()) == 0; |
| 159 } | 143 } |
| 160 | 144 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 if (errno != EXDEV) | 206 if (errno != EXDEV) |
| 223 return false; | 207 return false; |
| 224 if (!CopyFile(old_path, new_path)) | 208 if (!CopyFile(old_path, new_path)) |
| 225 return false; | 209 return false; |
| 226 if (!DeleteFile(old_path)) | 210 if (!DeleteFile(old_path)) |
| 227 return false; | 211 return false; |
| 228 } | 212 } |
| 229 return true; | 213 return true; |
| 230 } | 214 } |
| 231 | 215 |
| 232 bool UnixFilesystem::MoveFolder(const Pathname &old_path, | |
| 233 const Pathname &new_path) { | |
| 234 if (!IsFolder(old_path)) { | |
| 235 ASSERT(IsFolder(old_path)); | |
| 236 return false; | |
| 237 } | |
| 238 LOG(LS_VERBOSE) << "Moving " << old_path.pathname() | |
| 239 << " to " << new_path.pathname(); | |
| 240 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) { | |
| 241 if (errno != EXDEV) | |
| 242 return false; | |
| 243 if (!CopyFolder(old_path, new_path)) | |
| 244 return false; | |
| 245 if (!DeleteFolderAndContents(old_path)) | |
| 246 return false; | |
| 247 } | |
| 248 return true; | |
| 249 } | |
| 250 | |
| 251 bool UnixFilesystem::IsFolder(const Pathname &path) { | 216 bool UnixFilesystem::IsFolder(const Pathname &path) { |
| 252 struct stat st; | 217 struct stat st; |
| 253 if (stat(path.pathname().c_str(), &st) < 0) | 218 if (stat(path.pathname().c_str(), &st) < 0) |
| 254 return false; | 219 return false; |
| 255 return S_ISDIR(st.st_mode); | 220 return S_ISDIR(st.st_mode); |
| 256 } | 221 } |
| 257 | 222 |
| 258 bool UnixFilesystem::CopyFile(const Pathname &old_path, | 223 bool UnixFilesystem::CopyFile(const Pathname &old_path, |
| 259 const Pathname &new_path) { | 224 const Pathname &new_path) { |
| 260 LOG(LS_VERBOSE) << "Copying " << old_path.pathname() | 225 LOG(LS_VERBOSE) << "Copying " << old_path.pathname() |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 break; | 305 break; |
| 341 case FTT_ACCESSED: | 306 case FTT_ACCESSED: |
| 342 *time = st.st_atime; | 307 *time = st.st_atime; |
| 343 break; | 308 break; |
| 344 default: | 309 default: |
| 345 return false; | 310 return false; |
| 346 } | 311 } |
| 347 return true; | 312 return true; |
| 348 } | 313 } |
| 349 | 314 |
| 350 bool UnixFilesystem::GetAppPathname(Pathname* path) { | |
| 351 #if defined(__native_client__) | |
| 352 return false; | |
| 353 #elif defined(WEBRTC_MAC) | |
| 354 AppleAppName(path); | |
| 355 return true; | |
| 356 #else // WEBRTC_MAC && !defined(WEBRTC_IOS) | |
| 357 char buffer[PATH_MAX + 2]; | |
| 358 ssize_t len = readlink("/proc/self/exe", buffer, arraysize(buffer) - 1); | |
| 359 if ((len <= 0) || (len == PATH_MAX + 1)) | |
| 360 return false; | |
| 361 buffer[len] = '\0'; | |
| 362 path->SetPathname(buffer); | |
| 363 return true; | |
| 364 #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) | |
| 365 } | |
| 366 | |
| 367 bool UnixFilesystem::GetAppDataFolder(Pathname* path, bool per_user) { | 315 bool UnixFilesystem::GetAppDataFolder(Pathname* path, bool per_user) { |
| 368 // On macOS and iOS, there is no requirement that the path contains the | 316 // On macOS and iOS, there is no requirement that the path contains the |
| 369 // organization. | 317 // organization. |
| 370 #if !defined(WEBRTC_MAC) | 318 #if !defined(WEBRTC_MAC) |
| 371 ASSERT(!organization_name_.empty()); | 319 ASSERT(!organization_name_.empty()); |
| 372 #endif | 320 #endif |
| 373 ASSERT(!application_name_.empty()); | 321 ASSERT(!application_name_.empty()); |
| 374 | 322 |
| 375 // First get the base directory for app data. | 323 // First get the base directory for app data. |
| 376 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) | 324 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 #if defined(WEBRTC_LINUX) | 444 #if defined(WEBRTC_LINUX) |
| 497 *freebytes = static_cast<int64_t>(vfs.f_bsize) * vfs.f_bavail; | 445 *freebytes = static_cast<int64_t>(vfs.f_bsize) * vfs.f_bavail; |
| 498 #elif defined(WEBRTC_MAC) | 446 #elif defined(WEBRTC_MAC) |
| 499 *freebytes = static_cast<int64_t>(vfs.f_frsize) * vfs.f_bavail; | 447 *freebytes = static_cast<int64_t>(vfs.f_frsize) * vfs.f_bavail; |
| 500 #endif | 448 #endif |
| 501 | 449 |
| 502 return true; | 450 return true; |
| 503 #endif // !__native_client__ | 451 #endif // !__native_client__ |
| 504 } | 452 } |
| 505 | 453 |
| 506 Pathname UnixFilesystem::GetCurrentDirectory() { | |
| 507 Pathname cwd; | |
| 508 char buffer[PATH_MAX]; | |
| 509 char *path = getcwd(buffer, PATH_MAX); | |
| 510 | |
| 511 if (!path) { | |
| 512 LOG_ERR(LS_ERROR) << "getcwd() failed"; | |
| 513 return cwd; // returns empty pathname | |
| 514 } | |
| 515 cwd.SetFolder(std::string(path)); | |
| 516 | |
| 517 return cwd; | |
| 518 } | |
| 519 | |
| 520 char* UnixFilesystem::CopyString(const std::string& str) { | 454 char* UnixFilesystem::CopyString(const std::string& str) { |
| 521 size_t size = str.length() + 1; | 455 size_t size = str.length() + 1; |
| 522 | 456 |
| 523 char* buf = new char[size]; | 457 char* buf = new char[size]; |
| 524 if (!buf) { | 458 if (!buf) { |
| 525 return NULL; | 459 return NULL; |
| 526 } | 460 } |
| 527 | 461 |
| 528 strcpyn(buf, size, str.c_str()); | 462 strcpyn(buf, size, str.c_str()); |
| 529 return buf; | 463 return buf; |
| 530 } | 464 } |
| 531 | 465 |
| 532 } // namespace rtc | 466 } // namespace rtc |
| 533 | 467 |
| 534 #if defined(__native_client__) | 468 #if defined(__native_client__) |
| 535 extern "C" int __attribute__((weak)) | 469 extern "C" int __attribute__((weak)) |
| 536 link(const char* oldpath, const char* newpath) { | 470 link(const char* oldpath, const char* newpath) { |
| 537 errno = EACCES; | 471 errno = EACCES; |
| 538 return -1; | 472 return -1; |
| 539 } | 473 } |
| 540 #endif | 474 #endif |
| OLD | NEW |