2 * File functions for CUPS.
4 * Since stdio files max out at 256 files on many systems, we have to
5 * write similar functions without this limit. At the same time, using
6 * our own file functions allows us to provide transparent support of
7 * different line endings, gzip'd print files, PPD files, etc.
9 * Copyright 2007-2017 by Apple Inc.
10 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * missing or damaged, see the license at "http://www.cups.org/".
18 * This file is subject to the Apple OS-Developed Software exception.
22 * Include necessary headers...
25 #include "file-private.h"
27 #include <sys/types.h>
35 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
36 #endif /* HAVE_LIBZ */
37 static ssize_t
cups_fill(cups_file_t
*fp
);
38 static int cups_open(const char *filename
, int mode
);
39 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
40 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
45 * '_cupsFileCheck()' - Check the permissions of the given filename.
48 _cups_fc_result_t
/* O - Check result */
50 const char *filename
, /* I - Filename to check */
51 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
52 int dorootchecks
, /* I - Check for root permissions? */
53 _cups_fc_func_t cb
, /* I - Callback function */
54 void *context
) /* I - Context pointer for callback */
57 struct stat fileinfo
; /* File information */
58 char message
[1024], /* Message string */
59 temp
[1024], /* Parent directory filename */
60 *ptr
; /* Pointer into parent directory */
61 _cups_fc_result_t result
; /* Check result */
65 * Does the filename contain a relative path ("../")?
68 if (strstr(filename
, "../"))
74 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
79 * Does the program even exist and is it accessible?
82 if (stat(filename
, &fileinfo
))
88 result
= _CUPS_FILE_CHECK_MISSING
;
93 * Check the execute bit...
96 result
= _CUPS_FILE_CHECK_OK
;
100 case _CUPS_FILE_CHECK_DIRECTORY
:
101 if (!S_ISDIR(fileinfo
.st_mode
))
102 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
106 if (!S_ISREG(fileinfo
.st_mode
))
107 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
115 * Are we doing root checks?
121 * Nope, so anything (else) goes...
128 * Verify permission of the file itself:
130 * 1. Must be owned by root
131 * 2. Must not be writable by group
132 * 3. Must not be setuid
133 * 4. Must not be writable by others
136 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
137 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
138 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
139 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
141 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
145 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
146 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
150 * Now check the containing directory...
153 strlcpy(temp
, filename
, sizeof(temp
));
154 if ((ptr
= strrchr(temp
, '/')) != NULL
)
162 if (stat(temp
, &fileinfo
))
168 result
= _CUPS_FILE_CHECK_MISSING
;
169 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
175 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
176 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
177 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
178 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
180 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
181 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
186 * Common return point...
193 cups_lang_t
*lang
= cupsLangDefault();
194 /* Localization information */
198 case _CUPS_FILE_CHECK_OK
:
199 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
200 snprintf(message
, sizeof(message
),
201 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
202 "(0%o/uid=%d/gid=%d).")),
203 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
204 (int)fileinfo
.st_gid
);
206 snprintf(message
, sizeof(message
),
207 _cupsLangString(lang
, _("File \"%s\" permissions OK "
208 "(0%o/uid=%d/gid=%d).")),
209 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
210 (int)fileinfo
.st_gid
);
213 case _CUPS_FILE_CHECK_MISSING
:
214 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
215 snprintf(message
, sizeof(message
),
216 _cupsLangString(lang
, _("Directory \"%s\" not available: "
218 filename
, strerror(errno
));
220 snprintf(message
, sizeof(message
),
221 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
222 filename
, strerror(errno
));
225 case _CUPS_FILE_CHECK_PERMISSIONS
:
226 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
227 snprintf(message
, sizeof(message
),
228 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
230 "(0%o/uid=%d/gid=%d).")),
231 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
232 (int)fileinfo
.st_gid
);
234 snprintf(message
, sizeof(message
),
235 _cupsLangString(lang
, _("File \"%s\" has insecure "
237 "(0%o/uid=%d/gid=%d).")),
238 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
239 (int)fileinfo
.st_gid
);
242 case _CUPS_FILE_CHECK_WRONG_TYPE
:
243 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
244 snprintf(message
, sizeof(message
),
245 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
248 snprintf(message
, sizeof(message
),
249 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
253 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
254 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
255 snprintf(message
, sizeof(message
),
256 _cupsLangString(lang
, _("Directory \"%s\" contains a "
257 "relative path.")), filename
);
259 snprintf(message
, sizeof(message
),
260 _cupsLangString(lang
, _("File \"%s\" contains a relative "
261 "path.")), filename
);
265 (*cb
)(context
, result
, message
);
273 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
277 _cupsFileCheckFilter(
278 void *context
, /* I - Context pointer (unused) */
279 _cups_fc_result_t result
, /* I - Result code */
280 const char *message
) /* I - Message text */
282 const char *prefix
; /* Messaging prefix */
290 case _CUPS_FILE_CHECK_OK
:
294 case _CUPS_FILE_CHECK_MISSING
:
295 case _CUPS_FILE_CHECK_WRONG_TYPE
:
297 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
300 case _CUPS_FILE_CHECK_PERMISSIONS
:
301 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
303 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
307 fprintf(stderr
, "%s: %s\n", prefix
, message
);
313 * 'cupsFileClose()' - Close a CUPS file.
315 * @since CUPS 1.2/macOS 10.5@
318 int /* O - 0 on success, -1 on error */
319 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
321 int fd
; /* File descriptor */
322 char mode
; /* Open mode */
323 int status
; /* Return status */
326 DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp
));
336 * Flush pending write data...
340 status
= cupsFileFlush(fp
);
345 if (fp
->compressed
&& status
>= 0)
350 * Free decompression data...
353 inflateEnd(&fp
->stream
);
358 * Flush any remaining compressed data...
361 unsigned char trailer
[8]; /* Trailer CRC and length */
362 int done
; /* Done writing... */
365 fp
->stream
.avail_in
= 0;
369 if (fp
->stream
.next_out
> fp
->cbuf
)
371 if (cups_write(fp
, (char *)fp
->cbuf
,
372 (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
375 fp
->stream
.next_out
= fp
->cbuf
;
376 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
379 if (done
|| status
< 0)
382 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
383 fp
->stream
.next_out
== fp
->cbuf
;
387 * Write the CRC and length...
390 trailer
[0] = (unsigned char)fp
->crc
;
391 trailer
[1] = (unsigned char)(fp
->crc
>> 8);
392 trailer
[2] = (unsigned char)(fp
->crc
>> 16);
393 trailer
[3] = (unsigned char)(fp
->crc
>> 24);
394 trailer
[4] = (unsigned char)fp
->pos
;
395 trailer
[5] = (unsigned char)(fp
->pos
>> 8);
396 trailer
[6] = (unsigned char)(fp
->pos
>> 16);
397 trailer
[7] = (unsigned char)(fp
->pos
>> 24);
399 if (cups_write(fp
, (char *)trailer
, 8) < 0)
403 * Free all memory used by the compression stream...
406 deflateEnd(&(fp
->stream
));
409 #endif /* HAVE_LIBZ */
412 * If this is one of the cupsFileStdin/out/err files, return now and don't
413 * actually free memory or close (these last the life of the process...)
420 * Save the file descriptor we used and free memory...
426 if (fp
->printf_buffer
)
427 free(fp
->printf_buffer
);
432 * Close the file, returning the close status...
437 if (httpAddrClose(NULL
, fd
) < 0)
440 else if (close(fd
) < 0)
448 * 'cupsFileCompression()' - Return whether a file is compressed.
450 * @since CUPS 1.2/macOS 10.5@
453 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
454 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
456 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
461 * 'cupsFileEOF()' - Return the end-of-file status.
463 * @since CUPS 1.2/macOS 10.5@
466 int /* O - 1 on end of file, 0 otherwise */
467 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
469 return (fp
? fp
->eof
: 1);
474 * 'cupsFileFind()' - Find a file using the specified path.
476 * This function allows the paths in the path string to be separated by
477 * colons (UNIX standard) or semicolons (Windows standard) and stores the
478 * result in the buffer supplied. If the file cannot be found in any of
479 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
480 * matches the current directory.
482 * @since CUPS 1.2/macOS 10.5@
485 const char * /* O - Full path to file or @code NULL@ if not found */
486 cupsFileFind(const char *filename
, /* I - File to find */
487 const char *path
, /* I - Colon/semicolon-separated path */
488 int executable
, /* I - 1 = executable files, 0 = any file/dir */
489 char *buffer
, /* I - Filename buffer */
490 int bufsize
) /* I - Size of filename buffer */
492 char *bufptr
, /* Current position in buffer */
493 *bufend
; /* End of buffer */
497 * Range check input...
500 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, buffer=%p, bufsize=%d)", filename
, path
, executable
, (void *)buffer
, bufsize
));
502 if (!filename
|| !buffer
|| bufsize
< 2)
508 * No path, so check current directory...
511 if (!access(filename
, 0))
513 strlcpy(buffer
, filename
, (size_t)bufsize
);
521 * Now check each path and return the first match...
524 bufend
= buffer
+ bufsize
- 1;
530 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
532 if (*path
== ';' || *path
== ':')
535 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
538 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
541 if (!access(buffer
, 0))
543 if (!access(buffer
, executable
? X_OK
: 0))
546 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
552 else if (bufptr
< bufend
)
559 * Check the last path...
562 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
565 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
567 if (!access(buffer
, 0))
569 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
574 DEBUG_puts("1cupsFileFind: Returning NULL");
581 * 'cupsFileFlush()' - Flush pending output.
583 * @since CUPS 1.2/macOS 10.5@
586 int /* O - 0 on success, -1 on error */
587 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
589 ssize_t bytes
; /* Bytes to write */
592 DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp
));
595 * Range check input...
598 if (!fp
|| fp
->mode
!= 'w')
600 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
604 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
606 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
613 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
615 #endif /* HAVE_LIBZ */
616 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
629 * 'cupsFileGetChar()' - Get a single character from a file.
631 * @since CUPS 1.2/macOS 10.5@
634 int /* O - Character or -1 on end of file */
635 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
638 * Range check input...
641 DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp
));
643 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
645 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
650 * If the input buffer is empty, try to read more data...
653 DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp
->eof
, (void *)fp
->ptr
, (void *)fp
->end
));
655 if (fp
->ptr
>= fp
->end
)
656 if (cups_fill(fp
) <= 0)
658 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
663 * Return the next character in the buffer...
666 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
670 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
672 return (*(fp
->ptr
)++ & 255);
677 * 'cupsFileGetConf()' - Get a line from a configuration file.
679 * @since CUPS 1.2/macOS 10.5@
682 char * /* O - Line read or @code NULL@ on end of file or error */
683 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
684 char *buf
, /* O - String buffer */
685 size_t buflen
, /* I - Size of string buffer */
686 char **value
, /* O - Pointer to value */
687 int *linenum
) /* IO - Current line number */
689 char *ptr
; /* Pointer into line */
693 * Range check input...
696 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
697 ", value=%p, linenum=%p)", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
, (void *)value
, (void *)linenum
));
699 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
700 !buf
|| buflen
< 2 || !value
)
709 * Read the next non-comment line...
714 while (cupsFileGets(fp
, buf
, buflen
))
719 * Strip any comments...
722 if ((ptr
= strchr(buf
, '#')) != NULL
)
724 if (ptr
> buf
&& ptr
[-1] == '\\')
727 _cups_strcpy(ptr
- 1, ptr
);
731 // Strip the comment and any trailing whitespace...
734 if (!_cups_isspace(ptr
[-1]))
745 * Strip leading whitespace...
748 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
751 _cups_strcpy(buf
, ptr
);
754 * See if there is anything left...
760 * Yes, grab any value and return...
763 for (ptr
= buf
; *ptr
; ptr
++)
764 if (_cups_isspace(*ptr
))
770 * Have a value, skip any other spaces...
773 while (_cups_isspace(*ptr
))
780 * Strip trailing whitespace and > for lines that begin with <...
783 ptr
+= strlen(ptr
) - 1;
785 if (buf
[0] == '<' && *ptr
== '>')
787 else if (buf
[0] == '<' && *ptr
!= '>')
797 while (ptr
> *value
&& _cups_isspace(*ptr
))
814 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
815 * contain binary data.
817 * This function differs from @link cupsFileGets@ in that the trailing CR
818 * and LF are preserved, as is any binary data on the line. The buffer is
819 * nul-terminated, however you should use the returned length to determine
820 * the number of bytes on the line.
822 * @since CUPS 1.2/macOS 10.5@
825 size_t /* O - Number of bytes on line or 0 on end of file */
826 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
827 char *buf
, /* I - Buffer */
828 size_t buflen
) /* I - Size of buffer */
830 int ch
; /* Character from file */
831 char *ptr
, /* Current position in line buffer */
832 *end
; /* End of line buffer */
836 * Range check input...
839 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
841 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
845 * Now loop until we have a valid line...
848 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
850 if (fp
->ptr
>= fp
->end
)
851 if (cups_fill(fp
) <= 0)
854 *ptr
++ = ch
= *(fp
->ptr
)++;
863 if (fp
->ptr
>= fp
->end
)
864 if (cups_fill(fp
) <= 0)
867 if (*(fp
->ptr
) == '\n')
869 *ptr
++ = *(fp
->ptr
)++;
878 * Line feed ends a line...
887 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
889 return ((size_t)(ptr
- buf
));
894 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
896 * @since CUPS 1.2/macOS 10.5@
899 char * /* O - Line read or @code NULL@ on end of file or error */
900 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
901 char *buf
, /* O - String buffer */
902 size_t buflen
) /* I - Size of string buffer */
904 int ch
; /* Character from file */
905 char *ptr
, /* Current position in line buffer */
906 *end
; /* End of line buffer */
910 * Range check input...
913 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
915 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
919 * Now loop until we have a valid line...
922 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
924 if (fp
->ptr
>= fp
->end
)
925 if (cups_fill(fp
) <= 0)
942 if (fp
->ptr
>= fp
->end
)
943 if (cups_fill(fp
) <= 0)
946 if (*(fp
->ptr
) == '\n')
957 * Line feed ends a line...
968 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
975 * 'cupsFileLock()' - Temporarily lock access to a file.
977 * @since CUPS 1.2/macOS 10.5@
980 int /* O - 0 on success, -1 on error */
981 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
982 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
988 if (!fp
|| fp
->mode
== 's')
996 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
998 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1004 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1006 * @since CUPS 1.2/macOS 10.5@
1009 int /* O - File descriptor */
1010 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1020 * 'cupsFileOpen()' - Open a CUPS file.
1022 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1023 * existing file, "a" to append to an existing file or create a new file,
1024 * or "s" to open a socket connection.
1026 * When opening for writing ("w"), an optional number from 1 to 9 can be
1027 * supplied which enables Flate compression of the file. Compression is
1028 * not supported for the "a" (append) mode.
1030 * When opening a socket connection, the filename is a string of the form
1031 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1032 * connection as needed, generally preferring IPv6 connections when there is
1035 * @since CUPS 1.2/macOS 10.5@
1038 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1039 cupsFileOpen(const char *filename
, /* I - Name of file */
1040 const char *mode
) /* I - Open mode */
1042 cups_file_t
*fp
; /* New CUPS file */
1043 int fd
; /* File descriptor */
1044 char hostname
[1024], /* Hostname */
1045 *portname
; /* Port "name" (number or service) */
1046 http_addrlist_t
*addrlist
; /* Host address list */
1049 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1053 * Range check input...
1056 if (!filename
|| !mode
||
1057 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1058 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1067 case 'a' : /* Append file */
1068 fd
= cups_open(filename
,
1069 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1072 case 'r' : /* Read file */
1073 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1076 case 'w' : /* Write file */
1077 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1078 if (fd
< 0 && errno
== ENOENT
)
1080 fd
= cups_open(filename
,
1081 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1082 if (fd
< 0 && errno
== EEXIST
)
1083 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1094 case 's' : /* Read/write socket */
1095 strlcpy(hostname
, filename
, sizeof(hostname
));
1096 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1102 * Lookup the hostname and service...
1105 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1109 * Connect to the server...
1112 if (!httpAddrConnect(addrlist
, &fd
))
1114 httpAddrFreeList(addrlist
);
1118 httpAddrFreeList(addrlist
);
1121 default : /* Remove bogus compiler warning... */
1129 * Create the CUPS file structure...
1132 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1135 httpAddrClose(NULL
, fd
);
1148 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1150 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1151 * or "s" to treat the file descriptor as a bidirectional socket connection.
1153 * When opening for writing ("w"), an optional number from 1 to 9 can be
1154 * supplied which enables Flate compression of the file. Compression is
1155 * not supported for the "a" (append) mode.
1157 * @since CUPS 1.2/macOS 10.5@
1160 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1161 cupsFileOpenFd(int fd
, /* I - File descriptor */
1162 const char *mode
) /* I - Open mode */
1164 cups_file_t
*fp
; /* New CUPS file */
1167 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1170 * Range check input...
1173 if (fd
< 0 || !mode
||
1174 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1175 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1179 * Allocate memory...
1182 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1194 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1199 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1202 if (mode
[1] >= '1' && mode
[1] <= '9')
1205 * Open a compressed stream, so write the standard gzip file
1209 unsigned char header
[10]; /* gzip file header */
1210 time_t curtime
; /* Current time */
1213 curtime
= time(NULL
);
1216 header
[2] = Z_DEFLATED
;
1218 header
[4] = (unsigned char)curtime
;
1219 header
[5] = (unsigned char)(curtime
>> 8);
1220 header
[6] = (unsigned char)(curtime
>> 16);
1221 header
[7] = (unsigned char)(curtime
>> 24);
1225 cups_write(fp
, (char *)header
, 10);
1228 * Initialize the compressor...
1231 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1232 Z_DEFAULT_STRATEGY
);
1234 fp
->stream
.next_out
= fp
->cbuf
;
1235 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1237 fp
->crc
= crc32(0L, Z_NULL
, 0);
1239 #endif /* HAVE_LIBZ */
1250 default : /* Remove bogus compiler warning... */
1255 * Don't pass this file to child processes...
1259 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1267 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1269 * @since CUPS 1.2/macOS 10.5@
1272 int /* O - Character or -1 on end of file */
1273 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1276 * Range check input...
1279 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1283 * If the input buffer is empty, try to read more data...
1286 if (fp
->ptr
>= fp
->end
)
1287 if (cups_fill(fp
) <= 0)
1291 * Return the next character in the buffer...
1294 return (*(fp
->ptr
) & 255);
1299 * 'cupsFilePrintf()' - Write a formatted string.
1301 * @since CUPS 1.2/macOS 10.5@
1304 int /* O - Number of bytes written or -1 on error */
1305 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1306 const char *format
, /* I - Printf-style format string */
1307 ...) /* I - Additional args as necessary */
1309 va_list ap
; /* Argument list */
1310 ssize_t bytes
; /* Formatted size */
1313 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp
, format
));
1315 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1318 if (!fp
->printf_buffer
)
1321 * Start with an 1k printf buffer...
1324 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1327 fp
->printf_size
= 1024;
1330 va_start(ap
, format
);
1331 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1334 if (bytes
>= (ssize_t
)fp
->printf_size
)
1337 * Expand the printf buffer...
1340 char *temp
; /* Temporary buffer pointer */
1346 if ((temp
= realloc(fp
->printf_buffer
, (size_t)(bytes
+ 1))) == NULL
)
1349 fp
->printf_buffer
= temp
;
1350 fp
->printf_size
= (size_t)(bytes
+ 1);
1352 va_start(ap
, format
);
1353 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1357 if (fp
->mode
== 's')
1359 if (cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
) < 0)
1364 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1366 return ((int)bytes
);
1369 if ((fp
->ptr
+ bytes
) > fp
->end
)
1370 if (cupsFileFlush(fp
))
1375 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1377 if ((size_t)bytes
> sizeof(fp
->buf
))
1381 return ((int)cups_compress(fp
, fp
->printf_buffer
, (size_t)bytes
));
1383 #endif /* HAVE_LIBZ */
1384 return ((int)cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
));
1388 memcpy(fp
->ptr
, fp
->printf_buffer
, (size_t)bytes
);
1391 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1394 return ((int)bytes
);
1400 * 'cupsFilePutChar()' - Write a character.
1402 * @since CUPS 1.2/macOS 10.5@
1405 int /* O - 0 on success, -1 on error */
1406 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1407 int c
) /* I - Character to write */
1410 * Range check input...
1413 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1416 if (fp
->mode
== 's')
1419 * Send character immediately over socket...
1422 char ch
; /* Output character */
1427 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1436 if (fp
->ptr
>= fp
->end
)
1437 if (cupsFileFlush(fp
))
1440 *(fp
->ptr
) ++ = (char)c
;
1445 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1452 * 'cupsFilePutConf()' - Write a configuration line.
1454 * This function handles any comment escaping of the value.
1456 * @since CUPS 1.4/macOS 10.6@
1459 ssize_t
/* O - Number of bytes written or -1 on error */
1460 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1461 const char *directive
, /* I - Directive */
1462 const char *value
) /* I - Value */
1464 ssize_t bytes
, /* Number of bytes written */
1465 temp
; /* Temporary byte count */
1466 const char *ptr
; /* Pointer into value */
1469 if (!fp
|| !directive
|| !*directive
)
1472 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1475 if (cupsFilePutChar(fp
, ' ') < 0)
1479 if (value
&& *value
)
1481 if ((ptr
= strchr(value
, '#')) != NULL
)
1484 * Need to quote the first # in the info string...
1487 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1491 if (cupsFilePutChar(fp
, '\\') < 0)
1495 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1499 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1505 if (cupsFilePutChar(fp
, '\n') < 0)
1513 * 'cupsFilePuts()' - Write a string.
1515 * Like the @code fputs@ function, no newline is appended to the string.
1517 * @since CUPS 1.2/macOS 10.5@
1520 int /* O - Number of bytes written or -1 on error */
1521 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1522 const char *s
) /* I - String to write */
1524 ssize_t bytes
; /* Bytes to write */
1528 * Range check input...
1531 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1535 * Write the string...
1538 bytes
= (ssize_t
)strlen(s
);
1540 if (fp
->mode
== 's')
1542 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1547 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1549 return ((int)bytes
);
1552 if ((fp
->ptr
+ bytes
) > fp
->end
)
1553 if (cupsFileFlush(fp
))
1558 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1560 if ((size_t)bytes
> sizeof(fp
->buf
))
1564 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1566 #endif /* HAVE_LIBZ */
1567 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1571 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1574 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1577 return ((int)bytes
);
1583 * 'cupsFileRead()' - Read from a file.
1585 * @since CUPS 1.2/macOS 10.5@
1588 ssize_t
/* O - Number of bytes read or -1 on error */
1589 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1590 char *buf
, /* O - Buffer */
1591 size_t bytes
) /* I - Number of bytes to read */
1593 size_t total
; /* Total bytes read */
1594 ssize_t count
; /* Bytes read */
1597 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
1600 * Range check input...
1603 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1610 * Loop until all bytes are read...
1616 if (fp
->ptr
>= fp
->end
)
1617 if (cups_fill(fp
) <= 0)
1619 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1620 CUPS_LLFMT
, CUPS_LLCAST total
));
1623 return ((ssize_t
)total
);
1628 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1629 if (count
> (ssize_t
)bytes
)
1630 count
= (ssize_t
)bytes
;
1632 memcpy(buf
, fp
->ptr
,(size_t) count
);
1636 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1639 * Update the counts for the last read...
1642 bytes
-= (size_t)count
;
1643 total
+= (size_t)count
;
1648 * Return the total number of bytes read...
1651 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1653 return ((ssize_t
)total
);
1658 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1661 * @since CUPS 1.2/macOS 10.5@
1664 off_t
/* O - New file position or -1 on error */
1665 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1668 * Range check input...
1671 DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp
));
1672 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1674 if (!fp
|| fp
->mode
!= 'r')
1678 * Handle special cases...
1681 if (fp
->bufpos
== 0)
1684 * No seeking necessary...
1695 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1701 * Otherwise, seek in the file and cleanup any compression buffers...
1707 inflateEnd(&fp
->stream
);
1710 #endif /* HAVE_LIBZ */
1712 if (lseek(fp
->fd
, 0, SEEK_SET
))
1714 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1724 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1731 * 'cupsFileSeek()' - Seek in a file.
1733 * @since CUPS 1.2/macOS 10.5@
1736 off_t
/* O - New file position or -1 on error */
1737 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1738 off_t pos
) /* I - Position in file */
1740 ssize_t bytes
; /* Number bytes in buffer */
1743 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", (void *)fp
, CUPS_LLCAST pos
));
1744 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1745 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", (void *)fp
->ptr
, (void *)fp
->end
));
1748 * Range check input...
1751 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1755 * Handle special cases...
1759 return (cupsFileRewind(fp
));
1763 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1765 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1767 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1770 * No seeking necessary...
1774 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1782 if (!fp
->compressed
&& !fp
->ptr
)
1785 * Preload a buffer to determine whether the file is compressed...
1788 if (cups_fill(fp
) <= 0)
1791 #endif /* HAVE_LIBZ */
1794 * Seek forwards or backwards...
1799 if (pos
< fp
->bufpos
)
1802 * Need to seek backwards...
1805 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1810 inflateEnd(&fp
->stream
);
1812 lseek(fp
->fd
, 0, SEEK_SET
);
1818 while ((bytes
= cups_fill(fp
)) > 0)
1819 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1825 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1829 #endif /* HAVE_LIBZ */
1831 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1832 fp
->pos
= fp
->bufpos
;
1836 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1837 CUPS_LLCAST fp
->pos
));
1843 * Need to seek forwards...
1846 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1851 while ((bytes
= cups_fill(fp
)) > 0)
1853 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1860 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1864 #endif /* HAVE_LIBZ */
1866 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1867 fp
->pos
= fp
->bufpos
;
1871 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1872 CUPS_LLCAST fp
->pos
));
1876 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1883 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1885 * @since CUPS 1.2/macOS 10.5@
1888 cups_file_t
* /* O - CUPS file */
1889 cupsFileStderr(void)
1891 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1895 * Open file descriptor 2 as needed...
1898 if (!cg
->stdio_files
[2])
1901 * Flush any pending output on the stdio file...
1907 * Open file descriptor 2...
1910 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1911 cg
->stdio_files
[2]->is_stdio
= 1;
1914 return (cg
->stdio_files
[2]);
1919 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1921 * @since CUPS 1.2/macOS 10.5@
1924 cups_file_t
* /* O - CUPS file */
1927 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1931 * Open file descriptor 0 as needed...
1934 if (!cg
->stdio_files
[0])
1937 * Open file descriptor 0...
1940 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1941 cg
->stdio_files
[0]->is_stdio
= 1;
1944 return (cg
->stdio_files
[0]);
1949 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1951 * @since CUPS 1.2/macOS 10.5@
1954 cups_file_t
* /* O - CUPS file */
1955 cupsFileStdout(void)
1957 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1961 * Open file descriptor 1 as needed...
1964 if (!cg
->stdio_files
[1])
1967 * Flush any pending output on the stdio file...
1973 * Open file descriptor 1...
1976 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1977 cg
->stdio_files
[1]->is_stdio
= 1;
1980 return (cg
->stdio_files
[1]);
1985 * 'cupsFileTell()' - Return the current file position.
1987 * @since CUPS 1.2/macOS 10.5@
1990 off_t
/* O - File position */
1991 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1993 DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp
));
1994 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
, CUPS_LLCAST (fp
? fp
->pos
: -1)));
1996 return (fp
? fp
->pos
: 0);
2001 * 'cupsFileUnlock()' - Unlock access to a file.
2003 * @since CUPS 1.2/macOS 10.5@
2006 int /* O - 0 on success, -1 on error */
2007 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2013 DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp
));
2015 if (!fp
|| fp
->mode
== 's')
2023 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2025 return (lockf(fp
->fd
, F_ULOCK
, 0));
2031 * 'cupsFileWrite()' - Write to a file.
2033 * @since CUPS 1.2/macOS 10.5@
2036 ssize_t
/* O - Number of bytes written or -1 on error */
2037 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2038 const char *buf
, /* I - Buffer */
2039 size_t bytes
) /* I - Number of bytes to write */
2042 * Range check input...
2045 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2047 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2054 * Write the buffer...
2057 if (fp
->mode
== 's')
2059 if (cups_write(fp
, buf
, bytes
) < 0)
2062 fp
->pos
+= (off_t
)bytes
;
2064 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2066 return ((ssize_t
)bytes
);
2069 if ((fp
->ptr
+ bytes
) > fp
->end
)
2070 if (cupsFileFlush(fp
))
2073 fp
->pos
+= (off_t
)bytes
;
2075 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2077 if (bytes
> sizeof(fp
->buf
))
2081 return (cups_compress(fp
, buf
, bytes
));
2083 #endif /* HAVE_LIBZ */
2084 return (cups_write(fp
, buf
, bytes
));
2088 memcpy(fp
->ptr
, buf
, bytes
);
2090 return ((ssize_t
)bytes
);
2097 * 'cups_compress()' - Compress a buffer of data.
2100 static ssize_t
/* O - Number of bytes written or -1 */
2101 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2102 const char *buf
, /* I - Buffer */
2103 size_t bytes
) /* I - Number bytes */
2105 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2111 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2114 * Deflate the bytes...
2117 fp
->stream
.next_in
= (Bytef
*)buf
;
2118 fp
->stream
.avail_in
= (uInt
)bytes
;
2120 while (fp
->stream
.avail_in
> 0)
2123 * Flush the current buffer...
2126 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2127 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2129 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2131 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2134 fp
->stream
.next_out
= fp
->cbuf
;
2135 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2138 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2141 return ((ssize_t
)bytes
);
2143 #endif /* HAVE_LIBZ */
2147 * 'cups_fill()' - Fill the input buffer.
2150 static ssize_t
/* O - Number of bytes or -1 */
2151 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2153 ssize_t bytes
; /* Number of bytes read */
2155 int status
; /* Decompression status */
2156 const unsigned char *ptr
, /* Pointer into buffer */
2157 *end
; /* End of buffer */
2158 #endif /* HAVE_LIBZ */
2161 DEBUG_printf(("7cups_fill(fp=%p)", (void *)fp
));
2162 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, fp->bufpos=" CUPS_LLFMT
", fp->eof=%d", (void *)fp
->ptr
, (void *)fp
->end
, (void *)fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2164 if (fp
->ptr
&& fp
->end
)
2165 fp
->bufpos
+= fp
->end
- fp
->buf
;
2168 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2170 while (!fp
->ptr
|| fp
->compressed
)
2173 * Check to see if we have read any data yet; if not, see if we have a
2174 * compressed file...
2180 * Reset the file position in case we are seeking...
2186 * Read the first bytes in the file to determine if we have a gzip'd
2190 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2193 * Can't read from file!
2196 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2197 CUPS_LLCAST bytes
));
2204 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2205 (fp
->buf
[1] & 255) != 0x8b ||
2206 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2209 * Not a gzip'd file!
2213 fp
->end
= fp
->buf
+ bytes
;
2215 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2216 CUPS_LLCAST bytes
));
2222 * Parse header junk: extra data, original name, and comment...
2225 ptr
= (unsigned char *)fp
->buf
+ 10;
2226 end
= (unsigned char *)fp
->buf
+ bytes
;
2228 if (fp
->buf
[3] & 0x04)
2231 * Skip extra data...
2234 if ((ptr
+ 2) > end
)
2237 * Can't read from file!
2240 DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
2248 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2254 * Can't read from file!
2257 DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
2266 if (fp
->buf
[3] & 0x08)
2269 * Skip original name data...
2272 while (ptr
< end
&& *ptr
)
2280 * Can't read from file!
2283 DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
2292 if (fp
->buf
[3] & 0x10)
2295 * Skip comment data...
2298 while (ptr
< end
&& *ptr
)
2306 * Can't read from file!
2309 DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
2318 if (fp
->buf
[3] & 0x02)
2321 * Skip header CRC data...
2329 * Can't read from file!
2332 DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
2342 * Copy the flate-compressed data to the compression buffer...
2345 if ((bytes
= end
- ptr
) > 0)
2346 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2349 * Setup the decompressor data...
2352 fp
->stream
.zalloc
= (alloc_func
)0;
2353 fp
->stream
.zfree
= (free_func
)0;
2354 fp
->stream
.opaque
= (voidpf
)0;
2355 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2356 fp
->stream
.next_out
= NULL
;
2357 fp
->stream
.avail_in
= (uInt
)bytes
;
2358 fp
->stream
.avail_out
= 0;
2359 fp
->crc
= crc32(0L, Z_NULL
, 0);
2361 if ((status
= inflateInit2(&(fp
->stream
), -15)) != Z_OK
)
2363 DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status
));
2377 * If we have reached end-of-file, return immediately...
2382 DEBUG_puts("9cups_fill: EOF, returning 0.");
2388 * Fill the decompression buffer as needed...
2391 if (fp
->stream
.avail_in
== 0)
2393 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2395 DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes
));
2402 fp
->stream
.next_in
= fp
->cbuf
;
2403 fp
->stream
.avail_in
= (uInt
)bytes
;
2407 * Decompress data from the buffer...
2410 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2411 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2413 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2415 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2416 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2417 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2419 if (status
== Z_STREAM_END
)
2422 * Read the CRC and length...
2425 unsigned char trailer
[8]; /* Trailer bytes */
2426 uLong tcrc
; /* Trailer CRC */
2427 ssize_t tbytes
= 0; /* Number of bytes */
2429 if (fp
->stream
.avail_in
> 0)
2431 if (fp
->stream
.avail_in
> sizeof(trailer
))
2432 tbytes
= (ssize_t
)sizeof(trailer
);
2434 tbytes
= (ssize_t
)fp
->stream
.avail_in
;
2436 memcpy(trailer
, fp
->stream
.next_in
, (size_t)tbytes
);
2437 fp
->stream
.next_in
+= tbytes
;
2438 fp
->stream
.avail_in
-= (size_t)tbytes
;
2441 if (tbytes
< (ssize_t
)sizeof(trailer
))
2443 if (read(fp
->fd
, trailer
+ tbytes
, sizeof(trailer
) - (size_t)tbytes
) < ((ssize_t
)sizeof(trailer
) - tbytes
))
2446 * Can't get it, so mark end-of-file...
2449 DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
2458 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2459 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2461 if (tcrc
!= fp
->crc
)
2464 * Bad CRC, mark end-of-file...
2467 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2476 * Otherwise, reset the compressed flag so that we re-read the
2480 inflateEnd(&fp
->stream
);
2484 else if (status
< Z_OK
)
2486 DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status
));
2494 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2497 * Return the decompressed data...
2501 fp
->end
= fp
->buf
+ bytes
;
2505 DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes
));
2510 #endif /* HAVE_LIBZ */
2513 * Read a buffer's full of data...
2516 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2519 * Can't read from file!
2529 * Return the bytes we read...
2534 fp
->end
= fp
->buf
+ bytes
;
2537 DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes
));
2544 * 'cups_open()' - Safely open a file for writing.
2546 * We don't allow appending to directories or files that are hard-linked or
2550 static int /* O - File descriptor or -1 otherwise */
2551 cups_open(const char *filename
, /* I - Filename */
2552 int mode
) /* I - Open mode */
2554 int fd
; /* File descriptor */
2555 struct stat fileinfo
; /* File information */
2557 struct stat linkinfo
; /* Link information */
2565 if ((fd
= open(filename
, mode
, 0666)) < 0)
2569 * Then verify that the file descriptor doesn't point to a directory or hard-
2573 if (fstat(fd
, &fileinfo
))
2579 if (fileinfo
.st_nlink
!= 1)
2587 if (fileinfo
.st_mode
& _S_IFDIR
)
2589 if (S_ISDIR(fileinfo
.st_mode
))
2599 * Then use lstat to determine whether the filename is a symlink...
2602 if (lstat(filename
, &linkinfo
))
2608 if (S_ISLNK(linkinfo
.st_mode
) ||
2609 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2610 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2612 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2613 #endif /* HAVE_ST_GEN */
2614 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2615 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2632 * 'cups_read()' - Read from a file descriptor.
2635 static ssize_t
/* O - Number of bytes read or -1 */
2636 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2637 char *buf
, /* I - Buffer */
2638 size_t bytes
) /* I - Number bytes */
2640 ssize_t total
; /* Total bytes read */
2643 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2646 * Loop until we read at least 0 bytes...
2652 if (fp
->mode
== 's')
2653 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2655 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2657 if (fp
->mode
== 's')
2658 total
= recv(fp
->fd
, buf
, bytes
, 0);
2660 total
= read(fp
->fd
, buf
, bytes
);
2663 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2669 * Reads can be interrupted by signals and unavailable resources...
2672 if (errno
== EAGAIN
|| errno
== EINTR
)
2679 * Return the total number of bytes read...
2687 * 'cups_write()' - Write to a file descriptor.
2690 static ssize_t
/* O - Number of bytes written or -1 */
2691 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2692 const char *buf
, /* I - Buffer */
2693 size_t bytes
) /* I - Number bytes */
2695 size_t total
; /* Total bytes written */
2696 ssize_t count
; /* Count this time */
2699 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2702 * Loop until all bytes are written...
2709 if (fp
->mode
== 's')
2710 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2712 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2714 if (fp
->mode
== 's')
2715 count
= send(fp
->fd
, buf
, bytes
, 0);
2717 count
= write(fp
->fd
, buf
, bytes
);
2720 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2725 * Writes can be interrupted by signals and unavailable resources...
2728 if (errno
== EAGAIN
|| errno
== EINTR
)
2735 * Update the counts for the last write call...
2738 bytes
-= (size_t)count
;
2739 total
+= (size_t)count
;
2744 * Return the total number of bytes written...
2747 return ((ssize_t
)total
);