Skip to content
Snippets Groups Projects
Commit c0f60596 authored by François Degros's avatar François Degros Committed by Copybara-Service
Browse files

[zip] Remove duplicated code

Made FilePathWriterDelegate a subclass of FileWriterDelegate.
This allows to reuse code.

BUG=None
TEST=autoninja -C out/Default zlib_unittests && out/Default/zlib_unittests
TEST=autoninja -C out/Default components_unittests && out/Default/components_unittests --gtest_filter='UnzipTest.*'

Change-Id: I96bf437cddcd853850e79d820d9561cfbbd80fe2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3511611


Reviewed-by: default avatarAlex Danilo <adanilo@chromium.org>
Commit-Queue: François Degros <fdegros@chromium.org>
Cr-Commit-Position: refs/heads/main@{#980438}
NOKEYCHECK=True
GitOrigin-RevId: a6c10f7a37219d77efec93bf1726d58728eca800
parent f92a1884
No related branches found
No related tags found
No related merge requests found
......@@ -527,9 +527,9 @@ void FileWriterDelegate::SetPosixFilePermissions(int mode) {
// FilePathWriterDelegate ------------------------------------------------------
FilePathWriterDelegate::FilePathWriterDelegate(
const base::FilePath& output_file_path)
: output_file_path_(output_file_path) {}
FilePathWriterDelegate::FilePathWriterDelegate(base::FilePath output_file_path)
: FileWriterDelegate(base::File()),
output_file_path_(std::move(output_file_path)) {}
FilePathWriterDelegate::~FilePathWriterDelegate() {}
......@@ -539,24 +539,9 @@ bool FilePathWriterDelegate::PrepareOutput() {
if (!base::CreateDirectory(output_file_path_.DirName()))
return false;
file_.Initialize(output_file_path_,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
return file_.IsValid();
}
bool FilePathWriterDelegate::WriteBytes(const char* data, int num_bytes) {
return num_bytes == file_.WriteAtCurrentPos(data, num_bytes);
}
void FilePathWriterDelegate::SetTimeModified(const base::Time& time) {
file_.Close();
base::TouchFile(output_file_path_, base::Time::Now(), time);
}
void FilePathWriterDelegate::SetPosixFilePermissions(int mode) {
#if defined(OS_POSIX)
zip::SetPosixFilePermissions(file_.GetPlatformFile(), mode);
#endif
owned_file_.Initialize(output_file_path_, base::File::FLAG_CREATE_ALWAYS |
base::File::FLAG_WRITE);
return FileWriterDelegate::PrepareOutput();
}
} // namespace zip
......@@ -310,7 +310,7 @@ class FileWriterDelegate : public WriterDelegate {
// Gets the number of bytes written into the file.
int64_t file_length() { return file_length_; }
private:
protected:
// The delegate can optionally own the file it modifies, in which case
// owned_file_ is set and file_ is an alias for owned_file_.
base::File owned_file_;
......@@ -322,9 +322,9 @@ class FileWriterDelegate : public WriterDelegate {
};
// A writer delegate that writes a file at a given path.
class FilePathWriterDelegate : public WriterDelegate {
class FilePathWriterDelegate : public FileWriterDelegate {
public:
explicit FilePathWriterDelegate(const base::FilePath& output_file_path);
explicit FilePathWriterDelegate(base::FilePath output_file_path);
FilePathWriterDelegate(const FilePathWriterDelegate&) = delete;
FilePathWriterDelegate& operator=(const FilePathWriterDelegate&) = delete;
......@@ -336,20 +336,8 @@ class FilePathWriterDelegate : public WriterDelegate {
// Creates the output file and any necessary intermediate directories.
bool PrepareOutput() override;
// Writes |num_bytes| bytes of |data| to the file, returning false if not all
// bytes could be written.
bool WriteBytes(const char* data, int num_bytes) override;
// Sets the last-modified time of the data.
void SetTimeModified(const base::Time& time) override;
// On POSIX systems, sets the file to be executable if the source file was
// executable.
void SetPosixFilePermissions(int mode) override;
private:
base::FilePath output_file_path_;
base::File file_;
const base::FilePath output_file_path_;
};
} // namespace zip
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment