blob: 275aa70223d25f85fac8b7b829d5c6fefc888ebc [file] [log] [blame]
Gilad Arnold11c066f2012-05-10 14:37:25 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/file_descriptor.h"
6
7#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/types.h>
10
11#include <base/eintr_wrapper.h>
12
13namespace chromeos_update_engine {
14
15bool EintrSafeFileDescriptor::Open(const char* path, int flags, mode_t mode) {
16 CHECK_EQ(fd_, -1);
17 return ((fd_ = HANDLE_EINTR(open(path, flags, mode))) >= 0);
18}
19
20bool EintrSafeFileDescriptor::Open(const char* path, int flags) {
21 CHECK_EQ(fd_, -1);
22 return ((fd_ = HANDLE_EINTR(open(path, flags))) >= 0);
23}
24
25ssize_t EintrSafeFileDescriptor::Read(void* buf, size_t count) {
26 CHECK_GE(fd_, 0);
27 return HANDLE_EINTR(read(fd_, buf, count));
28}
29
30ssize_t EintrSafeFileDescriptor::Write(const void* buf, size_t count) {
31 CHECK_GE(fd_, 0);
32
33 // Attempt repeated writes, as long as some progress is being made.
34 char* char_buf = const_cast<char*>(reinterpret_cast<const char*>(buf));
35 ssize_t written = 0;
36 while (count > 0) {
37 ssize_t ret = HANDLE_EINTR(write(fd_, char_buf, count));
38
39 // Fail on either an error or no progress.
40 if (ret <= 0)
41 return (written ? written : ret);
42 written += ret;
43 count -= ret;
44 char_buf += ret;
45 }
46 return written;
47}
48
49bool EintrSafeFileDescriptor::Close() {
50 CHECK_GE(fd_, 0);
51 int ret = HANDLE_EINTR(close(fd_));
52 if (!ret)
53 fd_ = -1;
54 return !ret;
55}
56
57} // namespace chromeos_update_engine