2 * "$Id: file.c 12297 2014-12-08 19:26:32Z msweet $"
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-2014 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)", 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, "
500 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
503 if (!filename
|| !buffer
|| bufsize
< 2)
509 * No path, so check current directory...
512 if (!access(filename
, 0))
514 strlcpy(buffer
, filename
, (size_t)bufsize
);
522 * Now check each path and return the first match...
525 bufend
= buffer
+ bufsize
- 1;
531 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
533 if (*path
== ';' || *path
== ':')
536 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
539 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
542 if (!access(buffer
, 0))
544 if (!access(buffer
, executable
? X_OK
: 0))
547 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
553 else if (bufptr
< bufend
)
560 * Check the last path...
563 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
566 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
568 if (!access(buffer
, 0))
570 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
575 DEBUG_puts("1cupsFileFind: Returning NULL");
582 * 'cupsFileFlush()' - Flush pending output.
584 * @since CUPS 1.2/OS X 10.5@
587 int /* O - 0 on success, -1 on error */
588 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
590 ssize_t bytes
; /* Bytes to write */
593 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
596 * Range check input...
599 if (!fp
|| fp
->mode
!= 'w')
601 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
605 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
607 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
614 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
616 #endif /* HAVE_LIBZ */
617 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
630 * 'cupsFileGetChar()' - Get a single character from a file.
632 * @since CUPS 1.2/OS X 10.5@
635 int /* O - Character or -1 on end of file */
636 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
639 * Range check input...
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 if (fp
->ptr
>= fp
->end
)
653 if (cups_fill(fp
) < 0)
655 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
660 * Return the next character in the buffer...
663 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
667 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
669 return (*(fp
->ptr
)++ & 255);
674 * 'cupsFileGetConf()' - Get a line from a configuration file.
676 * @since CUPS 1.2/OS X 10.5@
679 char * /* O - Line read or @code NULL@ on end of file or error */
680 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
681 char *buf
, /* O - String buffer */
682 size_t buflen
, /* I - Size of string buffer */
683 char **value
, /* O - Pointer to value */
684 int *linenum
) /* IO - Current line number */
686 char *ptr
; /* Pointer into line */
690 * Range check input...
693 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
694 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
697 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
698 !buf
|| buflen
< 2 || !value
)
707 * Read the next non-comment line...
712 while (cupsFileGets(fp
, buf
, buflen
))
717 * Strip any comments...
720 if ((ptr
= strchr(buf
, '#')) != NULL
)
722 if (ptr
> buf
&& ptr
[-1] == '\\')
725 _cups_strcpy(ptr
- 1, ptr
);
729 // Strip the comment and any trailing whitespace...
732 if (!_cups_isspace(ptr
[-1]))
743 * Strip leading whitespace...
746 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
749 _cups_strcpy(buf
, ptr
);
752 * See if there is anything left...
758 * Yes, grab any value and return...
761 for (ptr
= buf
; *ptr
; ptr
++)
762 if (_cups_isspace(*ptr
))
768 * Have a value, skip any other spaces...
771 while (_cups_isspace(*ptr
))
778 * Strip trailing whitespace and > for lines that begin with <...
781 ptr
+= strlen(ptr
) - 1;
783 if (buf
[0] == '<' && *ptr
== '>')
785 else if (buf
[0] == '<' && *ptr
!= '>')
795 while (ptr
> *value
&& _cups_isspace(*ptr
))
812 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
813 * contain binary data.
815 * This function differs from @link cupsFileGets@ in that the trailing CR
816 * and LF are preserved, as is any binary data on the line. The buffer is
817 * nul-terminated, however you should use the returned length to determine
818 * the number of bytes on the line.
820 * @since CUPS 1.2/OS X 10.5@
823 size_t /* O - Number of bytes on line or 0 on end of file */
824 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
825 char *buf
, /* I - Buffer */
826 size_t buflen
) /* I - Size of buffer */
828 int ch
; /* Character from file */
829 char *ptr
, /* Current position in line buffer */
830 *end
; /* End of line buffer */
834 * Range check input...
837 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
838 fp
, 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
")", fp
, buf
,
913 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/OS X 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/OS X 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/OS X 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/OS X 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/OS X 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/OS X 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\", ...)", 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
);
1390 return ((int)bytes
);
1396 * 'cupsFilePutChar()' - Write a character.
1398 * @since CUPS 1.2/OS X 10.5@
1401 int /* O - 0 on success, -1 on error */
1402 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1403 int c
) /* I - Character to write */
1406 * Range check input...
1409 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1412 if (fp
->mode
== 's')
1415 * Send character immediately over socket...
1418 char ch
; /* Output character */
1423 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1432 if (fp
->ptr
>= fp
->end
)
1433 if (cupsFileFlush(fp
))
1436 *(fp
->ptr
) ++ = (char)c
;
1441 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1448 * 'cupsFilePutConf()' - Write a configuration line.
1450 * This function handles any comment escaping of the value.
1452 * @since CUPS 1.4/OS X 10.6@
1455 ssize_t
/* O - Number of bytes written or -1 on error */
1456 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1457 const char *directive
, /* I - Directive */
1458 const char *value
) /* I - Value */
1460 ssize_t bytes
, /* Number of bytes written */
1461 temp
; /* Temporary byte count */
1462 const char *ptr
; /* Pointer into value */
1465 if (!fp
|| !directive
|| !*directive
)
1468 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1471 if (cupsFilePutChar(fp
, ' ') < 0)
1475 if (value
&& *value
)
1477 if ((ptr
= strchr(value
, '#')) != NULL
)
1480 * Need to quote the first # in the info string...
1483 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1487 if (cupsFilePutChar(fp
, '\\') < 0)
1491 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1495 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1501 if (cupsFilePutChar(fp
, '\n') < 0)
1509 * 'cupsFilePuts()' - Write a string.
1511 * Like the @code fputs@ function, no newline is appended to the string.
1513 * @since CUPS 1.2/OS X 10.5@
1516 int /* O - Number of bytes written or -1 on error */
1517 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1518 const char *s
) /* I - String to write */
1520 ssize_t bytes
; /* Bytes to write */
1524 * Range check input...
1527 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1531 * Write the string...
1534 bytes
= (ssize_t
)strlen(s
);
1536 if (fp
->mode
== 's')
1538 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1543 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1545 return ((int)bytes
);
1548 if ((fp
->ptr
+ bytes
) > fp
->end
)
1549 if (cupsFileFlush(fp
))
1554 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1556 if ((size_t)bytes
> sizeof(fp
->buf
))
1560 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1562 #endif /* HAVE_LIBZ */
1563 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1567 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1569 return ((int)bytes
);
1575 * 'cupsFileRead()' - Read from a file.
1577 * @since CUPS 1.2/OS X 10.5@
1580 ssize_t
/* O - Number of bytes read or -1 on error */
1581 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1582 char *buf
, /* O - Buffer */
1583 size_t bytes
) /* I - Number of bytes to read */
1585 size_t total
; /* Total bytes read */
1586 ssize_t count
; /* Bytes read */
1589 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1590 CUPS_LLCAST bytes
));
1593 * Range check input...
1596 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1603 * Loop until all bytes are read...
1609 if (fp
->ptr
>= fp
->end
)
1610 if (cups_fill(fp
) <= 0)
1612 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1613 CUPS_LLFMT
, CUPS_LLCAST total
));
1616 return ((ssize_t
)total
);
1621 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1622 if (count
> (ssize_t
)bytes
)
1623 count
= (ssize_t
)bytes
;
1625 memcpy(buf
, fp
->ptr
,(size_t) count
);
1629 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1632 * Update the counts for the last read...
1635 bytes
-= (size_t)count
;
1636 total
+= (size_t)count
;
1641 * Return the total number of bytes read...
1644 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1646 return ((ssize_t
)total
);
1651 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1654 * @since CUPS 1.2/OS X 10.5@
1657 off_t
/* O - New file position or -1 on error */
1658 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1661 * Range check input...
1664 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1665 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1667 if (!fp
|| fp
->mode
!= 'r')
1671 * Handle special cases...
1674 if (fp
->bufpos
== 0)
1677 * No seeking necessary...
1688 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1694 * Otherwise, seek in the file and cleanup any compression buffers...
1700 inflateEnd(&fp
->stream
);
1703 #endif /* HAVE_LIBZ */
1705 if (lseek(fp
->fd
, 0, SEEK_SET
))
1707 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1717 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1724 * 'cupsFileSeek()' - Seek in a file.
1726 * @since CUPS 1.2/OS X 10.5@
1729 off_t
/* O - New file position or -1 on error */
1730 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1731 off_t pos
) /* I - Position in file */
1733 ssize_t bytes
; /* Number bytes in buffer */
1736 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1738 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1739 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1742 * Range check input...
1745 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1749 * Handle special cases...
1753 return (cupsFileRewind(fp
));
1757 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1759 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1761 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1764 * No seeking necessary...
1768 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1776 if (!fp
->compressed
&& !fp
->ptr
)
1779 * Preload a buffer to determine whether the file is compressed...
1782 if (cups_fill(fp
) < 0)
1785 #endif /* HAVE_LIBZ */
1788 * Seek forwards or backwards...
1793 if (pos
< fp
->bufpos
)
1796 * Need to seek backwards...
1799 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1804 inflateEnd(&fp
->stream
);
1806 lseek(fp
->fd
, 0, SEEK_SET
);
1812 while ((bytes
= cups_fill(fp
)) > 0)
1813 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1819 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1823 #endif /* HAVE_LIBZ */
1825 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1826 fp
->pos
= fp
->bufpos
;
1830 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1831 CUPS_LLCAST fp
->pos
));
1837 * Need to seek forwards...
1840 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1845 while ((bytes
= cups_fill(fp
)) > 0)
1847 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1854 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1858 #endif /* HAVE_LIBZ */
1860 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1861 fp
->pos
= fp
->bufpos
;
1865 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1866 CUPS_LLCAST fp
->pos
));
1870 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1877 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1879 * @since CUPS 1.2/OS X 10.5@
1882 cups_file_t
* /* O - CUPS file */
1883 cupsFileStderr(void)
1885 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1889 * Open file descriptor 2 as needed...
1892 if (!cg
->stdio_files
[2])
1895 * Flush any pending output on the stdio file...
1901 * Open file descriptor 2...
1904 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1905 cg
->stdio_files
[2]->is_stdio
= 1;
1908 return (cg
->stdio_files
[2]);
1913 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1915 * @since CUPS 1.2/OS X 10.5@
1918 cups_file_t
* /* O - CUPS file */
1921 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1925 * Open file descriptor 0 as needed...
1928 if (!cg
->stdio_files
[0])
1931 * Open file descriptor 0...
1934 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1935 cg
->stdio_files
[0]->is_stdio
= 1;
1938 return (cg
->stdio_files
[0]);
1943 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1945 * @since CUPS 1.2/OS X 10.5@
1948 cups_file_t
* /* O - CUPS file */
1949 cupsFileStdout(void)
1951 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1955 * Open file descriptor 1 as needed...
1958 if (!cg
->stdio_files
[1])
1961 * Flush any pending output on the stdio file...
1967 * Open file descriptor 1...
1970 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1971 cg
->stdio_files
[1]->is_stdio
= 1;
1974 return (cg
->stdio_files
[1]);
1979 * 'cupsFileTell()' - Return the current file position.
1981 * @since CUPS 1.2/OS X 10.5@
1984 off_t
/* O - File position */
1985 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1987 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
1988 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
1989 CUPS_LLCAST (fp
? fp
->pos
: -1)));
1991 return (fp
? fp
->pos
: 0);
1996 * 'cupsFileUnlock()' - Unlock access to a file.
1998 * @since CUPS 1.2/OS X 10.5@
2001 int /* O - 0 on success, -1 on error */
2002 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2008 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2010 if (!fp
|| fp
->mode
== 's')
2018 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2020 return (lockf(fp
->fd
, F_ULOCK
, 0));
2026 * 'cupsFileWrite()' - Write to a file.
2028 * @since CUPS 1.2/OS X 10.5@
2031 ssize_t
/* O - Number of bytes written or -1 on error */
2032 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2033 const char *buf
, /* I - Buffer */
2034 size_t bytes
) /* I - Number of bytes to write */
2037 * Range check input...
2040 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2041 fp
, buf
, CUPS_LLCAST bytes
));
2043 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2050 * Write the buffer...
2053 if (fp
->mode
== 's')
2055 if (cups_write(fp
, buf
, bytes
) < 0)
2058 fp
->pos
+= (off_t
)bytes
;
2060 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2062 return ((ssize_t
)bytes
);
2065 if ((fp
->ptr
+ bytes
) > fp
->end
)
2066 if (cupsFileFlush(fp
))
2069 fp
->pos
+= (off_t
)bytes
;
2071 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2073 if (bytes
> sizeof(fp
->buf
))
2077 return (cups_compress(fp
, buf
, bytes
));
2079 #endif /* HAVE_LIBZ */
2080 return (cups_write(fp
, buf
, bytes
));
2084 memcpy(fp
->ptr
, buf
, bytes
);
2086 return ((ssize_t
)bytes
);
2093 * 'cups_compress()' - Compress a buffer of data.
2096 static ssize_t
/* O - Number of bytes written or -1 */
2097 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2098 const char *buf
, /* I - Buffer */
2099 size_t bytes
) /* I - Number bytes */
2101 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2102 CUPS_LLCAST bytes
));
2108 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2111 * Deflate the bytes...
2114 fp
->stream
.next_in
= (Bytef
*)buf
;
2115 fp
->stream
.avail_in
= (uInt
)bytes
;
2117 while (fp
->stream
.avail_in
> 0)
2120 * Flush the current buffer...
2123 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2124 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2126 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2128 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2131 fp
->stream
.next_out
= fp
->cbuf
;
2132 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2135 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2138 return ((ssize_t
)bytes
);
2140 #endif /* HAVE_LIBZ */
2144 * 'cups_fill()' - Fill the input buffer.
2147 static ssize_t
/* O - Number of bytes or -1 */
2148 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2150 ssize_t bytes
; /* Number of bytes read */
2152 int status
; /* Decompression status */
2153 const unsigned char *ptr
, /* Pointer into buffer */
2154 *end
; /* End of buffer */
2155 #endif /* HAVE_LIBZ */
2158 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2159 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2160 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2161 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2163 if (fp
->ptr
&& fp
->end
)
2164 fp
->bufpos
+= fp
->end
- fp
->buf
;
2167 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2169 while (!fp
->ptr
|| fp
->compressed
)
2172 * Check to see if we have read any data yet; if not, see if we have a
2173 * compressed file...
2179 * Reset the file position in case we are seeking...
2185 * Read the first bytes in the file to determine if we have a gzip'd
2189 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2192 * Can't read from file!
2195 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2196 CUPS_LLCAST bytes
));
2201 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2202 (fp
->buf
[1] & 255) != 0x8b ||
2203 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2206 * Not a gzip'd file!
2210 fp
->end
= fp
->buf
+ bytes
;
2212 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2213 CUPS_LLCAST bytes
));
2219 * Parse header junk: extra data, original name, and comment...
2222 ptr
= (unsigned char *)fp
->buf
+ 10;
2223 end
= (unsigned char *)fp
->buf
+ bytes
;
2225 if (fp
->buf
[3] & 0x04)
2228 * Skip extra data...
2231 if ((ptr
+ 2) > end
)
2234 * Can't read from file!
2240 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2246 * Can't read from file!
2253 if (fp
->buf
[3] & 0x08)
2256 * Skip original name data...
2259 while (ptr
< end
&& *ptr
)
2267 * Can't read from file!
2274 if (fp
->buf
[3] & 0x10)
2277 * Skip comment data...
2280 while (ptr
< end
&& *ptr
)
2288 * Can't read from file!
2295 if (fp
->buf
[3] & 0x02)
2298 * Skip header CRC data...
2306 * Can't read from file!
2314 * Copy the flate-compressed data to the compression buffer...
2317 if ((bytes
= end
- ptr
) > 0)
2318 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2321 * Setup the decompressor data...
2324 fp
->stream
.zalloc
= (alloc_func
)0;
2325 fp
->stream
.zfree
= (free_func
)0;
2326 fp
->stream
.opaque
= (voidpf
)0;
2327 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2328 fp
->stream
.next_out
= NULL
;
2329 fp
->stream
.avail_in
= (uInt
)bytes
;
2330 fp
->stream
.avail_out
= 0;
2331 fp
->crc
= crc32(0L, Z_NULL
, 0);
2333 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2342 * If we have reached end-of-file, return immediately...
2349 * Fill the decompression buffer as needed...
2352 if (fp
->stream
.avail_in
== 0)
2354 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2357 fp
->stream
.next_in
= fp
->cbuf
;
2358 fp
->stream
.avail_in
= (uInt
)bytes
;
2362 * Decompress data from the buffer...
2365 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2366 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2368 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2370 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2371 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2372 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2374 if (status
== Z_STREAM_END
)
2377 * Read the CRC and length...
2380 unsigned char trailer
[8]; /* Trailer bytes */
2381 uLong tcrc
; /* Trailer CRC */
2384 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < (ssize_t
)sizeof(trailer
))
2387 * Can't get it, so mark end-of-file...
2394 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2395 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2397 if (tcrc
!= fp
->crc
)
2400 * Bad CRC, mark end-of-file...
2403 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x",
2404 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2412 * Otherwise, reset the compressed flag so that we re-read the
2420 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2423 * Return the decompressed data...
2427 fp
->end
= fp
->buf
+ bytes
;
2433 #endif /* HAVE_LIBZ */
2436 * Read a buffer's full of data...
2439 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2442 * Can't read from file!
2453 * Return the bytes we read...
2458 fp
->end
= fp
->buf
+ bytes
;
2465 * 'cups_open()' - Safely open a file for writing.
2467 * We don't allow appending to directories or files that are hard-linked or
2471 static int /* O - File descriptor or -1 otherwise */
2472 cups_open(const char *filename
, /* I - Filename */
2473 int mode
) /* I - Open mode */
2475 int fd
; /* File descriptor */
2476 struct stat fileinfo
; /* File information */
2478 struct stat linkinfo
; /* Link information */
2486 if ((fd
= open(filename
, mode
, 0666)) < 0)
2490 * Then verify that the file descriptor doesn't point to a directory or hard-
2494 if (fstat(fd
, &fileinfo
))
2500 if (fileinfo
.st_nlink
!= 1)
2508 if (fileinfo
.st_mode
& _S_IFDIR
)
2510 if (S_ISDIR(fileinfo
.st_mode
))
2520 * Then use lstat to determine whether the filename is a symlink...
2523 if (lstat(filename
, &linkinfo
))
2529 if (S_ISLNK(linkinfo
.st_mode
) ||
2530 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2531 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2533 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2534 #endif /* HAVE_ST_GEN */
2535 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2536 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2553 * 'cups_read()' - Read from a file descriptor.
2556 static ssize_t
/* O - Number of bytes read or -1 */
2557 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2558 char *buf
, /* I - Buffer */
2559 size_t bytes
) /* I - Number bytes */
2561 ssize_t total
; /* Total bytes read */
2564 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2565 CUPS_LLCAST bytes
));
2568 * Loop until we read at least 0 bytes...
2574 if (fp
->mode
== 's')
2575 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2577 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2579 if (fp
->mode
== 's')
2580 total
= recv(fp
->fd
, buf
, bytes
, 0);
2582 total
= read(fp
->fd
, buf
, bytes
);
2585 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2591 * Reads can be interrupted by signals and unavailable resources...
2594 if (errno
== EAGAIN
|| errno
== EINTR
)
2601 * Return the total number of bytes read...
2609 * 'cups_write()' - Write to a file descriptor.
2612 static ssize_t
/* O - Number of bytes written or -1 */
2613 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2614 const char *buf
, /* I - Buffer */
2615 size_t bytes
) /* I - Number bytes */
2617 size_t total
; /* Total bytes written */
2618 ssize_t count
; /* Count this time */
2621 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2622 CUPS_LLCAST bytes
));
2625 * Loop until all bytes are written...
2632 if (fp
->mode
== 's')
2633 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2635 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2637 if (fp
->mode
== 's')
2638 count
= send(fp
->fd
, buf
, bytes
, 0);
2640 count
= write(fp
->fd
, buf
, bytes
);
2643 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2648 * Writes can be interrupted by signals and unavailable resources...
2651 if (errno
== EAGAIN
|| errno
== EINTR
)
2658 * Update the counts for the last write call...
2661 bytes
-= (size_t)count
;
2662 total
+= (size_t)count
;
2667 * Return the total number of bytes written...
2670 return ((ssize_t
)total
);
2675 * End of "$Id: file.c 12297 2014-12-08 19:26:32Z msweet $".