blob: 1086f3c7f7b2a26839ef4ca689b2e2d38a452d47 [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#include "rtc_base/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014
15#if defined(WEBRTC_WIN)
Yves Gerey2e00abc2018-10-05 15:39:24 +020016#include "rtc_base/stringutils.h" // for ToUtf16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/win32filesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/unixfilesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020#endif
21
22#if !defined(WEBRTC_WIN)
23#define MAX_PATH 260
24#endif
25
26namespace rtc {
27
28//////////////////////////
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
Yves Gerey665174f2018-06-19 15:03:05 +020036// Constructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037DirectoryIterator::DirectoryIterator()
38#ifdef WEBRTC_WIN
39 : handle_(INVALID_HANDLE_VALUE) {
40#else
deadbeef37f5ecf2017-02-27 14:06:41 -080041 : dir_(nullptr),
42 dirent_(nullptr){
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043#endif
44}
45
Yves Gerey665174f2018-06-19 15:03:05 +020046// Destructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047DirectoryIterator::~DirectoryIterator() {
48#if defined(WEBRTC_WIN)
49 if (handle_ != INVALID_HANDLE_VALUE)
50 ::FindClose(handle_);
51#else
52 if (dir_)
53 closedir(dir_);
54#endif
55}
56
Yves Gerey665174f2018-06-19 15:03:05 +020057// Starts traversing a directory.
58// dir is the directory to traverse
59// returns true if the directory exists and is valid
Niels Möller6b9dec02018-11-02 08:50:21 +010060bool DirectoryIterator::Iterate(const std::string& dir) {
61 directory_ = dir;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062#if defined(WEBRTC_WIN)
63 if (handle_ != INVALID_HANDLE_VALUE)
64 ::FindClose(handle_);
Niels Möller6b9dec02018-11-02 08:50:21 +010065 std::string d = dir + '*';
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
67 if (handle_ == INVALID_HANDLE_VALUE)
68 return false;
69#else
deadbeef37f5ecf2017-02-27 14:06:41 -080070 if (dir_ != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071 closedir(dir_);
72 dir_ = ::opendir(directory_.c_str());
deadbeef37f5ecf2017-02-27 14:06:41 -080073 if (dir_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 return false;
75 dirent_ = readdir(dir_);
deadbeef37f5ecf2017-02-27 14:06:41 -080076 if (dirent_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 return false;
78
Niels Möller6b9dec02018-11-02 08:50:21 +010079 if (::stat(PathName().c_str(), &stat_) != 0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 return false;
81#endif
82 return true;
83}
84
Yves Gerey665174f2018-06-19 15:03:05 +020085// Advances to the next file
86// returns true if there were more files in the directory.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087bool DirectoryIterator::Next() {
88#if defined(WEBRTC_WIN)
89 return ::FindNextFile(handle_, &data_) == TRUE;
90#else
91 dirent_ = ::readdir(dir_);
deadbeef37f5ecf2017-02-27 14:06:41 -080092 if (dirent_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 return false;
94
Niels Möller6b9dec02018-11-02 08:50:21 +010095 return ::stat(PathName().c_str(), &stat_) == 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096#endif
97}
98
Yves Gerey665174f2018-06-19 15:03:05 +020099// returns true if the file currently pointed to is a directory
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100bool DirectoryIterator::IsDirectory() const {
101#if defined(WEBRTC_WIN)
102 return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
103#else
104 return S_ISDIR(stat_.st_mode);
105#endif
106}
107
Yves Gerey665174f2018-06-19 15:03:05 +0200108// returns the name of the file currently pointed to
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109std::string DirectoryIterator::Name() const {
110#if defined(WEBRTC_WIN)
111 return ToUtf8(data_.cFileName);
112#else
kwiberg22487b22016-09-13 01:17:10 -0700113 RTC_DCHECK(dirent_);
Niels Möller6b9dec02018-11-02 08:50:21 +0100114 return std::string(dirent_->d_name);
115#endif
116}
117
118// returns the name of the file currently pointed to
119std::string DirectoryIterator::PathName() const {
120#if defined(WEBRTC_WIN)
121 return directory_ + "\\" + ToUtf8(data_.cFileName);
122#else
123 RTC_DCHECK(dirent_);
124 return directory_ + "/" + dirent_->d_name;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125#endif
126}
127
Niels Möllerae82ffa2018-06-25 09:52:21 +0200128FilesystemInterface* Filesystem::GetFilesystem() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129#if defined(WEBRTC_WIN)
Niels Möllerae82ffa2018-06-25 09:52:21 +0200130 static FilesystemInterface* const filesystem = new Win32Filesystem();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131#else
Niels Möllerae82ffa2018-06-25 09:52:21 +0200132 static FilesystemInterface* const filesystem = new UnixFilesystem();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133#endif
Niels Möllerae82ffa2018-06-25 09:52:21 +0200134
135 return filesystem;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136}
137
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138} // namespace rtc