blob: 132fd884401cff5ffb023efe85e8f08956751ff7 [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 bool DeleteFile(const Pathname& filename) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200107 return GetFilesystem()->DeleteFile(filename);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108 }
109
Yves Gerey665174f2018-06-19 15:03:05 +0200110 static bool MoveFile(const Pathname& old_path, const Pathname& new_path) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200111 return GetFilesystem()->MoveFile(old_path, new_path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112 }
113
114 static bool IsFolder(const Pathname& pathname) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200115 return GetFilesystem()->IsFolder(pathname);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116 }
117
Yves Gerey665174f2018-06-19 15:03:05 +0200118 static bool IsFile(const Pathname& pathname) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200119 return GetFilesystem()->IsFile(pathname);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 }
121
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 static bool GetFileSize(const Pathname& path, size_t* size) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200123 return GetFilesystem()->GetFileSize(path, size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124 }
125
126 private:
Niels Möllerae82ffa2018-06-25 09:52:21 +0200127 static FilesystemInterface* GetFilesystem();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
129};
130
131} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200133#endif // RTC_BASE_FILEUTILS_H_