blob: 0adbbac4b0885f06a61b98e600c77c4f1576c872 [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"
14#include "rtc_base/pathutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
16#if defined(WEBRTC_WIN)
Yves Gerey2e00abc2018-10-05 15:39:24 +020017#include "rtc_base/stringutils.h" // for ToUtf16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/win32filesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/unixfilesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#endif
22
23#if !defined(WEBRTC_WIN)
24#define MAX_PATH 260
25#endif
26
27namespace rtc {
28
29//////////////////////////
30// Directory Iterator //
31//////////////////////////
32
33// A DirectoryIterator is created with a given directory. It originally points
34// to the first file in the directory, and can be advanecd with Next(). This
35// allows you to get information about each file.
36
Yves Gerey665174f2018-06-19 15:03:05 +020037// Constructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038DirectoryIterator::DirectoryIterator()
39#ifdef WEBRTC_WIN
40 : handle_(INVALID_HANDLE_VALUE) {
41#else
deadbeef37f5ecf2017-02-27 14:06:41 -080042 : dir_(nullptr),
43 dirent_(nullptr){
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044#endif
45}
46
Yves Gerey665174f2018-06-19 15:03:05 +020047// Destructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048DirectoryIterator::~DirectoryIterator() {
49#if defined(WEBRTC_WIN)
50 if (handle_ != INVALID_HANDLE_VALUE)
51 ::FindClose(handle_);
52#else
53 if (dir_)
54 closedir(dir_);
55#endif
56}
57
Yves Gerey665174f2018-06-19 15:03:05 +020058// Starts traversing a directory.
59// dir is the directory to traverse
60// returns true if the directory exists and is valid
61bool DirectoryIterator::Iterate(const Pathname& dir) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 directory_ = dir.pathname();
63#if defined(WEBRTC_WIN)
64 if (handle_ != INVALID_HANDLE_VALUE)
65 ::FindClose(handle_);
66 std::string d = dir.pathname() + '*';
67 handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
68 if (handle_ == INVALID_HANDLE_VALUE)
69 return false;
70#else
deadbeef37f5ecf2017-02-27 14:06:41 -080071 if (dir_ != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 closedir(dir_);
73 dir_ = ::opendir(directory_.c_str());
deadbeef37f5ecf2017-02-27 14:06:41 -080074 if (dir_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 return false;
76 dirent_ = readdir(dir_);
deadbeef37f5ecf2017-02-27 14:06:41 -080077 if (dirent_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078 return false;
79
80 if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
81 return false;
82#endif
83 return true;
84}
85
Yves Gerey665174f2018-06-19 15:03:05 +020086// Advances to the next file
87// returns true if there were more files in the directory.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088bool DirectoryIterator::Next() {
89#if defined(WEBRTC_WIN)
90 return ::FindNextFile(handle_, &data_) == TRUE;
91#else
92 dirent_ = ::readdir(dir_);
deadbeef37f5ecf2017-02-27 14:06:41 -080093 if (dirent_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 return false;
95
96 return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
97#endif
98}
99
Yves Gerey665174f2018-06-19 15:03:05 +0200100// returns true if the file currently pointed to is a directory
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101bool DirectoryIterator::IsDirectory() const {
102#if defined(WEBRTC_WIN)
103 return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
104#else
105 return S_ISDIR(stat_.st_mode);
106#endif
107}
108
Yves Gerey665174f2018-06-19 15:03:05 +0200109// returns the name of the file currently pointed to
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110std::string DirectoryIterator::Name() const {
111#if defined(WEBRTC_WIN)
112 return ToUtf8(data_.cFileName);
113#else
kwiberg22487b22016-09-13 01:17:10 -0700114 RTC_DCHECK(dirent_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115 return dirent_->d_name;
116#endif
117}
118
Niels Möllerae82ffa2018-06-25 09:52:21 +0200119FilesystemInterface* Filesystem::GetFilesystem() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120#if defined(WEBRTC_WIN)
Niels Möllerae82ffa2018-06-25 09:52:21 +0200121 static FilesystemInterface* const filesystem = new Win32Filesystem();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122#else
Niels Möllerae82ffa2018-06-25 09:52:21 +0200123 static FilesystemInterface* const filesystem = new UnixFilesystem();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124#endif
Niels Möllerae82ffa2018-06-25 09:52:21 +0200125
126 return filesystem;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127}
128
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129} // namespace rtc