blob: 6f20a203138f5af5da348f97a87e9e26773a3c5e [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>
22#include <sys/types.h>
23#include <unistd.h>
Patrik Höglunda8005cf2017-12-13 16:05:42 +010024#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
27#include "rtc_base/constructormagic.h"
28#include "rtc_base/platform_file.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029
30namespace rtc {
31
32class FileStream;
33class Pathname;
34
35//////////////////////////
36// Directory Iterator //
37//////////////////////////
38
39// A DirectoryIterator is created with a given directory. It originally points
40// to the first file in the directory, and can be advanecd with Next(). This
41// allows you to get information about each file.
42
43class DirectoryIterator {
44 friend class Filesystem;
45 public:
46 // Constructor
47 DirectoryIterator();
48 // Destructor
49 virtual ~DirectoryIterator();
50
51 // Starts traversing a directory
52 // dir is the directory to traverse
53 // returns true if the directory exists and is valid
54 // The iterator will point to the first entry in the directory
55 virtual bool Iterate(const Pathname &path);
56
57 // Advances to the next file
58 // returns true if there were more files in the directory.
59 virtual bool Next();
60
61 // returns true if the file currently pointed to is a directory
62 virtual bool IsDirectory() const;
63
64 // returns the name of the file currently pointed to
65 virtual std::string Name() const;
66
67 private:
68 std::string directory_;
69#if defined(WEBRTC_WIN)
70 WIN32_FIND_DATA data_;
71 HANDLE handle_;
72#else
73 DIR *dir_;
74 struct dirent *dirent_;
75 struct stat stat_;
76#endif
77};
78
79class FilesystemInterface {
80 public:
81 virtual ~FilesystemInterface() {}
82
83 // This will attempt to delete the path located at filename.
84 // It DCHECKs and returns false if the path points to a folder or a
85 // non-existent file.
86 virtual bool DeleteFile(const Pathname &filename) = 0;
87
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 // This moves a file from old_path to new_path, where "old_path" is a
89 // plain file. This DCHECKs and returns false if old_path points to a
90 // directory, and returns true if the function succeeds.
91 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
92
93 // Returns true if pathname refers to a directory
94 virtual bool IsFolder(const Pathname& pathname) = 0;
95
96 // Returns true if pathname refers to a file
97 virtual bool IsFile(const Pathname& pathname) = 0;
98
Zhi Huang53292142017-12-08 00:08:23 +000099 virtual std::string TempFilename(const Pathname &dir,
100 const std::string &prefix) = 0;
101
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102 // Determines the size of the file indicated by path.
103 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
104};
105
106class Filesystem {
107 public:
108 static FilesystemInterface *default_filesystem() {
109 RTC_DCHECK(default_filesystem_);
110 return default_filesystem_;
111 }
112
113 static void set_default_filesystem(FilesystemInterface *filesystem) {
114 default_filesystem_ = filesystem;
115 }
116
117 static FilesystemInterface *swap_default_filesystem(
118 FilesystemInterface *filesystem) {
119 FilesystemInterface *cur = default_filesystem_;
120 default_filesystem_ = filesystem;
121 return cur;
122 }
123
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124 static bool DeleteFile(const Pathname &filename) {
125 return EnsureDefaultFilesystem()->DeleteFile(filename);
126 }
127
128 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
129 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
130 }
131
132 static bool IsFolder(const Pathname& pathname) {
133 return EnsureDefaultFilesystem()->IsFolder(pathname);
134 }
135
136 static bool IsFile(const Pathname &pathname) {
137 return EnsureDefaultFilesystem()->IsFile(pathname);
138 }
139
Zhi Huang53292142017-12-08 00:08:23 +0000140 static std::string TempFilename(const Pathname &dir,
141 const std::string &prefix) {
142 return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
143 }
144
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200145 static bool GetFileSize(const Pathname& path, size_t* size) {
146 return EnsureDefaultFilesystem()->GetFileSize(path, size);
147 }
148
149 private:
150 static FilesystemInterface* default_filesystem_;
151
152 static FilesystemInterface *EnsureDefaultFilesystem();
153 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
154};
155
156} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200158#endif // RTC_BASE_FILEUTILS_H_