]> git.cworth.org Git - tar/blob - gnu/safe-read.c
upstream: Fix extraction of device nodes.
[tar] / gnu / safe-read.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* An interface to read and write that retries after interrupts.
4
5    Copyright (C) 1993-1994, 1998, 2002-2006, 2009-2010 Free Software
6    Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <config.h>
22
23 /* Specification.  */
24 #ifdef SAFE_WRITE
25 # include "safe-write.h"
26 #else
27 # include "safe-read.h"
28 #endif
29
30 /* Get ssize_t.  */
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include <errno.h>
35
36 #ifdef EINTR
37 # define IS_EINTR(x) ((x) == EINTR)
38 #else
39 # define IS_EINTR(x) 0
40 #endif
41
42 #include <limits.h>
43
44 #ifdef SAFE_WRITE
45 # define safe_rw safe_write
46 # define rw write
47 #else
48 # define safe_rw safe_read
49 # define rw read
50 # undef const
51 # define const /* empty */
52 #endif
53
54 /* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
55    interrupted.  Return the actual number of bytes read(written), zero for EOF,
56    or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error.  */
57 size_t
58 safe_rw (int fd, void const *buf, size_t count)
59 {
60   /* Work around a bug in Tru64 5.1.  Attempting to read more than
61      INT_MAX bytes fails with errno == EINVAL.  See
62      <http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>.
63      When decreasing COUNT, keep it block-aligned.  */
64   enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 };
65
66   for (;;)
67     {
68       ssize_t result = rw (fd, buf, count);
69
70       if (0 <= result)
71         return result;
72       else if (IS_EINTR (errno))
73         continue;
74       else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count)
75         count = BUGGY_READ_MAXIMUM;
76       else
77         return result;
78     }
79 }