OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "update_engine/file_writer.h" | 5 #include "update_engine/file_writer.h" |
6 #include <errno.h> | 6 #include <errno.h> |
7 | 7 |
8 namespace chromeos_update_engine { | 8 namespace chromeos_update_engine { |
9 | 9 |
10 int DirectFileWriter::Open(const char* path, int flags, mode_t mode) { | 10 int DirectFileWriter::Open(const char* path, int flags, mode_t mode) { |
11 CHECK_EQ(fd_, -1); | 11 CHECK_EQ(fd_, -1); |
12 fd_ = open(path, flags, mode); | 12 fd_ = open(path, flags, mode); |
13 if (fd_ < 0) | 13 if (fd_ < 0) |
14 return -errno; | 14 return -errno; |
15 return 0; | 15 return 0; |
16 } | 16 } |
17 | 17 |
18 int DirectFileWriter::Write(const void* bytes, size_t count) { | 18 ssize_t DirectFileWriter::Write(const void* bytes, size_t count) { |
19 CHECK_GE(fd_, 0); | 19 CHECK_GE(fd_, 0); |
20 const char* char_bytes = reinterpret_cast<const char*>(bytes); | 20 const char* char_bytes = reinterpret_cast<const char*>(bytes); |
21 | 21 |
22 size_t bytes_written = 0; | 22 size_t bytes_written = 0; |
23 while (bytes_written < count) { | 23 while (bytes_written < count) { |
24 ssize_t rc = write(fd_, char_bytes + bytes_written, | 24 ssize_t rc = write(fd_, char_bytes + bytes_written, |
25 count - bytes_written); | 25 count - bytes_written); |
26 if (rc < 0) | 26 if (rc < 0) |
27 return -errno; | 27 return -errno; |
28 bytes_written += rc; | 28 bytes_written += rc; |
29 } | 29 } |
30 CHECK_EQ(bytes_written, count); | 30 CHECK_EQ(bytes_written, count); |
31 return bytes_written; | 31 return bytes_written; |
32 } | 32 } |
33 | 33 |
34 int DirectFileWriter::Close() { | 34 int DirectFileWriter::Close() { |
35 CHECK_GE(fd_, 0); | 35 CHECK_GE(fd_, 0); |
36 int rc = close(fd_); | 36 int rc = close(fd_); |
37 | 37 |
38 // This can be any negative number that's not -1. This way, this FileWriter | 38 // This can be any negative number that's not -1. This way, this FileWriter |
39 // won't be used again for another file. | 39 // won't be used again for another file. |
40 fd_ = -2; | 40 fd_ = -2; |
41 | 41 |
42 if (rc < 0) | 42 if (rc < 0) |
43 return -errno; | 43 return -errno; |
44 return rc; | 44 return rc; |
45 } | 45 } |
46 | 46 |
47 } // namespace chromeos_update_engine | 47 } // namespace chromeos_update_engine |
OLD | NEW |