blob: fcbc6c3f456db62a5c68123db00d42ebba231ae9 [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;
Yves Gerey665174f2018-06-19 15:03:05 +020045
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046 public:
47 // Constructor
48 DirectoryIterator();
49 // Destructor
50 virtual ~DirectoryIterator();
51
52 // Starts traversing a directory
53 // dir is the directory to traverse
54 // returns true if the directory exists and is valid
55 // The iterator will point to the first entry in the directory
Yves Gerey665174f2018-06-19 15:03:05 +020056 virtual bool Iterate(const Pathname& path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057
58 // Advances to the next file
59 // returns true if there were more files in the directory.
60 virtual bool Next();
61
62 // returns true if the file currently pointed to is a directory
63 virtual bool IsDirectory() const;
64
65 // returns the name of the file currently pointed to
66 virtual std::string Name() const;
67
68 private:
69 std::string directory_;
70#if defined(WEBRTC_WIN)
71 WIN32_FIND_DATA data_;
72 HANDLE handle_;
73#else
Yves Gerey665174f2018-06-19 15:03:05 +020074 DIR* dir_;
75 struct dirent* dirent_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076 struct stat stat_;
77#endif
78};
79
80class FilesystemInterface {
81 public:
82 virtual ~FilesystemInterface() {}
83
84 // This will attempt to delete the path located at filename.
85 // It DCHECKs and returns false if the path points to a folder or a
86 // non-existent file.
Yves Gerey665174f2018-06-19 15:03:05 +020087 virtual bool DeleteFile(const Pathname& filename) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089 // This moves a file from old_path to new_path, where "old_path" is a
90 // plain file. This DCHECKs and returns false if old_path points to a
91 // directory, and returns true if the function succeeds.
Yves Gerey665174f2018-06-19 15:03:05 +020092 virtual bool MoveFile(const Pathname& old_path, const Pathname& new_path) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093
94 // Returns true if pathname refers to a directory
95 virtual bool IsFolder(const Pathname& pathname) = 0;
96
97 // Returns true if pathname refers to a file
98 virtual bool IsFile(const Pathname& pathname) = 0;
99
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 // Determines the size of the file indicated by path.
101 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
102};
103
104class Filesystem {
105 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200106 static FilesystemInterface* default_filesystem() {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 RTC_DCHECK(default_filesystem_);
108 return default_filesystem_;
109 }
110
Yves Gerey665174f2018-06-19 15:03:05 +0200111 static void set_default_filesystem(FilesystemInterface* filesystem) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112 default_filesystem_ = filesystem;
113 }
114
Yves Gerey665174f2018-06-19 15:03:05 +0200115 static FilesystemInterface* swap_default_filesystem(
116 FilesystemInterface* filesystem) {
117 FilesystemInterface* cur = default_filesystem_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 default_filesystem_ = filesystem;
119 return cur;
120 }
121
Yves Gerey665174f2018-06-19 15:03:05 +0200122 static bool DeleteFile(const Pathname& filename) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200123 return EnsureDefaultFilesystem()->DeleteFile(filename);
124 }
125
Yves Gerey665174f2018-06-19 15:03:05 +0200126 static bool MoveFile(const Pathname& old_path, const Pathname& new_path) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
128 }
129
130 static bool IsFolder(const Pathname& pathname) {
131 return EnsureDefaultFilesystem()->IsFolder(pathname);
132 }
133
Yves Gerey665174f2018-06-19 15:03:05 +0200134 static bool IsFile(const Pathname& pathname) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135 return EnsureDefaultFilesystem()->IsFile(pathname);
136 }
137
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138 static bool GetFileSize(const Pathname& path, size_t* size) {
139 return EnsureDefaultFilesystem()->GetFileSize(path, size);
140 }
141
142 private:
143 static FilesystemInterface* default_filesystem_;
144
Yves Gerey665174f2018-06-19 15:03:05 +0200145 static FilesystemInterface* EnsureDefaultFilesystem();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200146 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
147};
148
149} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200151#endif // RTC_BASE_FILEUTILS_H_