2014-11-14 14:48:12 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
#include "util/Files.h"
|
2016-10-21 17:56:45 -07:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-04-06 16:09:43 -07:00
|
|
|
#include <algorithm>
|
2014-11-14 14:48:12 -08:00
|
|
|
#include <cerrno>
|
2015-08-14 14:26:04 -07:00
|
|
|
#include <cstdio>
|
2014-11-14 14:48:12 -08:00
|
|
|
#include <string>
|
2016-10-21 17:56:45 -07:00
|
|
|
|
|
|
|
#include "android-base/errors.h"
|
|
|
|
#include "android-base/file.h"
|
|
|
|
#include "android-base/logging.h"
|
|
|
|
|
|
|
|
#include "util/Util.h"
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-08-19 10:38:36 -07:00
|
|
|
#ifdef _WIN32
|
2015-04-03 12:08:26 -07:00
|
|
|
// Windows includes.
|
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
using android::StringPiece;
|
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
namespace aapt {
|
2015-08-14 14:26:04 -07:00
|
|
|
namespace file {
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
FileType GetFileType(const StringPiece& path) {
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(path.data(), &sb) < 0) {
|
|
|
|
if (errno == ENOENT || errno == ENOTDIR) {
|
|
|
|
return FileType::kNonexistant;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
return FileType::kUnknown;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (S_ISREG(sb.st_mode)) {
|
|
|
|
return FileType::kRegular;
|
|
|
|
} else if (S_ISDIR(sb.st_mode)) {
|
|
|
|
return FileType::kDirectory;
|
|
|
|
} else if (S_ISCHR(sb.st_mode)) {
|
|
|
|
return FileType::kCharDev;
|
|
|
|
} else if (S_ISBLK(sb.st_mode)) {
|
|
|
|
return FileType::kBlockDev;
|
|
|
|
} else if (S_ISFIFO(sb.st_mode)) {
|
|
|
|
return FileType::kFifo;
|
2015-04-03 12:08:26 -07:00
|
|
|
#if defined(S_ISLNK)
|
2016-10-21 17:56:45 -07:00
|
|
|
} else if (S_ISLNK(sb.st_mode)) {
|
|
|
|
return FileType::kSymlink;
|
2015-04-03 12:08:26 -07:00
|
|
|
#endif
|
|
|
|
#if defined(S_ISSOCK)
|
2016-10-21 17:56:45 -07:00
|
|
|
} else if (S_ISSOCK(sb.st_mode)) {
|
|
|
|
return FileType::kSocket;
|
2015-04-03 12:08:26 -07:00
|
|
|
#endif
|
2016-10-21 17:56:45 -07:00
|
|
|
} else {
|
|
|
|
return FileType::kUnknown;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline static int MkdirImpl(const StringPiece& path) {
|
2015-08-19 10:38:36 -07:00
|
|
|
#ifdef _WIN32
|
2017-01-16 15:07:21 -08:00
|
|
|
return _mkdir(path.to_string().c_str());
|
2014-11-14 14:48:12 -08:00
|
|
|
#else
|
2017-01-16 15:07:21 -08:00
|
|
|
return mkdir(path.to_string().c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP);
|
2014-11-14 14:48:12 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mkdirs(const StringPiece& path) {
|
2016-10-21 17:56:45 -07:00
|
|
|
const char* start = path.begin();
|
|
|
|
const char* end = path.end();
|
|
|
|
for (const char* current = start; current != end; ++current) {
|
|
|
|
if (*current == sDirSep && current != start) {
|
|
|
|
StringPiece parent_path(start, current - start);
|
|
|
|
int result = MkdirImpl(parent_path);
|
|
|
|
if (result < 0 && errno != EEXIST) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
}
|
|
|
|
return MkdirImpl(path) == 0 || errno == EEXIST;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2015-04-09 19:53:22 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
StringPiece GetStem(const StringPiece& path) {
|
|
|
|
const char* start = path.begin();
|
|
|
|
const char* end = path.end();
|
|
|
|
for (const char* current = end - 1; current != start - 1; --current) {
|
|
|
|
if (*current == sDirSep) {
|
|
|
|
return StringPiece(start, current - start);
|
2015-04-09 19:53:22 -07:00
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
}
|
|
|
|
return {};
|
2015-04-09 19:53:22 -07:00
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
StringPiece GetFilename(const StringPiece& path) {
|
|
|
|
const char* end = path.end();
|
|
|
|
const char* last_dir_sep = path.begin();
|
|
|
|
for (const char* c = path.begin(); c != end; ++c) {
|
|
|
|
if (*c == sDirSep) {
|
|
|
|
last_dir_sep = c + 1;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
}
|
|
|
|
return StringPiece(last_dir_sep, end - last_dir_sep);
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
StringPiece GetExtension(const StringPiece& path) {
|
|
|
|
StringPiece filename = GetFilename(path);
|
|
|
|
const char* const end = filename.end();
|
|
|
|
const char* c = std::find(filename.begin(), end, '.');
|
|
|
|
if (c != end) {
|
|
|
|
return StringPiece(c, end - c);
|
|
|
|
}
|
|
|
|
return {};
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void AppendPath(std::string* base, StringPiece part) {
|
|
|
|
CHECK(base != nullptr);
|
|
|
|
const bool base_has_trailing_sep =
|
|
|
|
(!base->empty() && *(base->end() - 1) == sDirSep);
|
|
|
|
const bool part_has_leading_sep =
|
|
|
|
(!part.empty() && *(part.begin()) == sDirSep);
|
|
|
|
if (base_has_trailing_sep && part_has_leading_sep) {
|
|
|
|
// Remove the part's leading sep
|
|
|
|
part = part.substr(1, part.size() - 1);
|
|
|
|
} else if (!base_has_trailing_sep && !part_has_leading_sep) {
|
|
|
|
// None of the pieces has a separator.
|
|
|
|
*base += sDirSep;
|
|
|
|
}
|
|
|
|
base->append(part.data(), part.size());
|
2016-03-09 13:11:25 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
std::string PackageToPath(const StringPiece& package) {
|
|
|
|
std::string out_path;
|
|
|
|
for (StringPiece part : util::Tokenize(package, '.')) {
|
|
|
|
AppendPath(&out_path, part);
|
|
|
|
}
|
|
|
|
return out_path;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Maybe<android::FileMap> MmapPath(const StringPiece& path,
|
|
|
|
std::string* out_error) {
|
|
|
|
std::unique_ptr<FILE, decltype(fclose)*> f = {fopen(path.data(), "rb"),
|
|
|
|
fclose};
|
|
|
|
if (!f) {
|
|
|
|
if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
|
|
|
|
return {};
|
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
int fd = fileno(f.get());
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
struct stat filestats = {};
|
|
|
|
if (fstat(fd, &filestats) != 0) {
|
|
|
|
if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
|
|
|
|
return {};
|
|
|
|
}
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
android::FileMap filemap;
|
|
|
|
if (filestats.st_size == 0) {
|
|
|
|
// mmap doesn't like a length of 0. Instead we return an empty FileMap.
|
|
|
|
return std::move(filemap);
|
|
|
|
}
|
2016-01-11 13:10:24 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (!filemap.create(path.data(), fd, 0, filestats.st_size, true)) {
|
|
|
|
if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return std::move(filemap);
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool AppendArgsFromFile(const StringPiece& path,
|
|
|
|
std::vector<std::string>* out_arglist,
|
|
|
|
std::string* out_error) {
|
|
|
|
std::string contents;
|
2017-01-16 15:07:21 -08:00
|
|
|
if (!android::base::ReadFileToString(path.to_string(), &contents)) {
|
2016-10-21 17:56:45 -07:00
|
|
|
if (out_error) *out_error = "failed to read argument-list file";
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-28 11:12:38 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
for (StringPiece line : util::Tokenize(contents, ' ')) {
|
|
|
|
line = util::TrimWhitespace(line);
|
|
|
|
if (!line.empty()) {
|
2017-01-16 15:07:21 -08:00
|
|
|
out_arglist->push_back(line.to_string());
|
2016-04-28 11:12:38 -07:00
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
}
|
|
|
|
return true;
|
2016-04-28 11:12:38 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool FileFilter::SetPattern(const StringPiece& pattern) {
|
|
|
|
pattern_tokens_ = util::SplitAndLowercase(pattern, ':');
|
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileFilter::operator()(const std::string& filename, FileType type) const {
|
2016-10-21 17:56:45 -07:00
|
|
|
if (filename == "." || filename == "..") {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
const char kDir[] = "dir";
|
|
|
|
const char kFile[] = "file";
|
|
|
|
const size_t filename_len = filename.length();
|
|
|
|
bool chatty = true;
|
|
|
|
for (const std::string& token : pattern_tokens_) {
|
|
|
|
const char* token_str = token.c_str();
|
|
|
|
if (*token_str == '!') {
|
|
|
|
chatty = false;
|
|
|
|
token_str++;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (strncasecmp(token_str, kDir, sizeof(kDir)) == 0) {
|
|
|
|
if (type != FileType::kDirectory) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
token_str += sizeof(kDir);
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (strncasecmp(token_str, kFile, sizeof(kFile)) == 0) {
|
|
|
|
if (type != FileType::kRegular) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
token_str += sizeof(kFile);
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool ignore = false;
|
|
|
|
size_t n = strlen(token_str);
|
|
|
|
if (*token_str == '*') {
|
|
|
|
// Math suffix.
|
|
|
|
token_str++;
|
|
|
|
n--;
|
|
|
|
if (n <= filename_len) {
|
|
|
|
ignore =
|
|
|
|
strncasecmp(token_str, filename.c_str() + filename_len - n, n) == 0;
|
|
|
|
}
|
|
|
|
} else if (n > 1 && token_str[n - 1] == '*') {
|
|
|
|
// Match prefix.
|
|
|
|
ignore = strncasecmp(token_str, filename.c_str(), n - 1) == 0;
|
|
|
|
} else {
|
|
|
|
ignore = strcasecmp(token_str, filename.c_str()) == 0;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (ignore) {
|
|
|
|
if (chatty) {
|
|
|
|
diag_->Warn(DiagMessage()
|
|
|
|
<< "skipping "
|
|
|
|
<< (type == FileType::kDirectory ? "dir '" : "file '")
|
|
|
|
<< filename << "' due to ignore pattern '" << token << "'");
|
|
|
|
}
|
|
|
|
return false;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
}
|
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
} // namespace file
|
|
|
|
} // namespace aapt
|