Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3a725dc1 authored by Adam Lesinski's avatar Adam Lesinski
Browse files

AAPT2: Fix GetFileType for unicode and long paths on Windows

Bug: 68262818
Test: manual
Change-Id: I4f02e544e45865984ff4e021a7d1e83f8baf24c3
parent d46f87a9
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -674,7 +674,7 @@ int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {


    // Determine how to compile the file based on its type.
    // Determine how to compile the file based on its type.
    auto compile_func = &CompileFile;
    auto compile_func = &CompileFile;
    if (path_data.resource_dir == "values") {
    if (path_data.resource_dir == "values" && path_data.extension == "xml") {
      compile_func = &CompileTable;
      compile_func = &CompileTable;
      // We use a different extension (not necessary anymore, but avoids altering the existing
      // We use a different extension (not necessary anymore, but avoids altering the existing
      // build system logic).
      // build system logic).
+16 −7
Original line number Original line Diff line number Diff line
@@ -34,7 +34,7 @@


#ifdef _WIN32
#ifdef _WIN32
// Windows includes.
// Windows includes.
#include <direct.h>
#include <windows.h>
#endif
#endif


using ::android::FileMap;
using ::android::FileMap;
@@ -46,21 +46,29 @@ using ::android::base::unique_fd;
namespace aapt {
namespace aapt {
namespace file {
namespace file {


FileType GetFileType(const std::string& path) {
// TODO(adamlesinski): I'd like to move this to ::android::base::utf8 but Windows does some macro
// trickery with 'stat' and things don't override very well.
#ifdef _WIN32
#ifdef _WIN32
FileType GetFileType(const std::string& path) {
  std::wstring path_utf16;
  std::wstring path_utf16;
  if (!::android::base::UTF8PathToWindowsLongPath(path.c_str(), &path_utf16)) {
  if (!::android::base::UTF8PathToWindowsLongPath(path.c_str(), &path_utf16)) {
    return FileType::kNonexistant;
    return FileType::kNonexistant;
  }
  }


  struct _stat64 sb;
  DWORD result = GetFileAttributesW(path_utf16.c_str());
  int result = _wstat64(path_utf16.c_str(), &sb);
  if (result == INVALID_FILE_ATTRIBUTES) {
    return FileType::kNonexistant;
  }

  if (result & FILE_ATTRIBUTE_DIRECTORY) {
    return FileType::kDirectory;
  }

  // Too many types to consider, just let open fail later.
  return FileType::kRegular;
}
#else
#else
FileType GetFileType(const std::string& path) {
  struct stat sb;
  struct stat sb;
  int result = stat(path.c_str(), &sb);
  int result = stat(path.c_str(), &sb);
#endif


  if (result == -1) {
  if (result == -1) {
    if (errno == ENOENT || errno == ENOTDIR) {
    if (errno == ENOENT || errno == ENOTDIR) {
@@ -91,6 +99,7 @@ FileType GetFileType(const std::string& path) {
    return FileType::kUnknown;
    return FileType::kUnknown;
  }
  }
}
}
#endif


bool mkdirs(const std::string& path) {
bool mkdirs(const std::string& path) {
  constexpr const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP;
  constexpr const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP;