blob: 73099907b0c64574b6f55b042cc1612bb5c13408 [file] [log] [blame]
Christopher Wileya59f7b92013-02-26 15:07:02 -08001// 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
12namespace shill {
13
14namespace {
15
Ben Chanbe0849f2014-08-15 23:02:32 -070016base::LazyInstance<FileIO> g_file_io = LAZY_INSTANCE_INITIALIZER;
Christopher Wileya59f7b92013-02-26 15:07:02 -080017
18} // namespace
19
20FileIO::FileIO() {}
21
22FileIO::~FileIO() {}
23
24// static
Paul Stewart8ae18742015-06-16 13:13:10 -070025FileIO* FileIO::GetInstance() {
Christopher Wileya59f7b92013-02-26 15:07:02 -080026 return g_file_io.Pointer();
27}
28
Paul Stewart8ae18742015-06-16 13:13:10 -070029ssize_t FileIO::Write(int fd, const void* buf, size_t count) {
Christopher Wileya59f7b92013-02-26 15:07:02 -080030 return HANDLE_EINTR(write(fd, buf, count));
31}
32
Paul Stewart8ae18742015-06-16 13:13:10 -070033ssize_t FileIO::Read(int fd, void* buf, size_t count) {
Christopher Wileya59f7b92013-02-26 15:07:02 -080034 return HANDLE_EINTR(read(fd, buf, count));
35}
36
37int FileIO::Close(int fd) {
Mike Frysinger2dd351c2014-05-14 16:27:34 -040038 return IGNORE_EINTR(close(fd));
Christopher Wileya59f7b92013-02-26 15:07:02 -080039}
40
41int 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