]> wimlib.net Git - wimlib/blob - src/util.c
imagex-extract initial implementation
[wimlib] / src / util.c
1 /*
2  * util.c
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #include "config.h"
25
26
27 #undef _GNU_SOURCE
28 /* Make sure the POSIX-compatible strerror_r() is declared, rather than the GNU
29  * version, which has a different return type. */
30 #include <string.h>
31
32 #define _GNU_SOURCE
33
34 #include "endianness.h"
35 #include "timestamp.h"
36 #include "wimlib_internal.h"
37
38 #include <ctype.h>
39 #include <errno.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44
45 #ifdef __WIN32__
46 #  include "win32.h"
47 #  define pread  win32_pread
48 #  define pwrite win32_pwrite
49 #  define writev win32_writev
50 #else
51 #  include <sys/uio.h> /* for writev() and `struct iovec' */
52 #endif
53
54 static size_t
55 utf16le_strlen(const utf16lechar *s)
56 {
57         const utf16lechar *p = s;
58         while (*p)
59                 p++;
60         return (p - s) * sizeof(utf16lechar);
61 }
62
63 #ifdef __WIN32__
64 #  define wimlib_vfprintf vfwprintf
65 #else
66 /* Handle %W for UTF16-LE printing.
67  *
68  * TODO: this is not yet done properly--- it's assumed that if the format string
69  * contains %W, then it contains no other format specifiers.
70  */
71 static int
72 wimlib_vfprintf(FILE *fp, const tchar *format, va_list va)
73 {
74         const tchar *p;
75         int n;
76
77         for (p = format; *p; p++)
78                 if (*p == T('%') && *(p + 1) == T('W'))
79                         goto special;
80         return tvfprintf(fp, format, va);
81 special:
82         n = 0;
83         for (p = format; *p; p++) {
84                 if (*p == T('%') && (*(p + 1) == T('W'))) {
85                         int ret;
86                         tchar *tstr;
87                         size_t tstr_nbytes;
88                         utf16lechar *ucs = va_arg(va, utf16lechar*);
89
90                         if (ucs) {
91                                 size_t ucs_nbytes = utf16le_strlen(ucs);
92
93                                 ret = utf16le_to_tstr(ucs, ucs_nbytes,
94                                                       &tstr, &tstr_nbytes);
95                                 if (ret) {
96                                         ret = tfprintf(fp, T("??????"));
97                                 } else {
98                                         ret = tfprintf(fp, T("%"TS), tstr);
99                                         FREE(tstr);
100                                 }
101                                 if (ret < 0)
102                                         return -1;
103                                 else
104                                         n += ret;
105                         } else {
106                                 n += tfprintf(fp, T("(null)"));
107                         }
108                         p++;
109                 } else {
110                         if (tputc(*p, fp) == EOF)
111                                 return -1;
112                         n++;
113                 }
114         }
115         return n;
116 }
117
118 int
119 wimlib_printf(const tchar *format, ...)
120 {
121         int ret;
122         va_list va;
123
124         va_start(va, format);
125         ret = wimlib_vfprintf(stdout, format, va);
126         va_end(va);
127         return ret;
128 }
129
130 int
131 wimlib_fprintf(FILE *fp, const tchar *format, ...)
132 {
133         int ret;
134         va_list va;
135
136         va_start(va, format);
137         ret = wimlib_vfprintf(fp, format, va);
138         va_end(va);
139         return ret;
140 }
141 #endif
142
143 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
144 static void
145 wimlib_vmsg(const tchar *tag, const tchar *format,
146             va_list va, bool perror)
147 {
148 #ifndef DEBUG
149         if (wimlib_print_errors) {
150 #endif
151                 int errno_save = errno;
152                 fflush(stdout);
153                 tfputs(tag, stderr);
154                 wimlib_vfprintf(stderr, format, va);
155                 if (perror && errno_save != 0) {
156                         tchar buf[50];
157                         int res;
158                         res = tstrerror_r(errno_save, buf, sizeof(buf));
159                         if (res) {
160                                 tsprintf(buf,
161                                          T("unknown error (errno=%d)"),
162                                          errno_save);
163                         }
164                         tfprintf(stderr, T(": %"TS), buf);
165                 }
166                 tputc(T('\n'), stderr);
167                 fflush(stderr);
168                 errno = errno_save;
169 #ifndef DEBUG
170         }
171 #endif
172 }
173 #endif
174
175 /* True if wimlib is to print an informational message when an error occurs.
176  * This can be turned off by calling wimlib_set_print_errors(false). */
177 #ifdef ENABLE_ERROR_MESSAGES
178 static bool wimlib_print_errors = false;
179
180
181 void
182 wimlib_error(const tchar *format, ...)
183 {
184         va_list va;
185
186         va_start(va, format);
187         wimlib_vmsg(T("\r[ERROR] "), format, va, false);
188         va_end(va);
189 }
190
191 void
192 wimlib_error_with_errno(const tchar *format, ...)
193 {
194         va_list va;
195
196         va_start(va, format);
197         wimlib_vmsg(T("\r[ERROR] "), format, va, true);
198         va_end(va);
199 }
200
201 void
202 wimlib_warning(const tchar *format, ...)
203 {
204         va_list va;
205
206         va_start(va, format);
207         wimlib_vmsg(T("\r[WARNING] "), format, va, false);
208         va_end(va);
209 }
210
211 void
212 wimlib_warning_with_errno(const tchar *format, ...)
213 {
214         va_list va;
215
216         va_start(va, format);
217         wimlib_vmsg(T("\r[WARNING] "), format, va, true);
218         va_end(va);
219 }
220
221 #endif
222
223 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
224 void wimlib_debug(const tchar *file, int line, const char *func,
225                   const tchar *format, ...)
226 {
227         va_list va;
228         tchar buf[tstrlen(file) + strlen(func) + 30];
229
230         tsprintf(buf, T("[%"TS" %d] %s(): "), file, line, func);
231
232         va_start(va, format);
233         wimlib_vmsg(buf, format, va, false);
234         va_end(va);
235 }
236 #endif
237
238 WIMLIBAPI int
239 wimlib_set_print_errors(bool show_error_messages)
240 {
241 #ifdef ENABLE_ERROR_MESSAGES
242         wimlib_print_errors = show_error_messages;
243         return 0;
244 #else
245         if (show_error_messages)
246                 return WIMLIB_ERR_UNSUPPORTED;
247         else
248                 return 0;
249 #endif
250 }
251
252 static const tchar *error_strings[] = {
253         [WIMLIB_ERR_SUCCESS]
254                 = T("Success"),
255         [WIMLIB_ERR_ALREADY_LOCKED]
256                 = T("The WIM is already locked for writing"),
257         [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
258                 = T("Lookup table is compressed"),
259         [WIMLIB_ERR_DECOMPRESSION]
260                 = T("Failed to decompress compressed data"),
261         [WIMLIB_ERR_DELETE_STAGING_DIR]
262                 = T("Failed to delete staging directory"),
263         [WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED]
264                 = T("The process servicing the mounted WIM has crashed"),
265         [WIMLIB_ERR_FORK]
266                 = T("Failed to fork another process"),
267         [WIMLIB_ERR_FUSE]
268                 = T("An error was returned by fuse_main()"),
269         [WIMLIB_ERR_FUSERMOUNT]
270                 = T("Could not execute the `fusermount' program, or it exited "
271                         "with a failure status"),
272         [WIMLIB_ERR_ICONV_NOT_AVAILABLE]
273                 = T("The iconv() function does not seem to work. "
274                   "Maybe check to make sure the directory /usr/lib/gconv exists"),
275         [WIMLIB_ERR_IMAGE_COUNT]
276                 = T("Inconsistent image count among the metadata "
277                         "resources, the WIM header, and/or the XML data"),
278         [WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT]
279                 = T("User does not have sufficient privileges to correctly extract the data"),
280         [WIMLIB_ERR_IMAGE_NAME_COLLISION]
281                 = T("Tried to add an image with a name that is already in use"),
282         [WIMLIB_ERR_INTEGRITY]
283                 = T("The WIM failed an integrity check"),
284         [WIMLIB_ERR_INVALID_CAPTURE_CONFIG]
285                 = T("The capture configuration string was invalid"),
286         [WIMLIB_ERR_INVALID_CHUNK_SIZE]
287                 = T("The WIM is compressed but does not have a chunk "
288                         "size of 32768"),
289         [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
290                 = T("The WIM is compressed, but is not marked as having LZX or "
291                         "XPRESS compression"),
292         [WIMLIB_ERR_INVALID_DENTRY]
293                 = T("A directory entry in the WIM was invalid"),
294         [WIMLIB_ERR_INVALID_HEADER_SIZE]
295                 = T("The WIM header was not 208 bytes"),
296         [WIMLIB_ERR_INVALID_IMAGE]
297                 = T("Tried to select an image that does not exist in the WIM"),
298         [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
299                 = T("The WIM's integrity table is invalid"),
300         [WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY]
301                 = T("An entry in the WIM's lookup table is invalid"),
302         [WIMLIB_ERR_INVALID_MULTIBYTE_STRING]
303                 = T("A string was not valid in the current locale's character encoding"),
304         [WIMLIB_ERR_INVALID_OVERLAY]
305                 = T("Conflicting files in overlay when creating a WIM image"),
306         [WIMLIB_ERR_INVALID_PARAM]
307                 = T("An invalid parameter was given"),
308         [WIMLIB_ERR_INVALID_PART_NUMBER]
309                 = T("The part number or total parts of the WIM is invalid"),
310         [WIMLIB_ERR_INVALID_REPARSE_DATA]
311                 = T("The reparse data of a reparse point was invalid"),
312         [WIMLIB_ERR_INVALID_RESOURCE_HASH]
313                 = T("The SHA1 message digest of a WIM resource did not match the expected value"),
314         [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
315                 = T("A resource entry in the WIM has an invalid size"),
316         [WIMLIB_ERR_INVALID_SECURITY_DATA]
317                 = T("The table of security descriptors in the WIM is invalid"),
318         [WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE]
319                 = T("The version of wimlib that has mounted a WIM image is incompatible with the "
320                   "version being used to unmount it"),
321         [WIMLIB_ERR_INVALID_UTF8_STRING]
322                 = T("A string provided as input by the user was not a valid UTF-8 string"),
323         [WIMLIB_ERR_INVALID_UTF16_STRING]
324                 = T("A string in a WIM dentry is not a valid UTF-16LE string"),
325         [WIMLIB_ERR_LIBXML_UTF16_HANDLER_NOT_AVAILABLE]
326                 = T("libxml2 was unable to find a character encoding conversion handler "
327                   "for UTF-16LE"),
328         [WIMLIB_ERR_LINK]
329                 = T("Failed to create a hard or symbolic link when extracting "
330                         "a file from the WIM"),
331         [WIMLIB_ERR_MKDIR]
332                 = T("Failed to create a directory"),
333         [WIMLIB_ERR_MQUEUE]
334                 = T("Failed to create or use a POSIX message queue"),
335         [WIMLIB_ERR_NOMEM]
336                 = T("Ran out of memory"),
337         [WIMLIB_ERR_NOTDIR]
338                 = T("Expected a directory"),
339         [WIMLIB_ERR_NOT_A_WIM_FILE]
340                 = T("The file did not begin with the magic characters that "
341                         "identify a WIM file"),
342         [WIMLIB_ERR_NO_FILENAME]
343                 = T("The WIM is not identified with a filename"),
344         [WIMLIB_ERR_NTFS_3G]
345                 = T("NTFS-3g encountered an error (check errno)"),
346         [WIMLIB_ERR_OPEN]
347                 = T("Failed to open a file"),
348         [WIMLIB_ERR_OPENDIR]
349                 = T("Failed to open a directory"),
350         [WIMLIB_ERR_PATH_DOES_NOT_EXIST]
351                 = T("The path does not exist in the WIM image"),
352         [WIMLIB_ERR_READ]
353                 = T("Could not read data from a file"),
354         [WIMLIB_ERR_READLINK]
355                 = T("Could not read the target of a symbolic link"),
356         [WIMLIB_ERR_RENAME]
357                 = T("Could not rename a file"),
358         [WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED]
359                 = T("Unable to complete reparse point fixup"),
360         [WIMLIB_ERR_RESOURCE_ORDER]
361                 = T("The components of the WIM were arranged in an unexpected order"),
362         [WIMLIB_ERR_SPECIAL_FILE]
363                 = T("Encountered a special file that cannot be archived"),
364         [WIMLIB_ERR_SPLIT_INVALID]
365                 = T("The WIM is part of an invalid split WIM"),
366         [WIMLIB_ERR_SPLIT_UNSUPPORTED]
367                 = T("The WIM is part of a split WIM, which is not supported for this operation"),
368         [WIMLIB_ERR_STAT]
369                 = T("Could not read the metadata for a file or directory"),
370         [WIMLIB_ERR_TIMEOUT]
371                 = T("Timed out while waiting for a message to arrive from another process"),
372         [WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE]
373                 = T("A Unicode string could not be represented in the current locale's encoding"),
374         [WIMLIB_ERR_UNKNOWN_VERSION]
375                 = T("The WIM file is marked with an unknown version number"),
376         [WIMLIB_ERR_UNSUPPORTED]
377                 = T("The requested operation is unsupported"),
378         [WIMLIB_ERR_VOLUME_LACKS_FEATURES]
379                 = T("The volume did not support a feature necessary to complete the operation"),
380         [WIMLIB_ERR_WRITE]
381                 = T("Failed to write data to a file"),
382         [WIMLIB_ERR_XML]
383                 = T("The XML data of the WIM is invalid"),
384 };
385
386 WIMLIBAPI const tchar *
387 wimlib_get_error_string(enum wimlib_error_code code)
388 {
389         if (code < 0 || code >= ARRAY_LEN(error_strings))
390                 return NULL;
391         else
392                 return error_strings[code];
393 }
394
395
396
397 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
398 void *(*wimlib_malloc_func) (size_t)         = malloc;
399 void  (*wimlib_free_func)   (void *)         = free;
400 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
401
402 void *
403 wimlib_calloc(size_t nmemb, size_t size)
404 {
405         size_t total_size = nmemb * size;
406         void *p = MALLOC(total_size);
407         if (p)
408                 memset(p, 0, total_size);
409         return p;
410 }
411
412 char *
413 wimlib_strdup(const char *str)
414 {
415         size_t size;
416         char *p;
417
418         size = strlen(str);
419         p = MALLOC(size + 1);
420         if (p)
421                 memcpy(p, str, size + 1);
422         return p;
423 }
424
425 #ifdef __WIN32__
426 wchar_t *
427 wimlib_wcsdup(const wchar_t *str)
428 {
429         size_t size;
430         wchar_t *p;
431
432         size = wcslen(str);
433         p = MALLOC((size + 1) * sizeof(wchar_t));
434         if (p)
435                 memcpy(p, str, (size + 1) * sizeof(wchar_t));
436         return p;
437 }
438 #endif
439
440 extern void
441 xml_set_memory_allocator(void *(*malloc_func)(size_t),
442                          void (*free_func)(void *),
443                          void *(*realloc_func)(void *, size_t));
444 #endif
445
446 WIMLIBAPI int
447 wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
448                             void (*free_func)(void *),
449                             void *(*realloc_func)(void *, size_t))
450 {
451 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
452         wimlib_malloc_func  = malloc_func  ? malloc_func  : malloc;
453         wimlib_free_func    = free_func    ? free_func    : free;
454         wimlib_realloc_func = realloc_func ? realloc_func : realloc;
455
456         xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
457                                  wimlib_realloc_func);
458         return 0;
459 #else
460         ERROR("Cannot set custom memory allocator functions:");
461         ERROR("wimlib was compiled with the --without-custom-memory-allocator "
462               "flag");
463         return WIMLIB_ERR_UNSUPPORTED;
464 #endif
465 }
466
467 static bool seeded = false;
468
469 static void
470 seed_random()
471 {
472         srand(time(NULL) * getpid());
473         seeded = true;
474 }
475
476 /* Fills @n characters pointed to by @p with random alphanumeric characters. */
477 void
478 randomize_char_array_with_alnum(tchar p[], size_t n)
479 {
480         if (!seeded)
481                 seed_random();
482         while (n--) {
483                 int r = rand() % 62;
484                 if (r < 26)
485                         *p++ = r + 'a';
486                 else if (r < 52)
487                         *p++ = r - 26 + 'A';
488                 else
489                         *p++ = r - 52 + '0';
490         }
491 }
492
493 /* Fills @n bytes pointer to by @p with random numbers. */
494 void
495 randomize_byte_array(u8 *p, size_t n)
496 {
497         if (!seeded)
498                 seed_random();
499         while (n--)
500                 *p++ = rand();
501 }
502
503 const tchar *
504 path_basename_with_len(const tchar *path, size_t len)
505 {
506         const tchar *p = &path[len] - 1;
507
508         /* Trailing slashes. */
509         while (1) {
510                 if (p == path - 1)
511                         return T("");
512                 if (*p != T('/'))
513                         break;
514                 p--;
515         }
516
517         while ((p != path - 1) && *p != T('/'))
518                 p--;
519
520         return p + 1;
521 }
522
523 /* Like the basename() function, but does not modify @path; it just returns a
524  * pointer to it. */
525 const tchar *
526 path_basename(const tchar *path)
527 {
528         return path_basename_with_len(path, tstrlen(path));
529 }
530
531 /*
532  * Returns a pointer to the part of @path following the first colon in the last
533  * path component, or NULL if the last path component does not contain a colon.
534  */
535 const tchar *
536 path_stream_name(const tchar *path)
537 {
538         const tchar *base = path_basename(path);
539         const tchar *stream_name = tstrchr(base, T(':'));
540         if (!stream_name)
541                 return NULL;
542         else
543                 return stream_name + 1;
544 }
545
546 u64
547 get_wim_timestamp()
548 {
549         struct timeval tv;
550         gettimeofday(&tv, NULL);
551         return timeval_to_wim_timestamp(tv);
552 }
553
554 void
555 wim_timestamp_to_str(u64 timestamp, tchar *buf, size_t len)
556 {
557         struct tm tm;
558         time_t t = wim_timestamp_to_unix(timestamp);
559         gmtime_r(&t, &tm);
560         tstrftime(buf, len, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
561 }
562
563 void
564 zap_backslashes(tchar *s)
565 {
566         if (s) {
567                 while (*s != T('\0')) {
568                         if (*s == T('\\'))
569                                 *s = T('/');
570                         s++;
571                 }
572         }
573 }
574
575 tchar *
576 canonicalize_fs_path(const tchar *fs_path)
577 {
578         tchar *canonical_path;
579
580         if (!fs_path)
581                 fs_path = T("");
582         canonical_path = TSTRDUP(fs_path);
583         zap_backslashes(canonical_path);
584         return canonical_path;
585 }
586
587 /* Strip leading and trailing slashes from a string.  Also translates
588  * backslashes into forward slashes.  */
589 tchar *
590 canonicalize_wim_path(const tchar *wim_path)
591 {
592         tchar *p;
593         tchar *canonical_path;
594
595         if (wim_path == NULL) {
596                 wim_path = T("");
597         } else {
598                 while (*wim_path == T('/') || *wim_path == T('\\'))
599                         wim_path++;
600         }
601         canonical_path = TSTRDUP(wim_path);
602         if (canonical_path) {
603                 zap_backslashes(canonical_path);
604                 for (p = tstrchr(canonical_path, T('\0')) - 1;
605                      p >= canonical_path && *p == T('/');
606                      p--)
607                 {
608                         *p = T('\0');
609                 }
610         }
611         return canonical_path;
612 }
613
614 /* Like read(), but keep trying until everything has been written or we know for
615  * sure that there was an error (or end-of-file). */
616 size_t
617 full_read(int fd, void *buf, size_t count)
618 {
619         ssize_t bytes_read;
620         size_t bytes_remaining;
621
622         for (bytes_remaining = count;
623              bytes_remaining != 0;
624              bytes_remaining -= bytes_read, buf += bytes_read)
625         {
626                 bytes_read = read(fd, buf, bytes_remaining);
627                 if (bytes_read <= 0) {
628                         if (bytes_read == 0)
629                                 errno = EIO;
630                         else if (errno == EINTR)
631                                 continue;
632                         break;
633                 }
634         }
635         return count - bytes_remaining;
636 }
637
638 /* Like write(), but keep trying until everything has been written or we know
639  * for sure that there was an error. */
640 size_t
641 full_write(int fd, const void *buf, size_t count)
642 {
643         ssize_t bytes_written;
644         size_t bytes_remaining;
645
646         for (bytes_remaining = count;
647              bytes_remaining != 0;
648              bytes_remaining -= bytes_written, buf += bytes_written)
649         {
650                 bytes_written = write(fd, buf, bytes_remaining);
651                 if (bytes_written < 0) {
652                         if (errno == EINTR)
653                                 continue;
654                         break;
655                 }
656         }
657         return count - bytes_remaining;
658 }
659
660 /* Like pread(), but keep trying until everything has been read or we know for
661  * sure that there was an error (or end-of-file) */
662 size_t
663 full_pread(int fd, void *buf, size_t count, off_t offset)
664 {
665         ssize_t bytes_read;
666         size_t bytes_remaining;
667
668         for (bytes_remaining = count;
669              bytes_remaining != 0;
670              bytes_remaining -= bytes_read, buf += bytes_read,
671                 offset += bytes_read)
672         {
673                 bytes_read = pread(fd, buf, bytes_remaining, offset);
674                 if (bytes_read <= 0) {
675                         if (bytes_read == 0)
676                                 errno = EIO;
677                         else if (errno == EINTR)
678                                 continue;
679                         break;
680                 }
681         }
682         return count - bytes_remaining;
683 }
684
685 /* Like pwrite(), but keep trying until everything has been written or we know
686  * for sure that there was an error. */
687 size_t
688 full_pwrite(int fd, const void *buf, size_t count, off_t offset)
689 {
690         ssize_t bytes_written;
691         size_t bytes_remaining;
692
693         for (bytes_remaining = count;
694              bytes_remaining != 0;
695              bytes_remaining -= bytes_written, buf += bytes_written,
696                 offset += bytes_written)
697         {
698                 bytes_written = pwrite(fd, buf, bytes_remaining, offset);
699                 if (bytes_written < 0) {
700                         if (errno == EINTR)
701                                 continue;
702                         break;
703                 }
704         }
705         return count - bytes_remaining;
706 }
707
708 /* Like writev(), but keep trying until everything has been written or we know
709  * for sure that there was an error. */
710 size_t
711 full_writev(int fd, struct iovec *iov, int iovcnt)
712 {
713         size_t total_bytes_written = 0;
714         while (iovcnt > 0) {
715                 ssize_t bytes_written;
716
717                 bytes_written = writev(fd, iov, iovcnt);
718                 if (bytes_written < 0) {
719                         if (errno == EINTR)
720                                 continue;
721                         break;
722                 }
723                 total_bytes_written += bytes_written;
724                 while (bytes_written) {
725                         if (bytes_written >= iov[0].iov_len) {
726                                 bytes_written -= iov[0].iov_len;
727                                 iov++;
728                                 iovcnt--;
729                         } else {
730                                 iov[0].iov_base += bytes_written;
731                                 iov[0].iov_len -= bytes_written;
732                                 bytes_written = 0;
733                         }
734                 }
735         }
736         return total_bytes_written;
737 }
738
739 off_t
740 filedes_offset(int fd)
741 {
742         return lseek(fd, 0, SEEK_CUR);
743 }