1//
2// Copyright (C) 2013 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//
16
17#include "shill/file_io.h"
18
19#include <fcntl.h>
20#include <unistd.h>
21
22#include <base/posix/eintr_wrapper.h>
23
24namespace shill {
25
26namespace {
27
28base::LazyInstance<FileIO> g_file_io = LAZY_INSTANCE_INITIALIZER;
29
30}  // namespace
31
32FileIO::FileIO() {}
33
34FileIO::~FileIO() {}
35
36// static
37FileIO* FileIO::GetInstance() {
38  return g_file_io.Pointer();
39}
40
41ssize_t FileIO::Write(int fd, const void* buf, size_t count) {
42  return HANDLE_EINTR(write(fd, buf, count));
43}
44
45ssize_t FileIO::Read(int fd, void* buf, size_t count) {
46  return HANDLE_EINTR(read(fd, buf, count));
47}
48
49int FileIO::Close(int fd) {
50  return IGNORE_EINTR(close(fd));
51}
52
53int FileIO::SetFdNonBlocking(int fd) {
54  const int flags = HANDLE_EINTR(fcntl(fd, F_GETFL)) | O_NONBLOCK;
55  return HANDLE_EINTR(fcntl(fd, F_SETFL,  flags));
56}
57
58}  // namespace shill
59