blob: d85b6b1787ff88c0cdebe9d7b708a7f0092f4270 [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>
Patrik Höglunda8005cf2017-12-13 16:05:42 +010022#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/constructormagic.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26namespace rtc {
27
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028//////////////////////////
29// Directory Iterator //
30//////////////////////////
31
32// A DirectoryIterator is created with a given directory. It originally points
33// to the first file in the directory, and can be advanecd with Next(). This
34// allows you to get information about each file.
35
36class DirectoryIterator {
37 friend class Filesystem;
Yves Gerey665174f2018-06-19 15:03:05 +020038
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039 public:
40 // Constructor
41 DirectoryIterator();
42 // Destructor
43 virtual ~DirectoryIterator();
44
Niels Möller6b9dec02018-11-02 08:50:21 +010045 // Starts traversing a directory.
46 // |dir| is the directory to traverse.
47 // returns true if the directory exists and is valid.
48 // The iterator will point to the first entry in the directory.
49 virtual bool Iterate(const std::string& dir);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050
51 // Advances to the next file
52 // returns true if there were more files in the directory.
53 virtual bool Next();
54
Niels Möller6b9dec02018-11-02 08:50:21 +010055 // Returns true if the file currently pointed to is a directory.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 virtual bool IsDirectory() const;
57
Niels Möller6b9dec02018-11-02 08:50:21 +010058 // Returns the name of the file currently pointed to.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059 virtual std::string Name() const;
60
Niels Möller6b9dec02018-11-02 08:50:21 +010061 // Returns complete name of the file, including directory part.
62 virtual std::string PathName() const;
63
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064 private:
65 std::string directory_;
66#if defined(WEBRTC_WIN)
67 WIN32_FIND_DATA data_;
68 HANDLE handle_;
69#else
Yves Gerey665174f2018-06-19 15:03:05 +020070 DIR* dir_;
71 struct dirent* dirent_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072 struct stat stat_;
73#endif
74};
75
76class FilesystemInterface {
77 public:
78 virtual ~FilesystemInterface() {}
79
80 // This will attempt to delete the path located at filename.
81 // It DCHECKs and returns false if the path points to a folder or a
82 // non-existent file.
Niels Möller6b9dec02018-11-02 08:50:21 +010083 virtual bool DeleteFile(const std::string& filename) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085 // This moves a file from old_path to new_path, where "old_path" is a
86 // plain file. This DCHECKs and returns false if old_path points to a
87 // directory, and returns true if the function succeeds.
Niels Möller6b9dec02018-11-02 08:50:21 +010088 virtual bool MoveFile(const std::string& old_path,
89 const std::string& new_path) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090
91 // Returns true if pathname refers to a file
Niels Möller6b9dec02018-11-02 08:50:21 +010092 virtual bool IsFile(const std::string& pathname) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020094 // Determines the size of the file indicated by path.
Niels Möller6b9dec02018-11-02 08:50:21 +010095 virtual bool GetFileSize(const std::string& path, size_t* size) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096};
97
98class Filesystem {
99 public:
Niels Möller6b9dec02018-11-02 08:50:21 +0100100 static bool DeleteFile(const std::string& filename) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200101 return GetFilesystem()->DeleteFile(filename);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102 }
103
Niels Möller6b9dec02018-11-02 08:50:21 +0100104 static bool MoveFile(const std::string& old_path,
105 const std::string& new_path) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200106 return GetFilesystem()->MoveFile(old_path, new_path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 }
108
Niels Möller6b9dec02018-11-02 08:50:21 +0100109 static bool IsFile(const std::string& pathname) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200110 return GetFilesystem()->IsFile(pathname);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111 }
112
Niels Möller6b9dec02018-11-02 08:50:21 +0100113 static bool GetFileSize(const std::string& path, size_t* size) {
Niels Möllerae82ffa2018-06-25 09:52:21 +0200114 return GetFilesystem()->GetFileSize(path, size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115 }
116
117 private:
Niels Möllerae82ffa2018-06-25 09:52:21 +0200118 static FilesystemInterface* GetFilesystem();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
120};
121
122} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // RTC_BASE_FILEUTILS_H_