diff options
author | adlr@google.com <adlr@google.com@06c00378-0e64-4dae-be16-12b19f9950a1> | 2009-12-04 20:57:17 +0000 |
---|---|---|
committer | adlr@google.com <adlr@google.com@06c00378-0e64-4dae-be16-12b19f9950a1> | 2009-12-04 20:57:17 +0000 |
commit | 3defe6acb3609e70e851a6eff062577d25a2af9d (patch) | |
tree | 341e979027fde117dd8906483db7a5c703a2e1cf /file_writer.cc | |
parent | c98a7edf648aad88b3f66df3b5a7d43d6a6d7fa9 (diff) |
Missed new files in last commit
Review URL: http://codereview.chromium.org/465067
git-svn-id: svn://chrome-svn/chromeos/trunk@336 06c00378-0e64-4dae-be16-12b19f9950a1
Diffstat (limited to 'file_writer.cc')
-rw-r--r-- | file_writer.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/file_writer.cc b/file_writer.cc new file mode 100644 index 00000000..07bdb464 --- /dev/null +++ b/file_writer.cc @@ -0,0 +1,47 @@ +// Copyright (c) 2009 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_writer.h" +#include <errno.h> + +namespace chromeos_update_engine { + +int DirectFileWriter::Open(const char* path, int flags, mode_t mode) { + CHECK_EQ(fd_, -1); + fd_ = open(path, flags, mode); + if (fd_ < 0) + return -errno; + return 0; +} + +int DirectFileWriter::Write(const void* bytes, size_t count) { + CHECK_GE(fd_, 0); + const char* char_bytes = reinterpret_cast<const char*>(bytes); + + size_t bytes_written = 0; + while (bytes_written < count) { + ssize_t rc = write(fd_, char_bytes + bytes_written, + count - bytes_written); + if (rc < 0) + return -errno; + bytes_written += rc; + } + CHECK_EQ(bytes_written, count); + return bytes_written; +} + +int DirectFileWriter::Close() { + CHECK_GE(fd_, 0); + int rc = close(fd_); + + // This can be any negative number that's not -1. This way, this FileWriter + // won't be used again for another file. + fd_ = -2; + + if (rc < 0) + return -errno; + return rc; +} + +} // namespace chromeos_update_engine |