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