1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/directory_lister.h"
#include <algorithm>
#include <utility>
#include "base/check.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/i18n/file_util_icu.h"
#include "base/location.h"
#include "base/notreached.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "net/base/net_errors.h"
namespace net {
namespace {
bool IsDotDot(const base::FilePath& path) {
return FILE_PATH_LITERAL("..") == path.BaseName().value();
}
// Comparator for sorting lister results. This uses the locale aware filename
// comparison function on the filenames for sorting in the user's locale.
// Static.
bool CompareAlphaDirsFirst(const DirectoryLister::DirectoryListerData& a,
const DirectoryLister::DirectoryListerData& b) {
// Parent directory before all else.
if (IsDotDot(b.info.GetName())) {
return false;
}
if (IsDotDot(a.info.GetName())) {
return true;
}
// Directories before regular files.
bool a_is_directory = a.info.IsDirectory();
bool b_is_directory = b.info.IsDirectory();
if (a_is_directory != b_is_directory)
return a_is_directory;
return base::i18n::LocaleAwareCompareFilenames(a.info.GetName(),
b.info.GetName());
}
void SortData(std::vector<DirectoryLister::DirectoryListerData>* data,
DirectoryLister::ListingType listing_type) {
// Sort the results. See the TODO below (this sort should be removed and we
// should do it from JS).
if (listing_type == DirectoryLister::ALPHA_DIRS_FIRST) {
std::sort(data->begin(), data->end(), CompareAlphaDirsFirst);
} else if (listing_type != DirectoryLister::NO_SORT &&
listing_type != DirectoryLister::NO_SORT_RECURSIVE) {
NOTREACHED();
}
}
} // namespace
DirectoryLister::DirectoryLister(const base::FilePath& dir,
DirectoryListerDelegate* delegate)
: DirectoryLister(dir, ALPHA_DIRS_FIRST, delegate) {}
DirectoryLister::DirectoryLister(const base::FilePath& dir,
ListingType type,
DirectoryListerDelegate* delegate)
: delegate_(delegate) {
core_ = base::MakeRefCounted<Core>(dir, type, this);
DCHECK(delegate_);
DCHECK(!dir.value().empty());
}
DirectoryLister::~DirectoryLister() {
Cancel();
}
void DirectoryLister::Start() {
base::ThreadPool::PostTask(
FROM_HERE,
{base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&Core::Start, core_));
}
void DirectoryLister::Cancel() {
core_->CancelOnOriginSequence();
}
DirectoryLister::Core::Core(const base::FilePath& dir,
ListingType type,
DirectoryLister* lister)
: dir_(dir),
type_(type),
origin_task_runner_(base::SequencedTaskRunner::GetCurrentDefault().get()),
lister_(lister) {
DCHECK(lister_);
}
DirectoryLister::Core::~Core() = default;
void DirectoryLister::Core::CancelOnOriginSequence() {
DCHECK(origin_task_runner_->RunsTasksInCurrentSequence());
base::subtle::NoBarrier_Store(&cancelled_, 1);
// Core must not call into |lister_| after cancellation, as the |lister_| may
// have been destroyed. Setting |lister_| to NULL ensures any such access will
// cause a crash.
lister_ = nullptr;
}
void DirectoryLister::Core::Start() {
auto directory_list = std::make_unique<DirectoryList>();
if (!base::DirectoryExists(dir_)) {
origin_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&Core::DoneOnOriginSequence, this,
std::move(directory_list), ERR_FILE_NOT_FOUND));
return;
}
int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES;
bool recursive;
if (NO_SORT_RECURSIVE != type_) {
types |= base::FileEnumerator::INCLUDE_DOT_DOT;
recursive = false;
} else {
recursive = true;
}
base::FileEnumerator file_enum(dir_, recursive, types);
base::FilePath path;
while (!(path = file_enum.Next()).empty()) {
// Abort on cancellation. This is purely for performance reasons.
// Correctness guarantees are made by checks in DoneOnOriginSequence.
if (IsCancelled())
return;
DirectoryListerData data;
data.info = file_enum.GetInfo();
data.path = path;
data.absolute_path = base::MakeAbsoluteFilePath(path);
directory_list->push_back(data);
/* TODO(brettw) bug 24107: It would be nice to send incremental updates.
We gather them all so they can be sorted, but eventually the sorting
should be done from JS to give more flexibility in the page. When we do
that, we can uncomment this to send incremental updates to the page.
const int kFilesPerEvent = 8;
if (file_data.size() < kFilesPerEvent)
continue;
origin_loop_->PostTask(
FROM_HERE,
base::BindOnce(&DirectoryLister::Core::SendData, file_data));
file_data.clear();
*/
}
SortData(directory_list.get(), type_);
origin_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&Core::DoneOnOriginSequence, this,
std::move(directory_list), OK));
}
bool DirectoryLister::Core::IsCancelled() const {
return !!base::subtle::NoBarrier_Load(&cancelled_);
}
void DirectoryLister::Core::DoneOnOriginSequence(
std::unique_ptr<DirectoryList> directory_list,
int error) const {
DCHECK(origin_task_runner_->RunsTasksInCurrentSequence());
// Need to check if the operation was before first callback.
if (IsCancelled())
return;
for (const auto& lister_data : *directory_list) {
lister_->OnListFile(lister_data);
// Need to check if the operation was cancelled during the callback.
if (IsCancelled())
return;
}
lister_->OnListDone(error);
}
void DirectoryLister::OnListFile(const DirectoryListerData& data) {
delegate_->OnListFile(data);
}
void DirectoryLister::OnListDone(int error) {
delegate_->OnListDone(error);
}
} // namespace net
|