A reusable file descriptor abstraction.
Includes an API definition for opening/closing, reading from / writing
to files. Also includes a concrete implementation based on standard
POSIX calls.
BUG=none
TEST=Builds and runs unit tests
Change-Id: I7eefacff396493938c1c327182b27e56750fe284
Reviewed-on: https://gerrit.chromium.org/gerrit/22405
Commit-Ready: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/SConstruct b/SConstruct
index e2f21d0..0030bd5 100644
--- a/SConstruct
+++ b/SConstruct
@@ -254,6 +254,7 @@
extent_writer.cc
filesystem_copier_action.cc
filesystem_iterator.cc
+ file_descriptor.cc
file_writer.cc
flimflam_proxy.cc
full_update_generator.cc
diff --git a/file_descriptor.cc b/file_descriptor.cc
new file mode 100644
index 0000000..275aa70
--- /dev/null
+++ b/file_descriptor.cc
@@ -0,0 +1,57 @@
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "update_engine/file_descriptor.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <base/eintr_wrapper.h>
+
+namespace chromeos_update_engine {
+
+bool EintrSafeFileDescriptor::Open(const char* path, int flags, mode_t mode) {
+ CHECK_EQ(fd_, -1);
+ return ((fd_ = HANDLE_EINTR(open(path, flags, mode))) >= 0);
+}
+
+bool EintrSafeFileDescriptor::Open(const char* path, int flags) {
+ CHECK_EQ(fd_, -1);
+ return ((fd_ = HANDLE_EINTR(open(path, flags))) >= 0);
+}
+
+ssize_t EintrSafeFileDescriptor::Read(void* buf, size_t count) {
+ CHECK_GE(fd_, 0);
+ return HANDLE_EINTR(read(fd_, buf, count));
+}
+
+ssize_t EintrSafeFileDescriptor::Write(const void* buf, size_t count) {
+ CHECK_GE(fd_, 0);
+
+ // Attempt repeated writes, as long as some progress is being made.
+ char* char_buf = const_cast<char*>(reinterpret_cast<const char*>(buf));
+ ssize_t written = 0;
+ while (count > 0) {
+ ssize_t ret = HANDLE_EINTR(write(fd_, char_buf, count));
+
+ // Fail on either an error or no progress.
+ if (ret <= 0)
+ return (written ? written : ret);
+ written += ret;
+ count -= ret;
+ char_buf += ret;
+ }
+ return written;
+}
+
+bool EintrSafeFileDescriptor::Close() {
+ CHECK_GE(fd_, 0);
+ int ret = HANDLE_EINTR(close(fd_));
+ if (!ret)
+ fd_ = -1;
+ return !ret;
+}
+
+} // namespace chromeos_update_engine
diff --git a/file_descriptor.h b/file_descriptor.h
new file mode 100644
index 0000000..74749c6
--- /dev/null
+++ b/file_descriptor.h
@@ -0,0 +1,108 @@
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_DESCRIPTOR_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_DESCRIPTOR_H__
+
+#include <errno.h>
+#include <sys/types.h>
+
+#include "base/logging.h"
+
+#include "update_engine/utils.h"
+
+// Abstraction for managing opening, reading, writing and closing of file
+// descriptors. This includes an abstract class and one standard implementation
+// based on POSIX system calls.
+//
+// TODO(garnold) this class is modeled after (and augments the functionality of)
+// the FileWriter class; ultimately, the latter should be replaced by the former
+// throughout the codebase. A few deviations from the original FileWriter:
+//
+// * Providing two flavors of Open()
+//
+// * A FileDescriptor is reusable and can be used to read/write multiple files
+// as long as open/close preconditions are respected.
+//
+// * Write() returns the number of bytes written: this appears to be more useful
+// for clients, who may wish to retry or otherwise do something useful with
+// the remaining data that was not written.
+
+namespace chromeos_update_engine {
+
+// An abstract class defining the file descriptor API.
+class FileDescriptor {
+ public:
+ FileDescriptor() {}
+ virtual ~FileDescriptor() {}
+
+ // Opens a file descriptor. The descriptor must be in the closed state prior
+ // to this call. Returns true on success, false otherwise. Specific
+ // implementations may set errno accordingly.
+ virtual bool Open(const char* path, int flags, mode_t mode) = 0;
+ virtual bool Open(const char* path, int flags) = 0;
+
+ // Reads from a file descriptor up to a given count. The descriptor must be
+ // open prior to this call. Returns the number of bytes read, or -1 on error.
+ // Specific implementations may set errno accordingly.
+ virtual ssize_t Read(void* buf, size_t count) = 0;
+
+ // Writes to a file descriptor. The descriptor must be open prior to this
+ // call. Returns the number of bytes written, or -1 if an error occurred and
+ // no bytes were written. Specific implementations may set errno accordingly.
+ virtual ssize_t Write(const void* buf, size_t count) = 0;
+
+ // Wrapper around close. The descriptor must be open prior to this call.
+ // Returns true on success, false otherwise. Specific implementations may set
+ // errno accordingly.
+ virtual bool Close() = 0;
+
+ // Indicates whether or not an implementation sets meaningful errno.
+ virtual bool IsSettingErrno() = 0;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FileDescriptor);
+};
+
+// A simple EINTR-immune wrapper implementation around standard system calls.
+class EintrSafeFileDescriptor : public FileDescriptor {
+ public:
+ EintrSafeFileDescriptor() : fd_(-1) {}
+ virtual ~EintrSafeFileDescriptor() {};
+
+ // Interface methods.
+ virtual bool Open(const char* path, int flags, mode_t mode);
+ virtual bool Open(const char* path, int flags);
+ virtual ssize_t Read(void* buf, size_t count);
+ virtual ssize_t Write(const void* buf, size_t count);
+ virtual bool Close();
+ virtual bool IsSettingErrno() {
+ return true;
+ }
+
+ private:
+ int fd_;
+};
+
+// A scoped closer for a FileDescriptor object.
+class ScopedFileDescriptorCloser {
+ public:
+ explicit ScopedFileDescriptorCloser(FileDescriptor* descriptor)
+ : descriptor_(descriptor) {}
+ ~ScopedFileDescriptorCloser() {
+ if (descriptor_ && !descriptor_->Close())
+ LOG(ERROR) << "FileDescriptor::Close failed: "
+ << (descriptor_->IsSettingErrno() ?
+ utils::ErrnoNumberAsString(errno) :
+ "(no error code)");
+ }
+ private:
+ FileDescriptor* descriptor_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedFileDescriptorCloser);
+};
+
+} // namespace chromeos_update_engine
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_DESCRIPTOR_H__