Christopher Wiley | a59f7b9 | 2013-02-26 15:07:02 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 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 "shill/file_io.h" |
| 6 | |
| 7 | #include <fcntl.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include <base/posix/eintr_wrapper.h> |
| 11 | |
| 12 | namespace shill { |
| 13 | |
| 14 | namespace { |
| 15 | |
Ben Chan | be0849f | 2014-08-15 23:02:32 -0700 | [diff] [blame] | 16 | base::LazyInstance<FileIO> g_file_io = LAZY_INSTANCE_INITIALIZER; |
Christopher Wiley | a59f7b9 | 2013-02-26 15:07:02 -0800 | [diff] [blame] | 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | FileIO::FileIO() {} |
| 21 | |
| 22 | FileIO::~FileIO() {} |
| 23 | |
| 24 | // static |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 25 | FileIO* FileIO::GetInstance() { |
Christopher Wiley | a59f7b9 | 2013-02-26 15:07:02 -0800 | [diff] [blame] | 26 | return g_file_io.Pointer(); |
| 27 | } |
| 28 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 29 | ssize_t FileIO::Write(int fd, const void* buf, size_t count) { |
Christopher Wiley | a59f7b9 | 2013-02-26 15:07:02 -0800 | [diff] [blame] | 30 | return HANDLE_EINTR(write(fd, buf, count)); |
| 31 | } |
| 32 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 33 | ssize_t FileIO::Read(int fd, void* buf, size_t count) { |
Christopher Wiley | a59f7b9 | 2013-02-26 15:07:02 -0800 | [diff] [blame] | 34 | return HANDLE_EINTR(read(fd, buf, count)); |
| 35 | } |
| 36 | |
| 37 | int FileIO::Close(int fd) { |
Mike Frysinger | 2dd351c | 2014-05-14 16:27:34 -0400 | [diff] [blame] | 38 | return IGNORE_EINTR(close(fd)); |
Christopher Wiley | a59f7b9 | 2013-02-26 15:07:02 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | int FileIO::SetFdNonBlocking(int fd) { |
| 42 | const int flags = HANDLE_EINTR(fcntl(fd, F_GETFL)) | O_NONBLOCK; |
| 43 | return HANDLE_EINTR(fcntl(fd, F_SETFL, flags)); |
| 44 | } |
| 45 | |
| 46 | } // namespace shill |