webrtc/rtc_base/fileutils.h
Qingsi Wang 2039ee7dce Revert "Delete rtc::Pathname"
This reverts commit 6b9dec0d16.

Reason for revert: speculative revert for breaking internal projects

Original change's description:
> Delete rtc::Pathname
> 
> Bug: webrtc:6424
> Change-Id: Iec01dc5dd1426d4558983b828b67af872107d723
> Reviewed-on: https://webrtc-review.googlesource.com/c/108400
> Commit-Queue: Niels Moller <nisse@webrtc.org>
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#25479}

TBR=kwiberg@webrtc.org,nisse@webrtc.org

Change-Id: I3129a81a1d8e36b3e6c67572410bdc478ec4d5e9
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:6424
Reviewed-on: https://webrtc-review.googlesource.com/c/109201
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Commit-Queue: Qingsi Wang <qingsi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25490}
2018-11-02 16:30:24 +00:00

128 lines
3.5 KiB
C++

/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_FILEUTILS_H_
#define RTC_BASE_FILEUTILS_H_
#include <string>
#if defined(WEBRTC_WIN)
#include <windows.h>
#else
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#endif // WEBRTC_WIN
#include "rtc_base/constructormagic.h"
namespace rtc {
class Pathname;
//////////////////////////
// Directory Iterator //
//////////////////////////
// A DirectoryIterator is created with a given directory. It originally points
// to the first file in the directory, and can be advanecd with Next(). This
// allows you to get information about each file.
class DirectoryIterator {
friend class Filesystem;
public:
// Constructor
DirectoryIterator();
// Destructor
virtual ~DirectoryIterator();
// Starts traversing a directory
// dir is the directory to traverse
// returns true if the directory exists and is valid
// The iterator will point to the first entry in the directory
virtual bool Iterate(const Pathname& path);
// Advances to the next file
// returns true if there were more files in the directory.
virtual bool Next();
// returns true if the file currently pointed to is a directory
virtual bool IsDirectory() const;
// returns the name of the file currently pointed to
virtual std::string Name() const;
private:
std::string directory_;
#if defined(WEBRTC_WIN)
WIN32_FIND_DATA data_;
HANDLE handle_;
#else
DIR* dir_;
struct dirent* dirent_;
struct stat stat_;
#endif
};
class FilesystemInterface {
public:
virtual ~FilesystemInterface() {}
// This will attempt to delete the path located at filename.
// It DCHECKs and returns false if the path points to a folder or a
// non-existent file.
virtual bool DeleteFile(const Pathname& filename) = 0;
// This moves a file from old_path to new_path, where "old_path" is a
// plain file. This DCHECKs and returns false if old_path points to a
// directory, and returns true if the function succeeds.
virtual bool MoveFile(const Pathname& old_path, const Pathname& new_path) = 0;
// Returns true if pathname refers to a directory
virtual bool IsFolder(const Pathname& pathname) = 0;
// Returns true if pathname refers to a file
virtual bool IsFile(const Pathname& pathname) = 0;
// Determines the size of the file indicated by path.
virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
};
class Filesystem {
public:
static bool DeleteFile(const Pathname& filename) {
return GetFilesystem()->DeleteFile(filename);
}
static bool MoveFile(const Pathname& old_path, const Pathname& new_path) {
return GetFilesystem()->MoveFile(old_path, new_path);
}
static bool IsFolder(const Pathname& pathname) {
return GetFilesystem()->IsFolder(pathname);
}
static bool IsFile(const Pathname& pathname) {
return GetFilesystem()->IsFile(pathname);
}
static bool GetFileSize(const Pathname& path, size_t* size) {
return GetFilesystem()->GetFileSize(path, size);
}
private:
static FilesystemInterface* GetFilesystem();
RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
};
} // namespace rtc
#endif // RTC_BASE_FILEUTILS_H_