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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_FILEUTILS_H_ |
| 12 | #define RTC_BASE_FILEUTILS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 16 | #if defined(WEBRTC_WIN) |
| 17 | #include <windows.h> |
| 18 | #else |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <stdio.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 24 | #endif // WEBRTC_WIN |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 25 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/constructormagic.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 27 | |
| 28 | namespace rtc { |
| 29 | |
| 30 | class FileStream; |
| 31 | class Pathname; |
| 32 | |
| 33 | ////////////////////////// |
| 34 | // Directory Iterator // |
| 35 | ////////////////////////// |
| 36 | |
| 37 | // A DirectoryIterator is created with a given directory. It originally points |
| 38 | // to the first file in the directory, and can be advanecd with Next(). This |
| 39 | // allows you to get information about each file. |
| 40 | |
| 41 | class DirectoryIterator { |
| 42 | friend class Filesystem; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 43 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 44 | public: |
| 45 | // Constructor |
| 46 | DirectoryIterator(); |
| 47 | // Destructor |
| 48 | virtual ~DirectoryIterator(); |
| 49 | |
| 50 | // Starts traversing a directory |
| 51 | // dir is the directory to traverse |
| 52 | // returns true if the directory exists and is valid |
| 53 | // The iterator will point to the first entry in the directory |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 54 | virtual bool Iterate(const Pathname& path); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | |
| 56 | // Advances to the next file |
| 57 | // returns true if there were more files in the directory. |
| 58 | virtual bool Next(); |
| 59 | |
| 60 | // returns true if the file currently pointed to is a directory |
| 61 | virtual bool IsDirectory() const; |
| 62 | |
| 63 | // returns the name of the file currently pointed to |
| 64 | virtual std::string Name() const; |
| 65 | |
| 66 | private: |
| 67 | std::string directory_; |
| 68 | #if defined(WEBRTC_WIN) |
| 69 | WIN32_FIND_DATA data_; |
| 70 | HANDLE handle_; |
| 71 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 72 | DIR* dir_; |
| 73 | struct dirent* dirent_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 74 | struct stat stat_; |
| 75 | #endif |
| 76 | }; |
| 77 | |
| 78 | class FilesystemInterface { |
| 79 | public: |
| 80 | virtual ~FilesystemInterface() {} |
| 81 | |
| 82 | // This will attempt to delete the path located at filename. |
| 83 | // It DCHECKs and returns false if the path points to a folder or a |
| 84 | // non-existent file. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 85 | virtual bool DeleteFile(const Pathname& filename) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 86 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | // This moves a file from old_path to new_path, where "old_path" is a |
| 88 | // plain file. This DCHECKs and returns false if old_path points to a |
| 89 | // directory, and returns true if the function succeeds. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 90 | virtual bool MoveFile(const Pathname& old_path, const Pathname& new_path) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 91 | |
| 92 | // Returns true if pathname refers to a directory |
| 93 | virtual bool IsFolder(const Pathname& pathname) = 0; |
| 94 | |
| 95 | // Returns true if pathname refers to a file |
| 96 | virtual bool IsFile(const Pathname& pathname) = 0; |
| 97 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | // Determines the size of the file indicated by path. |
| 99 | virtual bool GetFileSize(const Pathname& path, size_t* size) = 0; |
| 100 | }; |
| 101 | |
| 102 | class Filesystem { |
| 103 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 104 | static bool DeleteFile(const Pathname& filename) { |
Niels Möller | ae82ffa | 2018-06-25 09:52:21 +0200 | [diff] [blame] | 105 | return GetFilesystem()->DeleteFile(filename); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 108 | static bool MoveFile(const Pathname& old_path, const Pathname& new_path) { |
Niels Möller | ae82ffa | 2018-06-25 09:52:21 +0200 | [diff] [blame] | 109 | return GetFilesystem()->MoveFile(old_path, new_path); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static bool IsFolder(const Pathname& pathname) { |
Niels Möller | ae82ffa | 2018-06-25 09:52:21 +0200 | [diff] [blame] | 113 | return GetFilesystem()->IsFolder(pathname); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 116 | static bool IsFile(const Pathname& pathname) { |
Niels Möller | ae82ffa | 2018-06-25 09:52:21 +0200 | [diff] [blame] | 117 | return GetFilesystem()->IsFile(pathname); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 120 | static bool GetFileSize(const Pathname& path, size_t* size) { |
Niels Möller | ae82ffa | 2018-06-25 09:52:21 +0200 | [diff] [blame] | 121 | return GetFilesystem()->GetFileSize(path, size); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private: |
Niels Möller | ae82ffa | 2018-06-25 09:52:21 +0200 | [diff] [blame] | 125 | static FilesystemInterface* GetFilesystem(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 126 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem); |
| 127 | }; |
| 128 | |
| 129 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 131 | #endif // RTC_BASE_FILEUTILS_H_ |