Kea: fd.cc Source File

| Kea 3.2.0-git

fd.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <util/io/fd.h>
10
11#include <unistd.h>
12#include <cerrno>
13
14namespace isc {
15namespace util {
16 namespace io {
17
18bool
19write_data(const int fd, const void *buffer_v, const size_t length) {
20 const unsigned char* buffer(static_cast<const unsigned char*>(buffer_v));
21 size_t remaining = length; // Amount remaining to be written
22
23 // Just keep writing until all is written
24 while (remaining > 0) {
25 const int written = write(fd, buffer, remaining);
26 if (written == -1) {
27 if (errno == EINTR) { // Just keep going
28 continue;
29 } else {
30 return (false);
31 }
32
33 } else if (written > 0) {
34 // Wrote "written" bytes from the buffer
35 remaining -= written;
36 buffer += written;
37
38 } else {
39 // Wrote zero bytes from the buffer. We should not get here as any
40 // error that causes zero bytes to be written should have returned
41 // -1. However, write(2) can return 0, and in this case we
42 // interpret it as an error.
43 return (false);
44 }
45 }
46 return (true);
47}
48
49ssize_t
50read_data(const int fd, void *buffer_v, const size_t length) {
51 unsigned char* buffer(static_cast<unsigned char*>(buffer_v));
52 size_t remaining = length; // Amount remaining to be read
53
54 while (remaining > 0) {
55 const int amount = read(fd, buffer, remaining);
56 if (amount == -1) {
57 if (errno == EINTR) { // Continue on interrupted call
58 continue;
59 } else {
60 return (-1);
61 }
62 } else if (amount > 0) {
63 // Read "amount" bytes into the buffer
64 remaining -= amount;
65 buffer += amount;
66 } else {
67 // EOF - end the read
68 break;
69 }
70 }
71
72 // Return total number of bytes read
73 return (static_cast<ssize_t>(length - remaining));
74}
75
76}
77}
78}
fd.h
Wrappers around common unix fd manipulation functions.
isc::util::io
Definition fd.cc:16
isc::util::io::read_data
ssize_t read_data(const int fd, void *buffer_v, const size_t length)
Definition fd.cc:50
isc::util::io::write_data
bool write_data(const int fd, const void *buffer_v, const size_t length)
Definition fd.cc:19
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18