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