blob: f7afaf959534b6f6e6c742dd372e409b5c3f7572 [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/constructormagic.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;
Yves Gerey665174f2018-06-19 15:03:05 +020043
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044 public:
45 // Constructor
46 DirectoryIterator();
47 // Destructor
48 virtual ~DirectoryIterator();
49
50 // Starts traversing a directory
51 // dir is the directory to traverse
52 // returns true if the directory exists and is valid
53 // The iterator will point to the first entry in the directory
Yves Gerey665174f2018-06-19 15:03:05 +020054 virtual bool Iterate(const Pathname& path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055
56 // Advances to the next file
57 // returns true if there were more files in the directory.
58 virtual bool Next();
59
60 // returns true if the file currently pointed to is a directory
61 virtual bool IsDirectory() const;
62
63 // returns the name of the file currently pointed to
64 virtual std::string Name() const;
65
66 private:
67 std::string directory_;
68#if defined(WEBRTC_WIN)
69 WIN32_FIND_DATA data_;
70 HANDLE handle_;
71#else
Yves Gerey665174f2018-06-19 15:03:05 +020072 DIR* dir_;
73 struct dirent* dirent_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074 struct stat stat_;
75#endif
76};
77
78class FilesystemInterface {
79 public:
80 virtual ~FilesystemInterface() {}
81
82 // This will attempt to delete the path located at filename.
83 // It DCHECKs and returns false if the path points to a folder or a
84 // non-existent file.
Yves Gerey665174f2018-06-19 15:03:05 +020085 virtual bool DeleteFile(const Pathname& filename) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087 // This moves a file from old_path to new_path, where "old_path" is a
88 // plain file. This DCHECKs and returns false if old_path points to a
89 // directory, and returns true if the function succeeds.
Yves Gerey665174f2018-06-19 15:03:05 +020090 virtual bool MoveFile(const Pathname& old_path, const Pathname& new_path) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091
92 // Returns true if pathname refers to a directory
93 virtual bool IsFolder(const Pathname& pathname) = 0;
94
95 // Returns true if pathname refers to a file
96 virtual bool IsFile(const Pathname& pathname) = 0;
97
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098 // Determines the size of the file indicated by path.
99 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
100};
101
102class Filesystem {
103 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200104 static bool DeleteFile(const Pathname& filename) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200105 return GetFilesystem()->DeleteFile(filename);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106 }
107
Yves Gerey665174f2018-06-19 15:03:05 +0200108 static bool MoveFile(const Pathname& old_path, const Pathname& new_path) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200109 return GetFilesystem()->MoveFile(old_path, new_path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110 }
111
112 static bool IsFolder(const Pathname& pathname) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200113 return GetFilesystem()->IsFolder(pathname);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 }
115
Yves Gerey665174f2018-06-19 15:03:05 +0200116 static bool IsFile(const Pathname& pathname) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200117 return GetFilesystem()->IsFile(pathname);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 }
119
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 static bool GetFileSize(const Pathname& path, size_t* size) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200121 return GetFilesystem()->GetFileSize(path, size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 }
123
124 private:
Niels Möllerae82ffa2018-06-25 09:52:21 +0200125 static FilesystemInterface* GetFilesystem();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200126 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
127};
128
129} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // RTC_BASE_FILEUTILS_H_