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

Commit ff3a8080 authored by Adam Lesinski's avatar Adam Lesinski Committed by Android (Google) Code Review
Browse files

Merge "AAPT2: Move all file output to FileOutputStream"

parents 719c227c a693c4a3
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -268,6 +268,11 @@ inline ::std::ostream& operator<<(::std::ostream& out, const ResourceId& res_id)
  return out << res_id.to_string();
}

// For generic code to call 'using std::to_string; to_string(T);'.
inline std::string to_string(const ResourceId& id) {
  return id.to_string();
}

//
// ResourceType implementation.
//
+48 −45
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

#include <sys/stat.h>

#include <fstream>
#include <queue>
#include <unordered_map>
#include <vector>
@@ -212,6 +211,8 @@ class LinkContext : public IAaptContext {
// This delegate will attempt to masquerade any '@id/' references with ID 0xPPTTEEEE,
// where PP > 7f, as 0x7fPPEEEE. Any potential overlapping is verified and an error occurs if such
// an overlap exists.
//
// See b/37498913.
class FeatureSplitSymbolTableDelegate : public DefaultSymbolTableDelegate {
 public:
  FeatureSplitSymbolTableDelegate(IAaptContext* context) : context_(context) {
@@ -652,24 +653,26 @@ bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archiv
static bool WriteStableIdMapToPath(IDiagnostics* diag,
                                   const std::unordered_map<ResourceName, ResourceId>& id_map,
                                   const std::string& id_map_path) {
  std::ofstream fout(id_map_path, std::ofstream::binary);
  if (!fout) {
    diag->Error(DiagMessage(id_map_path) << strerror(errno));
  io::FileOutputStream fout(id_map_path);
  if (fout.HadError()) {
    diag->Error(DiagMessage(id_map_path) << "failed to open: " << fout.GetError());
    return false;
  }

  text::Printer printer(&fout);
  for (const auto& entry : id_map) {
    const ResourceName& name = entry.first;
    const ResourceId& id = entry.second;
    fout << name << " = " << id << "\n";
    printer.Print(name.to_string());
    printer.Print(" = ");
    printer.Println(id.to_string());
  }
  fout.Flush();

  if (!fout) {
    diag->Error(DiagMessage(id_map_path) << "failed writing to file: "
                                         << android::base::SystemErrorCodeToString(errno));
  if (fout.HadError()) {
    diag->Error(DiagMessage(id_map_path) << "failed writing to file: " << fout.GetError());
    return false;
  }

  return true;
}

@@ -985,36 +988,35 @@ class LinkCommand {

    file::AppendPath(&out_path, "R.java");

    std::ofstream fout(out_path, std::ofstream::binary);
    if (!fout) {
      context_->GetDiagnostics()->Error(DiagMessage()
                                        << "failed writing to '" << out_path
                                        << "': " << android::base::SystemErrorCodeToString(errno));
    io::FileOutputStream fout(out_path);
    if (fout.HadError()) {
      context_->GetDiagnostics()->Error(DiagMessage() << "failed writing to '" << out_path
                                                      << "': " << fout.GetError());
      return false;
    }

    std::unique_ptr<std::ofstream> fout_text;
    std::unique_ptr<io::FileOutputStream> fout_text;
    if (out_text_symbols_path) {
      fout_text =
          util::make_unique<std::ofstream>(out_text_symbols_path.value(), std::ofstream::binary);
      if (!*fout_text) {
        context_->GetDiagnostics()->Error(
            DiagMessage() << "failed writing to '" << out_text_symbols_path.value()
                          << "': " << android::base::SystemErrorCodeToString(errno));
      fout_text = util::make_unique<io::FileOutputStream>(out_text_symbols_path.value());
      if (fout_text->HadError()) {
        context_->GetDiagnostics()->Error(DiagMessage()
                                          << "failed writing to '" << out_text_symbols_path.value()
                                          << "': " << fout_text->GetError());
        return false;
      }
    }

    JavaClassGenerator generator(context_, table, java_options);
    if (!generator.Generate(package_name_to_generate, out_package, &fout, fout_text.get())) {
      context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError());
      context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.GetError());
      return false;
    }

    if (!fout) {
      context_->GetDiagnostics()->Error(DiagMessage()
                                        << "failed writing to '" << out_path
                                        << "': " << android::base::SystemErrorCodeToString(errno));
    fout.Flush();

    if (fout.HadError()) {
      context_->GetDiagnostics()->Error(DiagMessage() << "failed writing to '" << out_path
                                                      << "': " << fout.GetError());
      return false;
    }
    return true;
@@ -1142,18 +1144,19 @@ class LinkCommand {

    file::AppendPath(&out_path, "Manifest.java");

    std::ofstream fout(out_path, std::ofstream::binary);
    if (!fout) {
      context_->GetDiagnostics()->Error(DiagMessage()
                                        << "failed writing to '" << out_path
                                        << "': " << android::base::SystemErrorCodeToString(errno));
    io::FileOutputStream fout(out_path);
    if (fout.HadError()) {
      context_->GetDiagnostics()->Error(DiagMessage() << "failed to open '" << out_path
                                                      << "': " << fout.GetError());
      return false;
    }

    if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) {
      context_->GetDiagnostics()->Error(DiagMessage()
                                        << "failed writing to '" << out_path
                                        << "': " << android::base::SystemErrorCodeToString(errno));
    ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout);
    fout.Flush();

    if (fout.HadError()) {
      context_->GetDiagnostics()->Error(DiagMessage() << "failed writing to '" << out_path
                                                      << "': " << fout.GetError());
      return false;
    }
    return true;
@@ -1165,19 +1168,19 @@ class LinkCommand {
    }

    const std::string& out_path = out.value();
    std::ofstream fout(out_path, std::ofstream::binary);
    if (!fout) {
      context_->GetDiagnostics()->Error(DiagMessage()
                                        << "failed to open '" << out_path
                                        << "': " << android::base::SystemErrorCodeToString(errno));
    io::FileOutputStream fout(out_path);
    if (fout.HadError()) {
      context_->GetDiagnostics()->Error(DiagMessage() << "failed to open '" << out_path
                                                      << "': " << fout.GetError());
      return false;
    }

    proguard::WriteKeepSet(&fout, keep_set);
    if (!fout) {
      context_->GetDiagnostics()->Error(DiagMessage()
                                        << "failed writing to '" << out_path
                                        << "': " << android::base::SystemErrorCodeToString(errno));
    proguard::WriteKeepSet(keep_set, &fout);
    fout.Flush();

    if (fout.HadError()) {
      context_->GetDiagnostics()->Error(DiagMessage() << "failed writing to '" << out_path
                                                      << "': " << fout.GetError());
      return false;
    }
    return true;
+30 −15
Original line number Diff line number Diff line
@@ -25,6 +25,12 @@
#include "android-base/macros.h"
#include "android-base/utf8.h"

#if defined(_WIN32)
// This is only needed for O_CLOEXEC.
#include <windows.h>
#define O_CLOEXEC O_NOINHERIT
#endif

using ::android::base::SystemErrorCodeToString;
using ::android::base::unique_fd;

@@ -32,18 +38,20 @@ namespace aapt {
namespace io {

FileInputStream::FileInputStream(const std::string& path, size_t buffer_capacity)
    : FileInputStream(::android::base::utf8::open(path.c_str(), O_RDONLY | O_BINARY),
                      buffer_capacity) {
    : buffer_capacity_(buffer_capacity) {
  int mode = O_RDONLY | O_CLOEXEC | O_BINARY;
  fd_.reset(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode)));
  if (fd_ == -1) {
    error_ = SystemErrorCodeToString(errno);
  } else {
    buffer_.reset(new uint8_t[buffer_capacity_]);
  }
}

FileInputStream::FileInputStream(int fd, size_t buffer_capacity)
    : fd_(fd),
      buffer_capacity_(buffer_capacity),
      buffer_offset_(0u),
      buffer_size_(0u),
      total_byte_count_(0u) {
  if (fd_ == -1) {
    error_ = SystemErrorCodeToString(errno);
    : fd_(fd), buffer_capacity_(buffer_capacity) {
  if (fd_ < 0) {
    error_ = "Bad File Descriptor";
  } else {
    buffer_.reset(new uint8_t[buffer_capacity_]);
  }
@@ -100,9 +108,16 @@ std::string FileInputStream::GetError() const {
  return error_;
}

FileOutputStream::FileOutputStream(const std::string& path, int mode, size_t buffer_capacity)
    : FileOutputStream(unique_fd(::android::base::utf8::open(path.c_str(), mode)),
                       buffer_capacity) {
FileOutputStream::FileOutputStream(const std::string& path, size_t buffer_capacity)
    : buffer_capacity_(buffer_capacity) {
  int mode = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY;
  owned_fd_.reset(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode, 0666)));
  fd_ = owned_fd_.get();
  if (fd_ < 0) {
    error_ = SystemErrorCodeToString(errno);
  } else {
    buffer_.reset(new uint8_t[buffer_capacity_]);
  }
}

FileOutputStream::FileOutputStream(unique_fd fd, size_t buffer_capacity)
@@ -111,9 +126,9 @@ FileOutputStream::FileOutputStream(unique_fd fd, size_t buffer_capacity)
}

FileOutputStream::FileOutputStream(int fd, size_t buffer_capacity)
    : fd_(fd), buffer_capacity_(buffer_capacity), buffer_offset_(0u), total_byte_count_(0u) {
  if (fd_ == -1) {
    error_ = SystemErrorCodeToString(errno);
    : fd_(fd), buffer_capacity_(buffer_capacity) {
  if (fd_ < 0) {
    error_ = "Bad File Descriptor";
  } else {
    buffer_.reset(new uint8_t[buffer_capacity_]);
  }
+8 −9
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@
#include <memory>
#include <string>

#include "android-base/file.h"  // for O_BINARY
#include "android-base/macros.h"
#include "android-base/unique_fd.h"

@@ -55,15 +54,15 @@ class FileInputStream : public InputStream {
  android::base::unique_fd fd_;
  std::string error_;
  std::unique_ptr<uint8_t[]> buffer_;
  size_t buffer_capacity_;
  size_t buffer_offset_;
  size_t buffer_size_;
  size_t total_byte_count_;
  size_t buffer_capacity_ = 0u;
  size_t buffer_offset_ = 0u;
  size_t buffer_size_ = 0u;
  size_t total_byte_count_ = 0u;
};

class FileOutputStream : public OutputStream {
 public:
  explicit FileOutputStream(const std::string& path, int mode = O_RDWR | O_CREAT | O_BINARY,
  explicit FileOutputStream(const std::string& path,
                            size_t buffer_capacity = kDefaultBufferCapacity);

  // Does not take ownership of `fd`.
@@ -97,9 +96,9 @@ class FileOutputStream : public OutputStream {
  int fd_;
  std::string error_;
  std::unique_ptr<uint8_t[]> buffer_;
  size_t buffer_capacity_;
  size_t buffer_offset_;
  size_t total_byte_count_;
  size_t buffer_capacity_ = 0u;
  size_t buffer_offset_ = 0u;
  size_t total_byte_count_ = 0u;
};

}  // namespace io
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "io/FileStream.h"

#include "android-base/file.h"
#include "android-base/macros.h"
#include "android-base/test_utils.h"

Loading