blob: 00b4d8d263af4b6bed26ae1607ed93905a497fc3 [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
11#ifndef WEBRTC_BASE_FILEUTILS_H_
12#define WEBRTC_BASE_FILEUTILS_H_
13
14#include <string>
15
xians@webrtc.orge46bc772014-10-10 08:36:56 +000016#if !defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include <dirent.h>
18#include <stdio.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22#endif
23
zijiehedd87d582016-12-06 15:04:02 -080024#include "webrtc/base/checks.h"
kwiberg4485ffb2016-04-26 08:14:39 -070025#include "webrtc/base/constructormagic.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000026#include "webrtc/base/platform_file.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
28namespace rtc {
29
30class FileStream;
31class 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
41class DirectoryIterator {
42 friend class Filesystem;
43 public:
44 // Constructor
45 DirectoryIterator();
46 // Destructor
47 virtual ~DirectoryIterator();
48
49 // Starts traversing a directory
50 // dir is the directory to traverse
51 // returns true if the directory exists and is valid
52 // The iterator will point to the first entry in the directory
53 virtual bool Iterate(const Pathname &path);
54
55 // Advances to the next file
56 // returns true if there were more files in the directory.
57 virtual bool Next();
58
59 // returns true if the file currently pointed to is a directory
60 virtual bool IsDirectory() const;
61
62 // returns the name of the file currently pointed to
63 virtual std::string Name() const;
64
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 private:
66 std::string directory_;
67#if defined(WEBRTC_WIN)
68 WIN32_FIND_DATA data_;
69 HANDLE handle_;
70#else
71 DIR *dir_;
72 struct dirent *dirent_;
73 struct stat stat_;
74#endif
75};
76
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077class FilesystemInterface {
78 public:
79 virtual ~FilesystemInterface() {}
80
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 // This will attempt to delete the path located at filename.
nisse0ebdf272017-01-23 07:43:05 -080082 // It DCHECKs and returns false if the path points to a folder or a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 // non-existent file.
84 virtual bool DeleteFile(const Pathname &filename) = 0;
85
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 // Creates a directory. This will call itself recursively to create /foo/bar
87 // even if /foo does not exist. Returns true if the function succeeds.
88 virtual bool CreateFolder(const Pathname &pathname) = 0;
89
90 // This moves a file from old_path to new_path, where "old_path" is a
nisse0ebdf272017-01-23 07:43:05 -080091 // plain file. This DCHECKs and returns false if old_path points to a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 // directory, and returns true if the function succeeds.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
94
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 // Returns true if pathname refers to a directory
96 virtual bool IsFolder(const Pathname& pathname) = 0;
97
98 // Returns true if pathname refers to a file
99 virtual bool IsFile(const Pathname& pathname) = 0;
100
101 // Returns true if pathname refers to no filesystem object, every parent
102 // directory either exists, or is also absent.
103 virtual bool IsAbsent(const Pathname& pathname) = 0;
104
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 // A folder appropriate for storing temporary files (Contents are
106 // automatically deleted when the program exits)
107 virtual bool GetTemporaryFolder(Pathname &path, bool create,
108 const std::string *append) = 0;
109
110 virtual std::string TempFilename(const Pathname &dir,
111 const std::string &prefix) = 0;
112
113 // Determines the size of the file indicated by path.
114 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115};
116
117class Filesystem {
118 public:
119 static FilesystemInterface *default_filesystem() {
zijiehedd87d582016-12-06 15:04:02 -0800120 RTC_DCHECK(default_filesystem_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 return default_filesystem_;
122 }
123
124 static void set_default_filesystem(FilesystemInterface *filesystem) {
125 default_filesystem_ = filesystem;
126 }
127
128 static FilesystemInterface *swap_default_filesystem(
129 FilesystemInterface *filesystem) {
130 FilesystemInterface *cur = default_filesystem_;
131 default_filesystem_ = filesystem;
132 return cur;
133 }
134
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 static bool CreateFolder(const Pathname &pathname) {
136 return EnsureDefaultFilesystem()->CreateFolder(pathname);
137 }
138
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 static bool DeleteFile(const Pathname &filename) {
140 return EnsureDefaultFilesystem()->DeleteFile(filename);
141 }
142
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
144 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
145 }
146
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147 static bool IsFolder(const Pathname& pathname) {
148 return EnsureDefaultFilesystem()->IsFolder(pathname);
149 }
150
151 static bool IsFile(const Pathname &pathname) {
152 return EnsureDefaultFilesystem()->IsFile(pathname);
153 }
154
155 static bool IsAbsent(const Pathname &pathname) {
156 return EnsureDefaultFilesystem()->IsAbsent(pathname);
157 }
158
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159 static bool GetTemporaryFolder(Pathname &path, bool create,
160 const std::string *append) {
161 return EnsureDefaultFilesystem()->GetTemporaryFolder(path, create, append);
162 }
163
164 static std::string TempFilename(const Pathname &dir,
165 const std::string &prefix) {
166 return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
167 }
168
169 static bool GetFileSize(const Pathname& path, size_t* size) {
170 return EnsureDefaultFilesystem()->GetFileSize(path, size);
171 }
172
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173 private:
174 static FilesystemInterface* default_filesystem_;
175
176 static FilesystemInterface *EnsureDefaultFilesystem();
henrikg3c089d72015-09-16 05:37:44 -0700177 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178};
179
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180} // namespace rtc
181
182#endif // WEBRTC_BASE_FILEUTILS_H_