blob: a2544947f3a87e448c047941a3d89165d4817228 [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 virtual std::string TempFilename(const Pathname &dir,
98 const std::string &prefix) = 0;
99
100 // 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:
106 static FilesystemInterface *default_filesystem() {
107 RTC_DCHECK(default_filesystem_);
108 return default_filesystem_;
109 }
110
111 static void set_default_filesystem(FilesystemInterface *filesystem) {
112 default_filesystem_ = filesystem;
113 }
114
115 static FilesystemInterface *swap_default_filesystem(
116 FilesystemInterface *filesystem) {
117 FilesystemInterface *cur = default_filesystem_;
118 default_filesystem_ = filesystem;
119 return cur;
120 }
121
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 static bool DeleteFile(const Pathname &filename) {
123 return EnsureDefaultFilesystem()->DeleteFile(filename);
124 }
125
126 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
127 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
128 }
129
130 static bool IsFolder(const Pathname& pathname) {
131 return EnsureDefaultFilesystem()->IsFolder(pathname);
132 }
133
134 static bool IsFile(const Pathname &pathname) {
135 return EnsureDefaultFilesystem()->IsFile(pathname);
136 }
137
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138 static std::string TempFilename(const Pathname &dir,
139 const std::string &prefix) {
140 return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
141 }
142
143 static bool GetFileSize(const Pathname& path, size_t* size) {
144 return EnsureDefaultFilesystem()->GetFileSize(path, size);
145 }
146
147 private:
148 static FilesystemInterface* default_filesystem_;
149
150 static FilesystemInterface *EnsureDefaultFilesystem();
151 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
152};
153
154} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200156#endif // RTC_BASE_FILEUTILS_H_