| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2009 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/file_writer.h" | 
| Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | #include <errno.h> | 
|  | 20 |  | 
|  | 21 | namespace chromeos_update_engine { | 
|  | 22 |  | 
|  | 23 | int DirectFileWriter::Open(const char* path, int flags, mode_t mode) { | 
|  | 24 | CHECK_EQ(fd_, -1); | 
|  | 25 | fd_ = open(path, flags, mode); | 
|  | 26 | if (fd_ < 0) | 
|  | 27 | return -errno; | 
|  | 28 | return 0; | 
|  | 29 | } | 
|  | 30 |  | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 31 | bool DirectFileWriter::Write(const void* bytes, size_t count) { | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 32 | CHECK_GE(fd_, 0); | 
|  | 33 | const char* char_bytes = reinterpret_cast<const char*>(bytes); | 
|  | 34 |  | 
|  | 35 | size_t bytes_written = 0; | 
|  | 36 | while (bytes_written < count) { | 
|  | 37 | ssize_t rc = write(fd_, char_bytes + bytes_written, | 
|  | 38 | count - bytes_written); | 
|  | 39 | if (rc < 0) | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 40 | return false; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | bytes_written += rc; | 
|  | 42 | } | 
|  | 43 | CHECK_EQ(bytes_written, count); | 
| Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 44 | return bytes_written == count; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
|  | 47 | int DirectFileWriter::Close() { | 
|  | 48 | CHECK_GE(fd_, 0); | 
|  | 49 | int rc = close(fd_); | 
|  | 50 |  | 
|  | 51 | // This can be any negative number that's not -1. This way, this FileWriter | 
|  | 52 | // won't be used again for another file. | 
|  | 53 | fd_ = -2; | 
|  | 54 |  | 
|  | 55 | if (rc < 0) | 
|  | 56 | return -errno; | 
|  | 57 | return rc; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | }  // namespace chromeos_update_engine |