blob: deaf2e387a38310e4f4844795ca0f3f3ec361e6d [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_FILEUTILS_H_
12#define RTC_BASE_FILEUTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Patrik Höglunda8005cf2017-12-13 16:05:42 +010016#if defined(WEBRTC_WIN)
17#include <windows.h>
18#else
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#include <dirent.h>
20#include <stdio.h>
21#include <sys/stat.h>
Patrik Höglunda8005cf2017-12-13 16:05:42 +010022#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/constructormagic.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26namespace rtc {
27
Qingsi Wang2039ee72018-11-02 16:30:10 +000028class Pathname;
29
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030//////////////////////////
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
38class DirectoryIterator {
39 friend class Filesystem;
Yves Gerey665174f2018-06-19 15:03:05 +020040
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 public:
42 // Constructor
43 DirectoryIterator();
44 // Destructor
45 virtual ~DirectoryIterator();
46
Qingsi Wang2039ee72018-11-02 16:30:10 +000047 // 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 Kjellanderec78f1c2017-06-29 07:52:50 +020052
53 // Advances to the next file
54 // returns true if there were more files in the directory.
55 virtual bool Next();
56
Qingsi Wang2039ee72018-11-02 16:30:10 +000057 // returns true if the file currently pointed to is a directory
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058 virtual bool IsDirectory() const;
59
Qingsi Wang2039ee72018-11-02 16:30:10 +000060 // returns the name of the file currently pointed to
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061 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 Gerey665174f2018-06-19 15:03:05 +020069 DIR* dir_;
70 struct dirent* dirent_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071 struct stat stat_;
72#endif
73};
74
75class 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 Wang2039ee72018-11-02 16:30:10 +000082 virtual bool DeleteFile(const Pathname& filename) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 // 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 Wang2039ee72018-11-02 16:30:10 +000087 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 Kjellanderec78f1c2017-06-29 07:52:50 +020091
92 // Returns true if pathname refers to a file
Qingsi Wang2039ee72018-11-02 16:30:10 +000093 virtual bool IsFile(const Pathname& pathname) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020094
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 // Determines the size of the file indicated by path.
Qingsi Wang2039ee72018-11-02 16:30:10 +000096 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097};
98
99class Filesystem {
100 public:
Qingsi Wang2039ee72018-11-02 16:30:10 +0000101 static bool DeleteFile(const Pathname& filename) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200102 return GetFilesystem()->DeleteFile(filename);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 }
104
Qingsi Wang2039ee72018-11-02 16:30:10 +0000105 static bool MoveFile(const Pathname& old_path, const Pathname& new_path) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200106 return GetFilesystem()->MoveFile(old_path, new_path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 }
108
Qingsi Wang2039ee72018-11-02 16:30:10 +0000109 static bool IsFolder(const Pathname& pathname) {
110 return GetFilesystem()->IsFolder(pathname);
111 }
112
113 static bool IsFile(const Pathname& pathname) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200114 return GetFilesystem()->IsFile(pathname);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115 }
116
Qingsi Wang2039ee72018-11-02 16:30:10 +0000117 static bool GetFileSize(const Pathname& path, size_t* size) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200118 return GetFilesystem()->GetFileSize(path, size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 }
120
121 private:
Niels Möllerae82ffa2018-06-25 09:52:21 +0200122 static FilesystemInterface* GetFilesystem();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200123 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
124};
125
126} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200128#endif // RTC_BASE_FILEUTILS_H_