blob: f7afaf959534b6f6e6c742dd372e409b5c3f7572 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#ifndef RTC_BASE_FILEUTILS_H_
12#define RTC_BASE_FILEUTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:2615
Patrik Höglunda8005cf2017-12-13 15:05:4216#if defined(WEBRTC_WIN)
17#include <windows.h>
18#else
Henrik Kjellanderec78f1c2017-06-29 05:52:5019#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 15:05:4224#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 05:52:5025
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "rtc_base/constructormagic.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5027
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 13:03:0543
Henrik Kjellanderec78f1c2017-06-29 05:52:5044 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 13:03:0554 virtual bool Iterate(const Pathname& path);
Henrik Kjellanderec78f1c2017-06-29 05:52:5055
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 13:03:0572 DIR* dir_;
73 struct dirent* dirent_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5074 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 13:03:0585 virtual bool DeleteFile(const Pathname& filename) = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:5086
Henrik Kjellanderec78f1c2017-06-29 05:52:5087 // 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 13:03:0590 virtual bool MoveFile(const Pathname& old_path, const Pathname& new_path) = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:5091
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 05:52:5098 // 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 13:03:05104 static bool DeleteFile(const Pathname& filename) {
Niels Möllerae82ffa2018-06-25 07:52:21105 return GetFilesystem()->DeleteFile(filename);
Henrik Kjellanderec78f1c2017-06-29 05:52:50106 }
107
Yves Gerey665174f2018-06-19 13:03:05108 static bool MoveFile(const Pathname& old_path, const Pathname& new_path) {
Niels Möllerae82ffa2018-06-25 07:52:21109 return GetFilesystem()->MoveFile(old_path, new_path);
Henrik Kjellanderec78f1c2017-06-29 05:52:50110 }
111
112 static bool IsFolder(const Pathname& pathname) {
Niels Möllerae82ffa2018-06-25 07:52:21113 return GetFilesystem()->IsFolder(pathname);
Henrik Kjellanderec78f1c2017-06-29 05:52:50114 }
115
Yves Gerey665174f2018-06-19 13:03:05116 static bool IsFile(const Pathname& pathname) {
Niels Möllerae82ffa2018-06-25 07:52:21117 return GetFilesystem()->IsFile(pathname);
Henrik Kjellanderec78f1c2017-06-29 05:52:50118 }
119
Henrik Kjellanderec78f1c2017-06-29 05:52:50120 static bool GetFileSize(const Pathname& path, size_t* size) {
Niels Möllerae82ffa2018-06-25 07:52:21121 return GetFilesystem()->GetFileSize(path, size);
Henrik Kjellanderec78f1c2017-06-29 05:52:50122 }
123
124 private:
Niels Möllerae82ffa2018-06-25 07:52:21125 static FilesystemInterface* GetFilesystem();
Henrik Kjellanderec78f1c2017-06-29 05:52:50126 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
127};
128
129} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26130
Mirko Bonadei92ea95e2017-09-15 04:47:31131#endif // RTC_BASE_FILEUTILS_H_