blob: ba4b28995e3117ae645de174c762fa4af0b90bd6 [file] [log] [blame]
henrike@webrtc.org47be73b2014-05-13 18:00:261/*
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
Henrik Kjellanderfb3e1b62017-06-29 06:03:0411#ifndef WEBRTC_RTC_BASE_FILEUTILS_H_
12#define WEBRTC_RTC_BASE_FILEUTILS_H_
henrike@webrtc.org47be73b2014-05-13 18:00:2613
Henrik Kjellander88b2dd42017-06-29 05:52:5014#include <string>
henrike@webrtc.org47be73b2014-05-13 18:00:2615
Henrik Kjellander88b2dd42017-06-29 05:52:5016#if !defined(WEBRTC_WIN)
17#include <dirent.h>
18#include <stdio.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22#endif
23
kjellander19796962017-06-30 17:45:2124#include "webrtc/rtc_base/checks.h"
25#include "webrtc/rtc_base/constructormagic.h"
26#include "webrtc/rtc_base/platform_file.h"
Henrik Kjellander88b2dd42017-06-29 05:52:5027
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
65 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
77class FilesystemInterface {
78 public:
79 virtual ~FilesystemInterface() {}
80
81 // This will attempt to delete the path located at filename.
82 // It DCHECKs and returns false if the path points to a folder or a
83 // non-existent file.
84 virtual bool DeleteFile(const Pathname &filename) = 0;
85
86 // 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
91 // plain file. This DCHECKs and returns false if old_path points to a
92 // directory, and returns true if the function succeeds.
93 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
94
95 // 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
Henrik Kjellander88b2dd42017-06-29 05:52:50101 virtual std::string TempFilename(const Pathname &dir,
102 const std::string &prefix) = 0;
103
104 // Determines the size of the file indicated by path.
105 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
106};
107
108class Filesystem {
109 public:
110 static FilesystemInterface *default_filesystem() {
111 RTC_DCHECK(default_filesystem_);
112 return default_filesystem_;
113 }
114
115 static void set_default_filesystem(FilesystemInterface *filesystem) {
116 default_filesystem_ = filesystem;
117 }
118
119 static FilesystemInterface *swap_default_filesystem(
120 FilesystemInterface *filesystem) {
121 FilesystemInterface *cur = default_filesystem_;
122 default_filesystem_ = filesystem;
123 return cur;
124 }
125
126 static bool CreateFolder(const Pathname &pathname) {
127 return EnsureDefaultFilesystem()->CreateFolder(pathname);
128 }
129
130 static bool DeleteFile(const Pathname &filename) {
131 return EnsureDefaultFilesystem()->DeleteFile(filename);
132 }
133
134 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
135 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
136 }
137
138 static bool IsFolder(const Pathname& pathname) {
139 return EnsureDefaultFilesystem()->IsFolder(pathname);
140 }
141
142 static bool IsFile(const Pathname &pathname) {
143 return EnsureDefaultFilesystem()->IsFile(pathname);
144 }
145
Henrik Kjellander88b2dd42017-06-29 05:52:50146 static std::string TempFilename(const Pathname &dir,
147 const std::string &prefix) {
148 return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
149 }
150
151 static bool GetFileSize(const Pathname& path, size_t* size) {
152 return EnsureDefaultFilesystem()->GetFileSize(path, size);
153 }
154
155 private:
156 static FilesystemInterface* default_filesystem_;
157
158 static FilesystemInterface *EnsureDefaultFilesystem();
159 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
160};
161
162} // namespace rtc
henrike@webrtc.org47be73b2014-05-13 18:00:26163
Henrik Kjellanderfb3e1b62017-06-29 06:03:04164#endif // WEBRTC_RTC_BASE_FILEUTILS_H_