2 * libdpkg - Debian packaging suite library routines
3 * fdio.c - safe file descriptor based input/output
5 * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
30 #include <dpkg/fdio.h>
33 static struct shadow
{
36 } shadowtab
[MAXSHADOW
];
37 static size_t nshadow
;
39 static struct shadow
*find_shadow(int fd
)
45 rc
= fstat(fd
, &st
); assert(!rc
);
47 for (i
= j
= 0; i
< nshadow
; i
++) {
48 if (shadowtab
[i
].fd
== fd
) {
49 if (st
.st_dev
!= shadowtab
[i
].dev
|| st
.st_ino
!= shadowtab
[i
].ino
)
50 { close(shadowtab
[i
].shadow
); continue; }
51 return (&shadowtab
[i
]);
53 if (i
!= j
) shadowtab
[j
] = shadowtab
[i
];
60 int shadowed_fd_p(int fd
) { return (!!find_shadow(fd
)); }
62 static void write_to_shadow(int fd
, const void *p
, size_t sz
)
64 const struct shadow
*shadow
= find_shadow(fd
);
67 if (shadow
) { n
= write(shadow
->shadow
, p
, sz
); assert((size_t)n
== sz
); }
70 void save_copy(const char *path
, const char *label
, const char *ext
)
77 sprintf(buf
, "/tmp/mdw/%s#%d.%s", label
, i
, ext
);
78 if (!link(path
, buf
)) break;
79 assert(errno
== EEXIST
);
82 fprintf(stderr
, ";; save `%s' as `%s'\n", path
, buf
);
86 void save_shadow(const char *label
, const char *ext
)
90 sprintf(buf
, "/tmp/mdw/t.shadow-%s", label
);
91 save_copy(buf
, label
, ext
);
94 void open_shadow(int fd
, const char *label
)
96 struct shadow
*shadow
;
101 assert(!find_shadow(fd
));
102 assert(nshadow
< MAXSHADOW
); shadow
= &shadowtab
[nshadow
++];
103 sprintf(buf
, "/tmp/mdw/t.shadow-%s", label
);
106 shadow
->shadow
= open(buf
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
107 assert(shadow
->shadow
>= 0);
108 rc
= fstat(fd
, &st
); assert(!rc
);
109 shadow
->dev
= st
.st_dev
;
110 shadow
->ino
= st
.st_ino
;
113 void close_shadow(int fd
)
116 struct shadow
*shadow
;
118 shadow
= find_shadow(fd
); if (!shadow
) return;
119 rc
= close(shadow
->shadow
); assert(!rc
);
120 assert(nshadow
); nshadow
--;
121 if (shadow
!= &shadowtab
[nshadow
]) *shadow
= shadowtab
[nshadow
];
125 fd_read(int fd
, void *buf
, size_t len
)
133 n
= read(fd
, ptr
+ total
, len
);
135 if (errno
== EINTR
|| errno
== EAGAIN
)
137 return total ?
-total
: n
;
141 write_to_shadow(fd
, ptr
+ total
, n
);
151 fd_write(int fd
, const void *buf
, size_t len
)
154 const char *ptr
= buf
;
159 n
= write(fd
, ptr
+ total
, len
);
161 if (errno
== EINTR
|| errno
== EAGAIN
)
163 return total ?
-total
: n
;
167 write_to_shadow(fd
, buf
, n
);
176 #ifdef USE_DISK_PREALLOCATE
177 #ifdef HAVE_F_PREALLOCATE
179 fd_preallocate_setup(fstore_t
*fs
, int flags
, off_t offset
, off_t len
)
181 fs
->fst_flags
= flags
;
182 fs
->fst_posmode
= F_PEOFPOSMODE
;
183 fs
->fst_offset
= offset
;
184 fs
->fst_length
= len
;
185 fs
->fst_bytesalloc
= 0;
190 * Request the kernel to allocate the specified size for a file descriptor.
192 * We only want to send a hint that we will be using the requested size. But
193 * we do not want to unnecessarily write the file contents. That is why we
194 * are not using posix_fallocate(3) directly if possible, and not at all
195 * on glibc based systems (except on GNU/kFreeBSD).
198 fd_allocate_size(int fd
, off_t offset
, off_t len
)
202 /* Do not preallocate on very small files as that degrades performance
203 * on some filesystems. */
204 if (len
< (4 * 4096) - 1)
207 #if defined(HAVE_F_PREALLOCATE)
211 fd_preallocate_setup(&fs
, F_ALLOCATECONTIG
, offset
, len
);
212 rc
= fcntl(fd
, F_PREALLOCATE
, &fs
);
213 if (rc
< 0 && errno
== ENOSPC
) {
214 /* If we cannot get a contiguous allocation, then try
216 fd_preallocate_setup(&fs
, F_ALLOCATEALL
, offset
, len
);
217 rc
= fcntl(fd
, F_PREALLOCATE
, &fs
);
219 #elif defined(HAVE_F_ALLOCSP64)
223 fl
.l_whence
= SEEK_SET
;
227 rc
= fcntl(fd
, F_ALLOCSP64
, &fl
);
228 #elif defined(HAVE_FALLOCATE)
231 rc
= fallocate(fd
, 0, offset
, len
);
232 } while (rc
< 0 && errno
== EINTR
);
233 #elif defined(HAVE_POSIX_FALLOCATE) && \
234 ((defined(__GLIBC__) && defined(__FreeBSD_kernel__)) || \
237 * On BSDs, newer GNU/kFreeBSD and other non-glibc based systems
238 * we can use posix_fallocate(2) which should be a simple syscall
239 * wrapper. But not on other glibc systems, as there the function
240 * will try to allocate the size by writing a '\0' to each block
241 * if the syscall is not implemented or not supported by the
242 * kernel or the filesystem, which we do not want.
244 rc
= posix_fallocate(fd
, offset
, len
);
254 fd_allocate_size(int fd
, off_t offset
, off_t len
)