4 * File functions for CUPS.
6 * Since stdio files max out at 256 files on many systems, we have to
7 * write similar functions without this limit. At the same time, using
8 * our own file functions allows us to provide transparent support of
9 * gzip'd print files, PPD files, etc.
11 * Copyright 2007-2015 by Apple Inc.
12 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
14 * These coded instructions, statements, and computer programs are the
15 * property of Apple Inc. and are protected by Federal copyright
16 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
17 * which should have been included with this file. If this file is
18 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * This file is subject to the Apple OS-Developed Software exception.
24 * Include necessary headers...
27 #include "file-private.h"
29 #include <sys/types.h>
37 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
38 #endif /* HAVE_LIBZ */
39 static ssize_t
cups_fill(cups_file_t
*fp
);
40 static int cups_open(const char *filename
, int mode
);
41 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
42 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
47 * '_cupsFileCheck()' - Check the permissions of the given filename.
50 _cups_fc_result_t
/* O - Check result */
52 const char *filename
, /* I - Filename to check */
53 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
54 int dorootchecks
, /* I - Check for root permissions? */
55 _cups_fc_func_t cb
, /* I - Callback function */
56 void *context
) /* I - Context pointer for callback */
59 struct stat fileinfo
; /* File information */
60 char message
[1024], /* Message string */
61 temp
[1024], /* Parent directory filename */
62 *ptr
; /* Pointer into parent directory */
63 _cups_fc_result_t result
; /* Check result */
67 * Does the filename contain a relative path ("../")?
70 if (strstr(filename
, "../"))
76 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
81 * Does the program even exist and is it accessible?
84 if (stat(filename
, &fileinfo
))
90 result
= _CUPS_FILE_CHECK_MISSING
;
95 * Check the execute bit...
98 result
= _CUPS_FILE_CHECK_OK
;
102 case _CUPS_FILE_CHECK_DIRECTORY
:
103 if (!S_ISDIR(fileinfo
.st_mode
))
104 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
108 if (!S_ISREG(fileinfo
.st_mode
))
109 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
117 * Are we doing root checks?
123 * Nope, so anything (else) goes...
130 * Verify permission of the file itself:
132 * 1. Must be owned by root
133 * 2. Must not be writable by group
134 * 3. Must not be setuid
135 * 4. Must not be writable by others
138 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
139 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
140 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
141 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
143 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
147 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
148 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
152 * Now check the containing directory...
155 strlcpy(temp
, filename
, sizeof(temp
));
156 if ((ptr
= strrchr(temp
, '/')) != NULL
)
164 if (stat(temp
, &fileinfo
))
170 result
= _CUPS_FILE_CHECK_MISSING
;
171 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
177 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
178 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
179 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
180 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
182 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
183 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
188 * Common return point...
195 cups_lang_t
*lang
= cupsLangDefault();
196 /* Localization information */
200 case _CUPS_FILE_CHECK_OK
:
201 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
202 snprintf(message
, sizeof(message
),
203 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
204 "(0%o/uid=%d/gid=%d).")),
205 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
206 (int)fileinfo
.st_gid
);
208 snprintf(message
, sizeof(message
),
209 _cupsLangString(lang
, _("File \"%s\" permissions OK "
210 "(0%o/uid=%d/gid=%d).")),
211 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
212 (int)fileinfo
.st_gid
);
215 case _CUPS_FILE_CHECK_MISSING
:
216 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
217 snprintf(message
, sizeof(message
),
218 _cupsLangString(lang
, _("Directory \"%s\" not available: "
220 filename
, strerror(errno
));
222 snprintf(message
, sizeof(message
),
223 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
224 filename
, strerror(errno
));
227 case _CUPS_FILE_CHECK_PERMISSIONS
:
228 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
229 snprintf(message
, sizeof(message
),
230 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
232 "(0%o/uid=%d/gid=%d).")),
233 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
234 (int)fileinfo
.st_gid
);
236 snprintf(message
, sizeof(message
),
237 _cupsLangString(lang
, _("File \"%s\" has insecure "
239 "(0%o/uid=%d/gid=%d).")),
240 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
241 (int)fileinfo
.st_gid
);
244 case _CUPS_FILE_CHECK_WRONG_TYPE
:
245 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
246 snprintf(message
, sizeof(message
),
247 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
250 snprintf(message
, sizeof(message
),
251 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
255 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
256 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
257 snprintf(message
, sizeof(message
),
258 _cupsLangString(lang
, _("Directory \"%s\" contains a "
259 "relative path.")), filename
);
261 snprintf(message
, sizeof(message
),
262 _cupsLangString(lang
, _("File \"%s\" contains a relative "
263 "path.")), filename
);
267 (*cb
)(context
, result
, message
);
275 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
279 _cupsFileCheckFilter(
280 void *context
, /* I - Context pointer (unused) */
281 _cups_fc_result_t result
, /* I - Result code */
282 const char *message
) /* I - Message text */
284 const char *prefix
; /* Messaging prefix */
292 case _CUPS_FILE_CHECK_OK
:
296 case _CUPS_FILE_CHECK_MISSING
:
297 case _CUPS_FILE_CHECK_WRONG_TYPE
:
299 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
302 case _CUPS_FILE_CHECK_PERMISSIONS
:
303 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
305 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
309 fprintf(stderr
, "%s: %s\n", prefix
, message
);
315 * 'cupsFileClose()' - Close a CUPS file.
317 * @since CUPS 1.2/OS X 10.5@
320 int /* O - 0 on success, -1 on error */
321 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
323 int fd
; /* File descriptor */
324 char mode
; /* Open mode */
325 int status
; /* Return status */
326 int is_stdio
; /* Is a stdio file? */
329 DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp
));
339 * Flush pending write data...
343 status
= cupsFileFlush(fp
);
348 if (fp
->compressed
&& status
>= 0)
353 * Free decompression data...
356 inflateEnd(&fp
->stream
);
361 * Flush any remaining compressed data...
364 unsigned char trailer
[8]; /* Trailer CRC and length */
365 int done
; /* Done writing... */
368 fp
->stream
.avail_in
= 0;
372 if (fp
->stream
.next_out
> fp
->cbuf
)
374 if (cups_write(fp
, (char *)fp
->cbuf
,
375 (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
378 fp
->stream
.next_out
= fp
->cbuf
;
379 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
382 if (done
|| status
< 0)
385 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
386 fp
->stream
.next_out
== fp
->cbuf
;
390 * Write the CRC and length...
393 trailer
[0] = (unsigned char)fp
->crc
;
394 trailer
[1] = (unsigned char)(fp
->crc
>> 8);
395 trailer
[2] = (unsigned char)(fp
->crc
>> 16);
396 trailer
[3] = (unsigned char)(fp
->crc
>> 24);
397 trailer
[4] = (unsigned char)fp
->pos
;
398 trailer
[5] = (unsigned char)(fp
->pos
>> 8);
399 trailer
[6] = (unsigned char)(fp
->pos
>> 16);
400 trailer
[7] = (unsigned char)(fp
->pos
>> 24);
402 if (cups_write(fp
, (char *)trailer
, 8) < 0)
406 * Free all memory used by the compression stream...
409 deflateEnd(&(fp
->stream
));
412 #endif /* HAVE_LIBZ */
415 * Save the file descriptor we used and free memory...
420 is_stdio
= fp
->is_stdio
;
422 if (fp
->printf_buffer
)
423 free(fp
->printf_buffer
);
428 * Close the file, returning the close status...
433 if (httpAddrClose(NULL
, fd
) < 0)
447 * 'cupsFileCompression()' - Return whether a file is compressed.
449 * @since CUPS 1.2/OS X 10.5@
452 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
453 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
455 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
460 * 'cupsFileEOF()' - Return the end-of-file status.
462 * @since CUPS 1.2/OS X 10.5@
465 int /* O - 1 on end of file, 0 otherwise */
466 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
468 return (fp
? fp
->eof
: 1);
473 * 'cupsFileFind()' - Find a file using the specified path.
475 * This function allows the paths in the path string to be separated by
476 * colons (UNIX standard) or semicolons (Windows standard) and stores the
477 * result in the buffer supplied. If the file cannot be found in any of
478 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
479 * matches the current directory.
481 * @since CUPS 1.2/OS X 10.5@
484 const char * /* O - Full path to file or @code NULL@ if not found */
485 cupsFileFind(const char *filename
, /* I - File to find */
486 const char *path
, /* I - Colon/semicolon-separated path */
487 int executable
, /* I - 1 = executable files, 0 = any file/dir */
488 char *buffer
, /* I - Filename buffer */
489 int bufsize
) /* I - Size of filename buffer */
491 char *bufptr
, /* Current position in buffer */
492 *bufend
; /* End of buffer */
496 * Range check input...
499 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, buffer=%p, bufsize=%d)", filename
, path
, executable
, (void *)buffer
, bufsize
));
501 if (!filename
|| !buffer
|| bufsize
< 2)
507 * No path, so check current directory...
510 if (!access(filename
, 0))
512 strlcpy(buffer
, filename
, (size_t)bufsize
);
520 * Now check each path and return the first match...
523 bufend
= buffer
+ bufsize
- 1;
529 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
531 if (*path
== ';' || *path
== ':')
534 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
537 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
540 if (!access(buffer
, 0))
542 if (!access(buffer
, executable
? X_OK
: 0))
545 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
551 else if (bufptr
< bufend
)
558 * Check the last path...
561 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
564 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
566 if (!access(buffer
, 0))
568 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
573 DEBUG_puts("1cupsFileFind: Returning NULL");
580 * 'cupsFileFlush()' - Flush pending output.
582 * @since CUPS 1.2/OS X 10.5@
585 int /* O - 0 on success, -1 on error */
586 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
588 ssize_t bytes
; /* Bytes to write */
591 DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp
));
594 * Range check input...
597 if (!fp
|| fp
->mode
!= 'w')
599 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
603 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
605 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
612 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
614 #endif /* HAVE_LIBZ */
615 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
628 * 'cupsFileGetChar()' - Get a single character from a file.
630 * @since CUPS 1.2/OS X 10.5@
633 int /* O - Character or -1 on end of file */
634 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
637 * Range check input...
640 DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp
));
642 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
644 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
649 * If the input buffer is empty, try to read more data...
652 DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp
->eof
, (void *)fp
->ptr
, (void *)fp
->end
));
654 if (fp
->ptr
>= fp
->end
)
655 if (cups_fill(fp
) <= 0)
657 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
662 * Return the next character in the buffer...
665 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
669 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
671 return (*(fp
->ptr
)++ & 255);
676 * 'cupsFileGetConf()' - Get a line from a configuration file.
678 * @since CUPS 1.2/OS X 10.5@
681 char * /* O - Line read or @code NULL@ on end of file or error */
682 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
683 char *buf
, /* O - String buffer */
684 size_t buflen
, /* I - Size of string buffer */
685 char **value
, /* O - Pointer to value */
686 int *linenum
) /* IO - Current line number */
688 char *ptr
; /* Pointer into line */
692 * Range check input...
695 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
696 ", value=%p, linenum=%p)", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
, (void *)value
, (void *)linenum
));
698 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
699 !buf
|| buflen
< 2 || !value
)
708 * Read the next non-comment line...
713 while (cupsFileGets(fp
, buf
, buflen
))
718 * Strip any comments...
721 if ((ptr
= strchr(buf
, '#')) != NULL
)
723 if (ptr
> buf
&& ptr
[-1] == '\\')
726 _cups_strcpy(ptr
- 1, ptr
);
730 // Strip the comment and any trailing whitespace...
733 if (!_cups_isspace(ptr
[-1]))
744 * Strip leading whitespace...
747 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
750 _cups_strcpy(buf
, ptr
);
753 * See if there is anything left...
759 * Yes, grab any value and return...
762 for (ptr
= buf
; *ptr
; ptr
++)
763 if (_cups_isspace(*ptr
))
769 * Have a value, skip any other spaces...
772 while (_cups_isspace(*ptr
))
779 * Strip trailing whitespace and > for lines that begin with <...
782 ptr
+= strlen(ptr
) - 1;
784 if (buf
[0] == '<' && *ptr
== '>')
786 else if (buf
[0] == '<' && *ptr
!= '>')
796 while (ptr
> *value
&& _cups_isspace(*ptr
))
813 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
814 * contain binary data.
816 * This function differs from @link cupsFileGets@ in that the trailing CR
817 * and LF are preserved, as is any binary data on the line. The buffer is
818 * nul-terminated, however you should use the returned length to determine
819 * the number of bytes on the line.
821 * @since CUPS 1.2/OS X 10.5@
824 size_t /* O - Number of bytes on line or 0 on end of file */
825 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
826 char *buf
, /* I - Buffer */
827 size_t buflen
) /* I - Size of buffer */
829 int ch
; /* Character from file */
830 char *ptr
, /* Current position in line buffer */
831 *end
; /* End of line buffer */
835 * Range check input...
838 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
840 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
844 * Now loop until we have a valid line...
847 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
849 if (fp
->ptr
>= fp
->end
)
850 if (cups_fill(fp
) <= 0)
853 *ptr
++ = ch
= *(fp
->ptr
)++;
862 if (fp
->ptr
>= fp
->end
)
863 if (cups_fill(fp
) <= 0)
866 if (*(fp
->ptr
) == '\n')
868 *ptr
++ = *(fp
->ptr
)++;
877 * Line feed ends a line...
886 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
888 return ((size_t)(ptr
- buf
));
893 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
895 * @since CUPS 1.2/OS X 10.5@
898 char * /* O - Line read or @code NULL@ on end of file or error */
899 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
900 char *buf
, /* O - String buffer */
901 size_t buflen
) /* I - Size of string buffer */
903 int ch
; /* Character from file */
904 char *ptr
, /* Current position in line buffer */
905 *end
; /* End of line buffer */
909 * Range check input...
912 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
914 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
918 * Now loop until we have a valid line...
921 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
923 if (fp
->ptr
>= fp
->end
)
924 if (cups_fill(fp
) <= 0)
941 if (fp
->ptr
>= fp
->end
)
942 if (cups_fill(fp
) <= 0)
945 if (*(fp
->ptr
) == '\n')
956 * Line feed ends a line...
967 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
974 * 'cupsFileLock()' - Temporarily lock access to a file.
976 * @since CUPS 1.2/OS X 10.5@
979 int /* O - 0 on success, -1 on error */
980 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
981 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
987 if (!fp
|| fp
->mode
== 's')
995 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
997 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1003 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1005 * @since CUPS 1.2/OS X 10.5@
1008 int /* O - File descriptor */
1009 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1019 * 'cupsFileOpen()' - Open a CUPS file.
1021 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1022 * existing file, "a" to append to an existing file or create a new file,
1023 * or "s" to open a socket connection.
1025 * When opening for writing ("w"), an optional number from 1 to 9 can be
1026 * supplied which enables Flate compression of the file. Compression is
1027 * not supported for the "a" (append) mode.
1029 * When opening a socket connection, the filename is a string of the form
1030 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1031 * connection as needed, generally preferring IPv6 connections when there is
1034 * @since CUPS 1.2/OS X 10.5@
1037 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1038 cupsFileOpen(const char *filename
, /* I - Name of file */
1039 const char *mode
) /* I - Open mode */
1041 cups_file_t
*fp
; /* New CUPS file */
1042 int fd
; /* File descriptor */
1043 char hostname
[1024], /* Hostname */
1044 *portname
; /* Port "name" (number or service) */
1045 http_addrlist_t
*addrlist
; /* Host address list */
1048 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1052 * Range check input...
1055 if (!filename
|| !mode
||
1056 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1057 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1066 case 'a' : /* Append file */
1067 fd
= cups_open(filename
,
1068 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1071 case 'r' : /* Read file */
1072 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1075 case 'w' : /* Write file */
1076 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1077 if (fd
< 0 && errno
== ENOENT
)
1079 fd
= cups_open(filename
,
1080 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1081 if (fd
< 0 && errno
== EEXIST
)
1082 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1093 case 's' : /* Read/write socket */
1094 strlcpy(hostname
, filename
, sizeof(hostname
));
1095 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1101 * Lookup the hostname and service...
1104 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1108 * Connect to the server...
1111 if (!httpAddrConnect(addrlist
, &fd
))
1113 httpAddrFreeList(addrlist
);
1117 httpAddrFreeList(addrlist
);
1120 default : /* Remove bogus compiler warning... */
1128 * Create the CUPS file structure...
1131 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1134 httpAddrClose(NULL
, fd
);
1147 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1149 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1150 * or "s" to treat the file descriptor as a bidirectional socket connection.
1152 * When opening for writing ("w"), an optional number from 1 to 9 can be
1153 * supplied which enables Flate compression of the file. Compression is
1154 * not supported for the "a" (append) mode.
1156 * @since CUPS 1.2/OS X 10.5@
1159 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1160 cupsFileOpenFd(int fd
, /* I - File descriptor */
1161 const char *mode
) /* I - Open mode */
1163 cups_file_t
*fp
; /* New CUPS file */
1166 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1169 * Range check input...
1172 if (fd
< 0 || !mode
||
1173 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1174 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1178 * Allocate memory...
1181 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1193 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1198 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1201 if (mode
[1] >= '1' && mode
[1] <= '9')
1204 * Open a compressed stream, so write the standard gzip file
1208 unsigned char header
[10]; /* gzip file header */
1209 time_t curtime
; /* Current time */
1212 curtime
= time(NULL
);
1215 header
[2] = Z_DEFLATED
;
1217 header
[4] = (unsigned char)curtime
;
1218 header
[5] = (unsigned char)(curtime
>> 8);
1219 header
[6] = (unsigned char)(curtime
>> 16);
1220 header
[7] = (unsigned char)(curtime
>> 24);
1224 cups_write(fp
, (char *)header
, 10);
1227 * Initialize the compressor...
1230 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1231 Z_DEFAULT_STRATEGY
);
1233 fp
->stream
.next_out
= fp
->cbuf
;
1234 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1236 fp
->crc
= crc32(0L, Z_NULL
, 0);
1238 #endif /* HAVE_LIBZ */
1249 default : /* Remove bogus compiler warning... */
1254 * Don't pass this file to child processes...
1258 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1266 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1268 * @since CUPS 1.2/OS X 10.5@
1271 int /* O - Character or -1 on end of file */
1272 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1275 * Range check input...
1278 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1282 * If the input buffer is empty, try to read more data...
1285 if (fp
->ptr
>= fp
->end
)
1286 if (cups_fill(fp
) <= 0)
1290 * Return the next character in the buffer...
1293 return (*(fp
->ptr
) & 255);
1298 * 'cupsFilePrintf()' - Write a formatted string.
1300 * @since CUPS 1.2/OS X 10.5@
1303 int /* O - Number of bytes written or -1 on error */
1304 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1305 const char *format
, /* I - Printf-style format string */
1306 ...) /* I - Additional args as necessary */
1308 va_list ap
; /* Argument list */
1309 ssize_t bytes
; /* Formatted size */
1312 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp
, format
));
1314 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1317 if (!fp
->printf_buffer
)
1320 * Start with an 1k printf buffer...
1323 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1326 fp
->printf_size
= 1024;
1329 va_start(ap
, format
);
1330 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1333 if (bytes
>= (ssize_t
)fp
->printf_size
)
1336 * Expand the printf buffer...
1339 char *temp
; /* Temporary buffer pointer */
1345 if ((temp
= realloc(fp
->printf_buffer
, (size_t)(bytes
+ 1))) == NULL
)
1348 fp
->printf_buffer
= temp
;
1349 fp
->printf_size
= (size_t)(bytes
+ 1);
1351 va_start(ap
, format
);
1352 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1356 if (fp
->mode
== 's')
1358 if (cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
) < 0)
1363 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1365 return ((int)bytes
);
1368 if ((fp
->ptr
+ bytes
) > fp
->end
)
1369 if (cupsFileFlush(fp
))
1374 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1376 if ((size_t)bytes
> sizeof(fp
->buf
))
1380 return ((int)cups_compress(fp
, fp
->printf_buffer
, (size_t)bytes
));
1382 #endif /* HAVE_LIBZ */
1383 return ((int)cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
));
1387 memcpy(fp
->ptr
, fp
->printf_buffer
, (size_t)bytes
);
1389 return ((int)bytes
);
1395 * 'cupsFilePutChar()' - Write a character.
1397 * @since CUPS 1.2/OS X 10.5@
1400 int /* O - 0 on success, -1 on error */
1401 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1402 int c
) /* I - Character to write */
1405 * Range check input...
1408 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1411 if (fp
->mode
== 's')
1414 * Send character immediately over socket...
1417 char ch
; /* Output character */
1422 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1431 if (fp
->ptr
>= fp
->end
)
1432 if (cupsFileFlush(fp
))
1435 *(fp
->ptr
) ++ = (char)c
;
1440 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1447 * 'cupsFilePutConf()' - Write a configuration line.
1449 * This function handles any comment escaping of the value.
1451 * @since CUPS 1.4/OS X 10.6@
1454 ssize_t
/* O - Number of bytes written or -1 on error */
1455 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1456 const char *directive
, /* I - Directive */
1457 const char *value
) /* I - Value */
1459 ssize_t bytes
, /* Number of bytes written */
1460 temp
; /* Temporary byte count */
1461 const char *ptr
; /* Pointer into value */
1464 if (!fp
|| !directive
|| !*directive
)
1467 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1470 if (cupsFilePutChar(fp
, ' ') < 0)
1474 if (value
&& *value
)
1476 if ((ptr
= strchr(value
, '#')) != NULL
)
1479 * Need to quote the first # in the info string...
1482 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1486 if (cupsFilePutChar(fp
, '\\') < 0)
1490 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1494 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1500 if (cupsFilePutChar(fp
, '\n') < 0)
1508 * 'cupsFilePuts()' - Write a string.
1510 * Like the @code fputs@ function, no newline is appended to the string.
1512 * @since CUPS 1.2/OS X 10.5@
1515 int /* O - Number of bytes written or -1 on error */
1516 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1517 const char *s
) /* I - String to write */
1519 ssize_t bytes
; /* Bytes to write */
1523 * Range check input...
1526 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1530 * Write the string...
1533 bytes
= (ssize_t
)strlen(s
);
1535 if (fp
->mode
== 's')
1537 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1542 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1544 return ((int)bytes
);
1547 if ((fp
->ptr
+ bytes
) > fp
->end
)
1548 if (cupsFileFlush(fp
))
1553 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1555 if ((size_t)bytes
> sizeof(fp
->buf
))
1559 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1561 #endif /* HAVE_LIBZ */
1562 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1566 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1568 return ((int)bytes
);
1574 * 'cupsFileRead()' - Read from a file.
1576 * @since CUPS 1.2/OS X 10.5@
1579 ssize_t
/* O - Number of bytes read or -1 on error */
1580 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1581 char *buf
, /* O - Buffer */
1582 size_t bytes
) /* I - Number of bytes to read */
1584 size_t total
; /* Total bytes read */
1585 ssize_t count
; /* Bytes read */
1588 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
1591 * Range check input...
1594 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1601 * Loop until all bytes are read...
1607 if (fp
->ptr
>= fp
->end
)
1608 if (cups_fill(fp
) <= 0)
1610 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1611 CUPS_LLFMT
, CUPS_LLCAST total
));
1614 return ((ssize_t
)total
);
1619 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1620 if (count
> (ssize_t
)bytes
)
1621 count
= (ssize_t
)bytes
;
1623 memcpy(buf
, fp
->ptr
,(size_t) count
);
1627 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1630 * Update the counts for the last read...
1633 bytes
-= (size_t)count
;
1634 total
+= (size_t)count
;
1639 * Return the total number of bytes read...
1642 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1644 return ((ssize_t
)total
);
1649 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1652 * @since CUPS 1.2/OS X 10.5@
1655 off_t
/* O - New file position or -1 on error */
1656 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1659 * Range check input...
1662 DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp
));
1663 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1665 if (!fp
|| fp
->mode
!= 'r')
1669 * Handle special cases...
1672 if (fp
->bufpos
== 0)
1675 * No seeking necessary...
1686 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1692 * Otherwise, seek in the file and cleanup any compression buffers...
1698 inflateEnd(&fp
->stream
);
1701 #endif /* HAVE_LIBZ */
1703 if (lseek(fp
->fd
, 0, SEEK_SET
))
1705 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1715 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1722 * 'cupsFileSeek()' - Seek in a file.
1724 * @since CUPS 1.2/OS X 10.5@
1727 off_t
/* O - New file position or -1 on error */
1728 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1729 off_t pos
) /* I - Position in file */
1731 ssize_t bytes
; /* Number bytes in buffer */
1734 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", (void *)fp
, CUPS_LLCAST pos
));
1735 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1736 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", (void *)fp
->ptr
, (void *)fp
->end
));
1739 * Range check input...
1742 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1746 * Handle special cases...
1750 return (cupsFileRewind(fp
));
1754 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1756 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1758 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1761 * No seeking necessary...
1765 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1773 if (!fp
->compressed
&& !fp
->ptr
)
1776 * Preload a buffer to determine whether the file is compressed...
1779 if (cups_fill(fp
) <= 0)
1782 #endif /* HAVE_LIBZ */
1785 * Seek forwards or backwards...
1790 if (pos
< fp
->bufpos
)
1793 * Need to seek backwards...
1796 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1801 inflateEnd(&fp
->stream
);
1803 lseek(fp
->fd
, 0, SEEK_SET
);
1809 while ((bytes
= cups_fill(fp
)) > 0)
1810 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1816 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1820 #endif /* HAVE_LIBZ */
1822 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1823 fp
->pos
= fp
->bufpos
;
1827 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1828 CUPS_LLCAST fp
->pos
));
1834 * Need to seek forwards...
1837 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1842 while ((bytes
= cups_fill(fp
)) > 0)
1844 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1851 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1855 #endif /* HAVE_LIBZ */
1857 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1858 fp
->pos
= fp
->bufpos
;
1862 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1863 CUPS_LLCAST fp
->pos
));
1867 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1874 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1876 * @since CUPS 1.2/OS X 10.5@
1879 cups_file_t
* /* O - CUPS file */
1880 cupsFileStderr(void)
1882 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1886 * Open file descriptor 2 as needed...
1889 if (!cg
->stdio_files
[2])
1892 * Flush any pending output on the stdio file...
1898 * Open file descriptor 2...
1901 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1902 cg
->stdio_files
[2]->is_stdio
= 1;
1905 return (cg
->stdio_files
[2]);
1910 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1912 * @since CUPS 1.2/OS X 10.5@
1915 cups_file_t
* /* O - CUPS file */
1918 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1922 * Open file descriptor 0 as needed...
1925 if (!cg
->stdio_files
[0])
1928 * Open file descriptor 0...
1931 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1932 cg
->stdio_files
[0]->is_stdio
= 1;
1935 return (cg
->stdio_files
[0]);
1940 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1942 * @since CUPS 1.2/OS X 10.5@
1945 cups_file_t
* /* O - CUPS file */
1946 cupsFileStdout(void)
1948 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1952 * Open file descriptor 1 as needed...
1955 if (!cg
->stdio_files
[1])
1958 * Flush any pending output on the stdio file...
1964 * Open file descriptor 1...
1967 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1968 cg
->stdio_files
[1]->is_stdio
= 1;
1971 return (cg
->stdio_files
[1]);
1976 * 'cupsFileTell()' - Return the current file position.
1978 * @since CUPS 1.2/OS X 10.5@
1981 off_t
/* O - File position */
1982 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1984 DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp
));
1985 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
, CUPS_LLCAST (fp
? fp
->pos
: -1)));
1987 return (fp
? fp
->pos
: 0);
1992 * 'cupsFileUnlock()' - Unlock access to a file.
1994 * @since CUPS 1.2/OS X 10.5@
1997 int /* O - 0 on success, -1 on error */
1998 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2004 DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp
));
2006 if (!fp
|| fp
->mode
== 's')
2014 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2016 return (lockf(fp
->fd
, F_ULOCK
, 0));
2022 * 'cupsFileWrite()' - Write to a file.
2024 * @since CUPS 1.2/OS X 10.5@
2027 ssize_t
/* O - Number of bytes written or -1 on error */
2028 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2029 const char *buf
, /* I - Buffer */
2030 size_t bytes
) /* I - Number of bytes to write */
2033 * Range check input...
2036 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2038 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2045 * Write the buffer...
2048 if (fp
->mode
== 's')
2050 if (cups_write(fp
, buf
, bytes
) < 0)
2053 fp
->pos
+= (off_t
)bytes
;
2055 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2057 return ((ssize_t
)bytes
);
2060 if ((fp
->ptr
+ bytes
) > fp
->end
)
2061 if (cupsFileFlush(fp
))
2064 fp
->pos
+= (off_t
)bytes
;
2066 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2068 if (bytes
> sizeof(fp
->buf
))
2072 return (cups_compress(fp
, buf
, bytes
));
2074 #endif /* HAVE_LIBZ */
2075 return (cups_write(fp
, buf
, bytes
));
2079 memcpy(fp
->ptr
, buf
, bytes
);
2081 return ((ssize_t
)bytes
);
2088 * 'cups_compress()' - Compress a buffer of data.
2091 static ssize_t
/* O - Number of bytes written or -1 */
2092 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2093 const char *buf
, /* I - Buffer */
2094 size_t bytes
) /* I - Number bytes */
2096 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2102 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2105 * Deflate the bytes...
2108 fp
->stream
.next_in
= (Bytef
*)buf
;
2109 fp
->stream
.avail_in
= (uInt
)bytes
;
2111 while (fp
->stream
.avail_in
> 0)
2114 * Flush the current buffer...
2117 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2118 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2120 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2122 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2125 fp
->stream
.next_out
= fp
->cbuf
;
2126 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2129 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2132 return ((ssize_t
)bytes
);
2134 #endif /* HAVE_LIBZ */
2138 * 'cups_fill()' - Fill the input buffer.
2141 static ssize_t
/* O - Number of bytes or -1 */
2142 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2144 ssize_t bytes
; /* Number of bytes read */
2146 int status
; /* Decompression status */
2147 const unsigned char *ptr
, /* Pointer into buffer */
2148 *end
; /* End of buffer */
2149 #endif /* HAVE_LIBZ */
2152 DEBUG_printf(("7cups_fill(fp=%p)", (void *)fp
));
2153 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
));
2155 if (fp
->ptr
&& fp
->end
)
2156 fp
->bufpos
+= fp
->end
- fp
->buf
;
2159 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2161 while (!fp
->ptr
|| fp
->compressed
)
2164 * Check to see if we have read any data yet; if not, see if we have a
2165 * compressed file...
2171 * Reset the file position in case we are seeking...
2177 * Read the first bytes in the file to determine if we have a gzip'd
2181 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2184 * Can't read from file!
2187 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2188 CUPS_LLCAST bytes
));
2195 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2196 (fp
->buf
[1] & 255) != 0x8b ||
2197 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2200 * Not a gzip'd file!
2204 fp
->end
= fp
->buf
+ bytes
;
2206 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2207 CUPS_LLCAST bytes
));
2213 * Parse header junk: extra data, original name, and comment...
2216 ptr
= (unsigned char *)fp
->buf
+ 10;
2217 end
= (unsigned char *)fp
->buf
+ bytes
;
2219 if (fp
->buf
[3] & 0x04)
2222 * Skip extra data...
2225 if ((ptr
+ 2) > end
)
2228 * Can't read from file!
2231 DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
2239 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2245 * Can't read from file!
2248 DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
2257 if (fp
->buf
[3] & 0x08)
2260 * Skip original name data...
2263 while (ptr
< end
&& *ptr
)
2271 * Can't read from file!
2274 DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
2283 if (fp
->buf
[3] & 0x10)
2286 * Skip comment data...
2289 while (ptr
< end
&& *ptr
)
2297 * Can't read from file!
2300 DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
2309 if (fp
->buf
[3] & 0x02)
2312 * Skip header CRC data...
2320 * Can't read from file!
2323 DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
2333 * Copy the flate-compressed data to the compression buffer...
2336 if ((bytes
= end
- ptr
) > 0)
2337 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2340 * Setup the decompressor data...
2343 fp
->stream
.zalloc
= (alloc_func
)0;
2344 fp
->stream
.zfree
= (free_func
)0;
2345 fp
->stream
.opaque
= (voidpf
)0;
2346 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2347 fp
->stream
.next_out
= NULL
;
2348 fp
->stream
.avail_in
= (uInt
)bytes
;
2349 fp
->stream
.avail_out
= 0;
2350 fp
->crc
= crc32(0L, Z_NULL
, 0);
2352 if ((status
= inflateInit2(&(fp
->stream
), -15)) != Z_OK
)
2354 DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status
));
2368 * If we have reached end-of-file, return immediately...
2373 DEBUG_puts("9cups_fill: EOF, returning 0.");
2379 * Fill the decompression buffer as needed...
2382 if (fp
->stream
.avail_in
== 0)
2384 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2386 DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes
));
2393 fp
->stream
.next_in
= fp
->cbuf
;
2394 fp
->stream
.avail_in
= (uInt
)bytes
;
2398 * Decompress data from the buffer...
2401 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2402 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2404 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2406 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2407 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2408 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2410 if (status
== Z_STREAM_END
)
2413 * Read the CRC and length...
2416 unsigned char trailer
[8]; /* Trailer bytes */
2417 uLong tcrc
; /* Trailer CRC */
2418 ssize_t tbytes
= 0; /* Number of bytes */
2420 if (fp
->stream
.avail_in
> 0)
2422 if (fp
->stream
.avail_in
> sizeof(trailer
))
2423 tbytes
= (ssize_t
)sizeof(trailer
);
2425 tbytes
= (ssize_t
)fp
->stream
.avail_in
;
2427 memcpy(trailer
, fp
->stream
.next_in
, (size_t)tbytes
);
2428 fp
->stream
.next_in
+= tbytes
;
2429 fp
->stream
.avail_in
-= (size_t)tbytes
;
2432 if (tbytes
< (ssize_t
)sizeof(trailer
))
2434 if (read(fp
->fd
, trailer
+ tbytes
, sizeof(trailer
) - (size_t)tbytes
) < ((ssize_t
)sizeof(trailer
) - tbytes
))
2437 * Can't get it, so mark end-of-file...
2440 DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
2449 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2450 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2452 if (tcrc
!= fp
->crc
)
2455 * Bad CRC, mark end-of-file...
2458 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2467 * Otherwise, reset the compressed flag so that we re-read the
2473 else if (status
< Z_OK
)
2475 DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status
));
2483 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2486 * Return the decompressed data...
2490 fp
->end
= fp
->buf
+ bytes
;
2494 DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes
));
2499 #endif /* HAVE_LIBZ */
2502 * Read a buffer's full of data...
2505 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2508 * Can't read from file!
2518 * Return the bytes we read...
2523 fp
->end
= fp
->buf
+ bytes
;
2526 DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes
));
2533 * 'cups_open()' - Safely open a file for writing.
2535 * We don't allow appending to directories or files that are hard-linked or
2539 static int /* O - File descriptor or -1 otherwise */
2540 cups_open(const char *filename
, /* I - Filename */
2541 int mode
) /* I - Open mode */
2543 int fd
; /* File descriptor */
2544 struct stat fileinfo
; /* File information */
2546 struct stat linkinfo
; /* Link information */
2554 if ((fd
= open(filename
, mode
, 0666)) < 0)
2558 * Then verify that the file descriptor doesn't point to a directory or hard-
2562 if (fstat(fd
, &fileinfo
))
2568 if (fileinfo
.st_nlink
!= 1)
2576 if (fileinfo
.st_mode
& _S_IFDIR
)
2578 if (S_ISDIR(fileinfo
.st_mode
))
2588 * Then use lstat to determine whether the filename is a symlink...
2591 if (lstat(filename
, &linkinfo
))
2597 if (S_ISLNK(linkinfo
.st_mode
) ||
2598 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2599 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2601 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2602 #endif /* HAVE_ST_GEN */
2603 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2604 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2621 * 'cups_read()' - Read from a file descriptor.
2624 static ssize_t
/* O - Number of bytes read or -1 */
2625 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2626 char *buf
, /* I - Buffer */
2627 size_t bytes
) /* I - Number bytes */
2629 ssize_t total
; /* Total bytes read */
2632 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2635 * Loop until we read at least 0 bytes...
2641 if (fp
->mode
== 's')
2642 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2644 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2646 if (fp
->mode
== 's')
2647 total
= recv(fp
->fd
, buf
, bytes
, 0);
2649 total
= read(fp
->fd
, buf
, bytes
);
2652 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2658 * Reads can be interrupted by signals and unavailable resources...
2661 if (errno
== EAGAIN
|| errno
== EINTR
)
2668 * Return the total number of bytes read...
2676 * 'cups_write()' - Write to a file descriptor.
2679 static ssize_t
/* O - Number of bytes written or -1 */
2680 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2681 const char *buf
, /* I - Buffer */
2682 size_t bytes
) /* I - Number bytes */
2684 size_t total
; /* Total bytes written */
2685 ssize_t count
; /* Count this time */
2688 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2691 * Loop until all bytes are written...
2698 if (fp
->mode
== 's')
2699 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2701 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2703 if (fp
->mode
== 's')
2704 count
= send(fp
->fd
, buf
, bytes
, 0);
2706 count
= write(fp
->fd
, buf
, bytes
);
2709 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2714 * Writes can be interrupted by signals and unavailable resources...
2717 if (errno
== EAGAIN
|| errno
== EINTR
)
2724 * Update the counts for the last write call...
2727 bytes
-= (size_t)count
;
2728 total
+= (size_t)count
;
2733 * Return the total number of bytes written...
2736 return ((ssize_t
)total
);