blob: 69e64ae35e215692974ebdc97e27c51b8af2cdf7 [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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
25#include "rtc_base/constructormagic.h"
26#include "rtc_base/platform_file.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027
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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086 // This moves a file from old_path to new_path, where "old_path" is a
87 // plain file. This DCHECKs and returns false if old_path points to a
88 // directory, and returns true if the function succeeds.
89 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
90
91 // Returns true if pathname refers to a directory
92 virtual bool IsFolder(const Pathname& pathname) = 0;
93
94 // Returns true if pathname refers to a file
95 virtual bool IsFile(const Pathname& pathname) = 0;
96
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097 // Determines the size of the file indicated by path.
98 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
99};
100
101class Filesystem {
102 public:
103 static FilesystemInterface *default_filesystem() {
104 RTC_DCHECK(default_filesystem_);
105 return default_filesystem_;
106 }
107
108 static void set_default_filesystem(FilesystemInterface *filesystem) {
109 default_filesystem_ = filesystem;
110 }
111
112 static FilesystemInterface *swap_default_filesystem(
113 FilesystemInterface *filesystem) {
114 FilesystemInterface *cur = default_filesystem_;
115 default_filesystem_ = filesystem;
116 return cur;
117 }
118
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 static bool DeleteFile(const Pathname &filename) {
120 return EnsureDefaultFilesystem()->DeleteFile(filename);
121 }
122
123 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
124 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
125 }
126
127 static bool IsFolder(const Pathname& pathname) {
128 return EnsureDefaultFilesystem()->IsFolder(pathname);
129 }
130
131 static bool IsFile(const Pathname &pathname) {
132 return EnsureDefaultFilesystem()->IsFile(pathname);
133 }
134
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135 static bool GetFileSize(const Pathname& path, size_t* size) {
136 return EnsureDefaultFilesystem()->GetFileSize(path, size);
137 }
138
139 private:
140 static FilesystemInterface* default_filesystem_;
141
142 static FilesystemInterface *EnsureDefaultFilesystem();
143 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
144};
145
146} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200148#endif // RTC_BASE_FILEUTILS_H_