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-2013 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/".
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/OS X 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 */
324 int is_stdio
; /* Is a stdio file? */
327 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
337 * Flush pending write data...
341 status
= cupsFileFlush(fp
);
346 if (fp
->compressed
&& status
>= 0)
351 * Free decompression data...
354 inflateEnd(&fp
->stream
);
359 * Flush any remaining compressed data...
362 unsigned char trailer
[8]; /* Trailer CRC and length */
363 int done
; /* Done writing... */
366 fp
->stream
.avail_in
= 0;
370 if (fp
->stream
.next_out
> fp
->cbuf
)
372 if (cups_write(fp
, (char *)fp
->cbuf
,
373 fp
->stream
.next_out
- fp
->cbuf
) < 0)
376 fp
->stream
.next_out
= fp
->cbuf
;
377 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
380 if (done
|| status
< 0)
383 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
384 fp
->stream
.next_out
== fp
->cbuf
;
388 * Write the CRC and length...
391 trailer
[0] = fp
->crc
;
392 trailer
[1] = fp
->crc
>> 8;
393 trailer
[2] = fp
->crc
>> 16;
394 trailer
[3] = fp
->crc
>> 24;
395 trailer
[4] = fp
->pos
;
396 trailer
[5] = fp
->pos
>> 8;
397 trailer
[6] = fp
->pos
>> 16;
398 trailer
[7] = fp
->pos
>> 24;
400 if (cups_write(fp
, (char *)trailer
, 8) < 0)
404 * Free all memory used by the compression stream...
407 deflateEnd(&(fp
->stream
));
410 #endif /* HAVE_LIBZ */
413 * Save the file descriptor we used and free memory...
418 is_stdio
= fp
->is_stdio
;
420 if (fp
->printf_buffer
)
421 free(fp
->printf_buffer
);
426 * Close the file, returning the close status...
431 if (httpAddrClose(NULL
, fd
) < 0)
445 * 'cupsFileCompression()' - Return whether a file is compressed.
447 * @since CUPS 1.2/OS X 10.5@
450 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
451 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
453 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
458 * 'cupsFileEOF()' - Return the end-of-file status.
460 * @since CUPS 1.2/OS X 10.5@
463 int /* O - 1 on end of file, 0 otherwise */
464 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
466 return (fp
? fp
->eof
: 1);
471 * 'cupsFileFind()' - Find a file using the specified path.
473 * This function allows the paths in the path string to be separated by
474 * colons (UNIX standard) or semicolons (Windows standard) and stores the
475 * result in the buffer supplied. If the file cannot be found in any of
476 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
477 * matches the current directory.
479 * @since CUPS 1.2/OS X 10.5@
482 const char * /* O - Full path to file or @code NULL@ if not found */
483 cupsFileFind(const char *filename
, /* I - File to find */
484 const char *path
, /* I - Colon/semicolon-separated path */
485 int executable
, /* I - 1 = executable files, 0 = any file/dir */
486 char *buffer
, /* I - Filename buffer */
487 int bufsize
) /* I - Size of filename buffer */
489 char *bufptr
, /* Current position in buffer */
490 *bufend
; /* End of buffer */
494 * Range check input...
497 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
498 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
501 if (!filename
|| !buffer
|| bufsize
< 2)
507 * No path, so check current directory...
510 if (!access(filename
, 0))
512 strlcpy(buffer
, filename
, 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
, 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
, 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)", 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
, bytes
);
614 #endif /* HAVE_LIBZ */
615 bytes
= cups_write(fp
, fp
->buf
, 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 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
642 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
647 * If the input buffer is empty, try to read more data...
650 if (fp
->ptr
>= fp
->end
)
651 if (cups_fill(fp
) < 0)
653 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
658 * Return the next character in the buffer...
661 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
665 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
667 return (*(fp
->ptr
)++ & 255);
672 * 'cupsFileGetConf()' - Get a line from a configuration file.
674 * @since CUPS 1.2/OS X 10.5@
677 char * /* O - Line read or @code NULL@ on end of file or error */
678 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
679 char *buf
, /* O - String buffer */
680 size_t buflen
, /* I - Size of string buffer */
681 char **value
, /* O - Pointer to value */
682 int *linenum
) /* IO - Current line number */
684 char *ptr
; /* Pointer into line */
688 * Range check input...
691 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
692 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
695 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
696 !buf
|| buflen
< 2 || !value
)
705 * Read the next non-comment line...
710 while (cupsFileGets(fp
, buf
, buflen
))
715 * Strip any comments...
718 if ((ptr
= strchr(buf
, '#')) != NULL
)
720 if (ptr
> buf
&& ptr
[-1] == '\\')
723 _cups_strcpy(ptr
- 1, ptr
);
727 // Strip the comment and any trailing whitespace...
730 if (!_cups_isspace(ptr
[-1]))
741 * Strip leading whitespace...
744 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
747 _cups_strcpy(buf
, ptr
);
750 * See if there is anything left...
756 * Yes, grab any value and return...
759 for (ptr
= buf
; *ptr
; ptr
++)
760 if (_cups_isspace(*ptr
))
766 * Have a value, skip any other spaces...
769 while (_cups_isspace(*ptr
))
776 * Strip trailing whitespace and > for lines that begin with <...
779 ptr
+= strlen(ptr
) - 1;
781 if (buf
[0] == '<' && *ptr
== '>')
783 else if (buf
[0] == '<' && *ptr
!= '>')
793 while (ptr
> *value
&& _cups_isspace(*ptr
))
810 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
811 * contain binary data.
813 * This function differs from @link cupsFileGets@ in that the trailing CR
814 * and LF are preserved, as is any binary data on the line. The buffer is
815 * nul-terminated, however you should use the returned length to determine
816 * the number of bytes on the line.
818 * @since CUPS 1.2/OS X 10.5@
821 size_t /* O - Number of bytes on line or 0 on end of file */
822 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
823 char *buf
, /* I - Buffer */
824 size_t buflen
) /* I - Size of buffer */
826 int ch
; /* Character from file */
827 char *ptr
, /* Current position in line buffer */
828 *end
; /* End of line buffer */
832 * Range check input...
835 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
836 fp
, buf
, CUPS_LLCAST buflen
));
838 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
842 * Now loop until we have a valid line...
845 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
847 if (fp
->ptr
>= fp
->end
)
848 if (cups_fill(fp
) <= 0)
851 *ptr
++ = ch
= *(fp
->ptr
)++;
860 if (fp
->ptr
>= fp
->end
)
861 if (cups_fill(fp
) <= 0)
864 if (*(fp
->ptr
) == '\n')
866 *ptr
++ = *(fp
->ptr
)++;
875 * Line feed ends a line...
884 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
891 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
893 * @since CUPS 1.2/OS X 10.5@
896 char * /* O - Line read or @code NULL@ on end of file or error */
897 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
898 char *buf
, /* O - String buffer */
899 size_t buflen
) /* I - Size of string buffer */
901 int ch
; /* Character from file */
902 char *ptr
, /* Current position in line buffer */
903 *end
; /* End of line buffer */
907 * Range check input...
910 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
911 CUPS_LLCAST buflen
));
913 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
917 * Now loop until we have a valid line...
920 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
922 if (fp
->ptr
>= fp
->end
)
923 if (cups_fill(fp
) <= 0)
940 if (fp
->ptr
>= fp
->end
)
941 if (cups_fill(fp
) <= 0)
944 if (*(fp
->ptr
) == '\n')
955 * Line feed ends a line...
966 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
973 * 'cupsFileLock()' - Temporarily lock access to a file.
975 * @since CUPS 1.2/OS X 10.5@
978 int /* O - 0 on success, -1 on error */
979 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
980 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
986 if (!fp
|| fp
->mode
== 's')
994 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
996 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1002 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1004 * @since CUPS 1.2/OS X 10.5@
1007 int /* O - File descriptor */
1008 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1018 * 'cupsFileOpen()' - Open a CUPS file.
1020 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1021 * existing file, "a" to append to an existing file or create a new file,
1022 * or "s" to open a socket connection.
1024 * When opening for writing ("w"), an optional number from 1 to 9 can be
1025 * supplied which enables Flate compression of the file. Compression is
1026 * not supported for the "a" (append) mode.
1028 * When opening a socket connection, the filename is a string of the form
1029 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1030 * connection as needed, generally preferring IPv6 connections when there is
1033 * @since CUPS 1.2/OS X 10.5@
1036 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1037 cupsFileOpen(const char *filename
, /* I - Name of file */
1038 const char *mode
) /* I - Open mode */
1040 cups_file_t
*fp
; /* New CUPS file */
1041 int fd
; /* File descriptor */
1042 char hostname
[1024], /* Hostname */
1043 *portname
; /* Port "name" (number or service) */
1044 http_addrlist_t
*addrlist
; /* Host address list */
1047 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1051 * Range check input...
1054 if (!filename
|| !mode
||
1055 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1056 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1065 case 'a' : /* Append file */
1066 fd
= cups_open(filename
,
1067 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1070 case 'r' : /* Read file */
1071 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1074 case 'w' : /* Write file */
1075 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1076 if (fd
< 0 && errno
== ENOENT
)
1078 fd
= cups_open(filename
,
1079 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1080 if (fd
< 0 && errno
== EEXIST
)
1081 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1092 case 's' : /* Read/write socket */
1093 strlcpy(hostname
, filename
, sizeof(hostname
));
1094 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1100 * Lookup the hostname and service...
1103 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1107 * Connect to the server...
1110 if (!httpAddrConnect(addrlist
, &fd
))
1112 httpAddrFreeList(addrlist
);
1116 httpAddrFreeList(addrlist
);
1119 default : /* Remove bogus compiler warning... */
1127 * Create the CUPS file structure...
1130 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1133 httpAddrClose(NULL
, fd
);
1146 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1148 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1149 * or "s" to treat the file descriptor as a bidirectional socket connection.
1151 * When opening for writing ("w"), an optional number from 1 to 9 can be
1152 * supplied which enables Flate compression of the file. Compression is
1153 * not supported for the "a" (append) mode.
1155 * @since CUPS 1.2/OS X 10.5@
1158 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1159 cupsFileOpenFd(int fd
, /* I - File descriptor */
1160 const char *mode
) /* I - Open mode */
1162 cups_file_t
*fp
; /* New CUPS file */
1165 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1168 * Range check input...
1171 if (fd
< 0 || !mode
||
1172 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1173 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1177 * Allocate memory...
1180 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1192 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1197 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1200 if (mode
[1] >= '1' && mode
[1] <= '9')
1203 * Open a compressed stream, so write the standard gzip file
1207 unsigned char header
[10]; /* gzip file header */
1208 time_t curtime
; /* Current time */
1211 curtime
= time(NULL
);
1214 header
[2] = Z_DEFLATED
;
1216 header
[4] = curtime
;
1217 header
[5] = curtime
>> 8;
1218 header
[6] = curtime
>> 16;
1219 header
[7] = curtime
>> 24;
1223 cups_write(fp
, (char *)header
, 10);
1226 * Initialize the compressor...
1229 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1230 Z_DEFAULT_STRATEGY
);
1232 fp
->stream
.next_out
= fp
->cbuf
;
1233 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1235 fp
->crc
= crc32(0L, Z_NULL
, 0);
1237 #endif /* HAVE_LIBZ */
1248 default : /* Remove bogus compiler warning... */
1253 * Don't pass this file to child processes...
1257 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1265 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1267 * @since CUPS 1.2/OS X 10.5@
1270 int /* O - Character or -1 on end of file */
1271 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1274 * Range check input...
1277 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1281 * If the input buffer is empty, try to read more data...
1284 if (fp
->ptr
>= fp
->end
)
1285 if (cups_fill(fp
) < 0)
1289 * Return the next character in the buffer...
1292 return (*(fp
->ptr
) & 255);
1297 * 'cupsFilePrintf()' - Write a formatted string.
1299 * @since CUPS 1.2/OS X 10.5@
1302 int /* O - Number of bytes written or -1 on error */
1303 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1304 const char *format
, /* I - Printf-style format string */
1305 ...) /* I - Additional args as necessary */
1307 va_list ap
; /* Argument list */
1308 ssize_t bytes
; /* Formatted size */
1311 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1313 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1316 if (!fp
->printf_buffer
)
1319 * Start with an 1k printf buffer...
1322 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1325 fp
->printf_size
= 1024;
1328 va_start(ap
, format
);
1329 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1332 if (bytes
>= (ssize_t
)fp
->printf_size
)
1335 * Expand the printf buffer...
1338 char *temp
; /* Temporary buffer pointer */
1344 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1347 fp
->printf_buffer
= temp
;
1348 fp
->printf_size
= bytes
+ 1;
1350 va_start(ap
, format
);
1351 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1355 if (fp
->mode
== 's')
1357 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1362 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1367 if ((fp
->ptr
+ bytes
) > fp
->end
)
1368 if (cupsFileFlush(fp
))
1373 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1375 if (bytes
> sizeof(fp
->buf
))
1379 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1381 #endif /* HAVE_LIBZ */
1382 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1386 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1394 * 'cupsFilePutChar()' - Write a character.
1396 * @since CUPS 1.2/OS X 10.5@
1399 int /* O - 0 on success, -1 on error */
1400 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1401 int c
) /* I - Character to write */
1404 * Range check input...
1407 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1410 if (fp
->mode
== 's')
1413 * Send character immediately over socket...
1416 char ch
; /* Output character */
1421 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1430 if (fp
->ptr
>= fp
->end
)
1431 if (cupsFileFlush(fp
))
1439 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1446 * 'cupsFilePutConf()' - Write a configuration line.
1448 * This function handles any comment escaping of the value.
1450 * @since CUPS 1.4/OS X 10.6@
1453 ssize_t
/* O - Number of bytes written or -1 on error */
1454 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1455 const char *directive
, /* I - Directive */
1456 const char *value
) /* I - Value */
1458 ssize_t bytes
, /* Number of bytes written */
1459 temp
; /* Temporary byte count */
1460 const char *ptr
; /* Pointer into value */
1463 if (!fp
|| !directive
|| !*directive
)
1466 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1469 if (cupsFilePutChar(fp
, ' ') < 0)
1473 if (value
&& *value
)
1475 if ((ptr
= strchr(value
, '#')) != NULL
)
1478 * Need to quote the first # in the info string...
1481 if ((temp
= cupsFileWrite(fp
, value
, ptr
- value
)) < 0)
1485 if (cupsFilePutChar(fp
, '\\') < 0)
1489 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1493 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1499 if (cupsFilePutChar(fp
, '\n') < 0)
1507 * 'cupsFilePuts()' - Write a string.
1509 * Like the @code fputs@ function, no newline is appended to the string.
1511 * @since CUPS 1.2/OS X 10.5@
1514 int /* O - Number of bytes written or -1 on error */
1515 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1516 const char *s
) /* I - String to write */
1518 ssize_t bytes
; /* Bytes to write */
1522 * Range check input...
1525 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1529 * Write the string...
1532 bytes
= (int)strlen(s
);
1534 if (fp
->mode
== 's')
1536 if (cups_write(fp
, s
, bytes
) < 0)
1541 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1546 if ((fp
->ptr
+ bytes
) > fp
->end
)
1547 if (cupsFileFlush(fp
))
1552 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1554 if (bytes
> sizeof(fp
->buf
))
1558 return (cups_compress(fp
, s
, bytes
));
1560 #endif /* HAVE_LIBZ */
1561 return (cups_write(fp
, s
, bytes
));
1565 memcpy(fp
->ptr
, s
, bytes
);
1573 * 'cupsFileRead()' - Read from a file.
1575 * @since CUPS 1.2/OS X 10.5@
1578 ssize_t
/* O - Number of bytes read or -1 on error */
1579 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1580 char *buf
, /* O - Buffer */
1581 size_t bytes
) /* I - Number of bytes to read */
1583 size_t total
; /* Total bytes read */
1584 ssize_t count
; /* Bytes read */
1587 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1588 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
, count
);
1627 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1630 * Update the counts for the last read...
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)", 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
")", fp
,
1736 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1737 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1740 * Range check input...
1743 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1747 * Handle special cases...
1751 return (cupsFileRewind(fp
));
1755 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1757 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1759 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1762 * No seeking necessary...
1766 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1774 if (!fp
->compressed
&& !fp
->ptr
)
1777 * Preload a buffer to determine whether the file is compressed...
1780 if (cups_fill(fp
) < 0)
1783 #endif /* HAVE_LIBZ */
1786 * Seek forwards or backwards...
1791 if (pos
< fp
->bufpos
)
1794 * Need to seek backwards...
1797 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1802 inflateEnd(&fp
->stream
);
1804 lseek(fp
->fd
, 0, SEEK_SET
);
1810 while ((bytes
= cups_fill(fp
)) > 0)
1811 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1817 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1821 #endif /* HAVE_LIBZ */
1823 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1824 fp
->pos
= fp
->bufpos
;
1828 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1829 CUPS_LLCAST fp
->pos
));
1835 * Need to seek forwards...
1838 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1843 while ((bytes
= cups_fill(fp
)) > 0)
1845 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1852 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1856 #endif /* HAVE_LIBZ */
1858 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1859 fp
->pos
= fp
->bufpos
;
1863 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1864 CUPS_LLCAST fp
->pos
));
1868 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1875 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1877 * @since CUPS 1.2/OS X 10.5@
1880 cups_file_t
* /* O - CUPS file */
1881 cupsFileStderr(void)
1883 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1887 * Open file descriptor 2 as needed...
1890 if (!cg
->stdio_files
[2])
1893 * Flush any pending output on the stdio file...
1899 * Open file descriptor 2...
1902 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1903 cg
->stdio_files
[2]->is_stdio
= 1;
1906 return (cg
->stdio_files
[2]);
1911 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1913 * @since CUPS 1.2/OS X 10.5@
1916 cups_file_t
* /* O - CUPS file */
1919 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1923 * Open file descriptor 0 as needed...
1926 if (!cg
->stdio_files
[0])
1929 * Open file descriptor 0...
1932 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1933 cg
->stdio_files
[0]->is_stdio
= 1;
1936 return (cg
->stdio_files
[0]);
1941 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1943 * @since CUPS 1.2/OS X 10.5@
1946 cups_file_t
* /* O - CUPS file */
1947 cupsFileStdout(void)
1949 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1953 * Open file descriptor 1 as needed...
1956 if (!cg
->stdio_files
[1])
1959 * Flush any pending output on the stdio file...
1965 * Open file descriptor 1...
1968 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1969 cg
->stdio_files
[1]->is_stdio
= 1;
1972 return (cg
->stdio_files
[1]);
1977 * 'cupsFileTell()' - Return the current file position.
1979 * @since CUPS 1.2/OS X 10.5@
1982 off_t
/* O - File position */
1983 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1985 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
1986 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
1987 CUPS_LLCAST (fp
? fp
->pos
: -1)));
1989 return (fp
? fp
->pos
: 0);
1994 * 'cupsFileUnlock()' - Unlock access to a file.
1996 * @since CUPS 1.2/OS X 10.5@
1999 int /* O - 0 on success, -1 on error */
2000 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2006 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2008 if (!fp
|| fp
->mode
== 's')
2016 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2018 return (lockf(fp
->fd
, F_ULOCK
, 0));
2024 * 'cupsFileWrite()' - Write to a file.
2026 * @since CUPS 1.2/OS X 10.5@
2029 ssize_t
/* O - Number of bytes written or -1 on error */
2030 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2031 const char *buf
, /* I - Buffer */
2032 size_t bytes
) /* I - Number of bytes to write */
2035 * Range check input...
2038 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2039 fp
, buf
, CUPS_LLCAST bytes
));
2041 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2048 * Write the buffer...
2051 if (fp
->mode
== 's')
2053 if (cups_write(fp
, buf
, bytes
) < 0)
2056 fp
->pos
+= (off_t
)bytes
;
2058 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2060 return ((ssize_t
)bytes
);
2063 if ((fp
->ptr
+ bytes
) > fp
->end
)
2064 if (cupsFileFlush(fp
))
2067 fp
->pos
+= (off_t
)bytes
;
2069 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2071 if (bytes
> sizeof(fp
->buf
))
2075 return (cups_compress(fp
, buf
, bytes
));
2077 #endif /* HAVE_LIBZ */
2078 return (cups_write(fp
, buf
, bytes
));
2082 memcpy(fp
->ptr
, buf
, bytes
);
2084 return ((ssize_t
)bytes
);
2091 * 'cups_compress()' - Compress a buffer of data.
2094 static ssize_t
/* O - Number of bytes written or -1 */
2095 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2096 const char *buf
, /* I - Buffer */
2097 size_t bytes
) /* I - Number bytes */
2099 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2100 CUPS_LLCAST bytes
));
2106 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
2109 * Deflate the bytes...
2112 fp
->stream
.next_in
= (Bytef
*)buf
;
2113 fp
->stream
.avail_in
= bytes
;
2115 while (fp
->stream
.avail_in
> 0)
2118 * Flush the current buffer...
2121 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2122 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2124 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
2126 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
2129 fp
->stream
.next_out
= fp
->cbuf
;
2130 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2133 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2138 #endif /* HAVE_LIBZ */
2142 * 'cups_fill()' - Fill the input buffer.
2145 static ssize_t
/* O - Number of bytes or -1 */
2146 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2148 ssize_t bytes
; /* Number of bytes read */
2150 int status
; /* Decompression status */
2151 const unsigned char *ptr
, /* Pointer into buffer */
2152 *end
; /* End of buffer */
2153 #endif /* HAVE_LIBZ */
2156 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2157 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2158 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2159 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2161 if (fp
->ptr
&& fp
->end
)
2162 fp
->bufpos
+= fp
->end
- fp
->buf
;
2165 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2167 while (!fp
->ptr
|| fp
->compressed
)
2170 * Check to see if we have read any data yet; if not, see if we have a
2171 * compressed file...
2177 * Reset the file position in case we are seeking...
2183 * Read the first bytes in the file to determine if we have a gzip'd
2187 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2190 * Can't read from file!
2193 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2194 CUPS_LLCAST bytes
));
2199 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2200 (fp
->buf
[1] & 255) != 0x8b ||
2201 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2204 * Not a gzip'd file!
2208 fp
->end
= fp
->buf
+ bytes
;
2210 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2211 CUPS_LLCAST bytes
));
2217 * Parse header junk: extra data, original name, and comment...
2220 ptr
= (unsigned char *)fp
->buf
+ 10;
2221 end
= (unsigned char *)fp
->buf
+ bytes
;
2223 if (fp
->buf
[3] & 0x04)
2226 * Skip extra data...
2229 if ((ptr
+ 2) > end
)
2232 * Can't read from file!
2238 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2244 * Can't read from file!
2251 if (fp
->buf
[3] & 0x08)
2254 * Skip original name data...
2257 while (ptr
< end
&& *ptr
)
2265 * Can't read from file!
2272 if (fp
->buf
[3] & 0x10)
2275 * Skip comment data...
2278 while (ptr
< end
&& *ptr
)
2286 * Can't read from file!
2293 if (fp
->buf
[3] & 0x02)
2296 * Skip header CRC data...
2304 * Can't read from file!
2312 * Copy the flate-compressed data to the compression buffer...
2315 if ((bytes
= end
- ptr
) > 0)
2316 memcpy(fp
->cbuf
, ptr
, bytes
);
2319 * Setup the decompressor data...
2322 fp
->stream
.zalloc
= (alloc_func
)0;
2323 fp
->stream
.zfree
= (free_func
)0;
2324 fp
->stream
.opaque
= (voidpf
)0;
2325 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2326 fp
->stream
.next_out
= NULL
;
2327 fp
->stream
.avail_in
= bytes
;
2328 fp
->stream
.avail_out
= 0;
2329 fp
->crc
= crc32(0L, Z_NULL
, 0);
2331 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2340 * If we have reached end-of-file, return immediately...
2347 * Fill the decompression buffer as needed...
2350 if (fp
->stream
.avail_in
== 0)
2352 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2355 fp
->stream
.next_in
= fp
->cbuf
;
2356 fp
->stream
.avail_in
= bytes
;
2360 * Decompress data from the buffer...
2363 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2364 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2366 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2368 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2369 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2370 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2372 if (status
== Z_STREAM_END
)
2375 * Read the CRC and length...
2378 unsigned char trailer
[8]; /* Trailer bytes */
2379 uLong tcrc
; /* Trailer CRC */
2382 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2385 * Can't get it, so mark end-of-file...
2392 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2393 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2395 if (tcrc
!= fp
->crc
)
2398 * Bad CRC, mark end-of-file...
2401 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x",
2402 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2410 * Otherwise, reset the compressed flag so that we re-read the
2418 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2421 * Return the decompressed data...
2425 fp
->end
= fp
->buf
+ bytes
;
2431 #endif /* HAVE_LIBZ */
2434 * Read a buffer's full of data...
2437 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2440 * Can't read from file!
2451 * Return the bytes we read...
2456 fp
->end
= fp
->buf
+ bytes
;
2463 * 'cups_open()' - Safely open a file for writing.
2465 * We don't allow appending to directories or files that are hard-linked or
2469 static int /* O - File descriptor or -1 otherwise */
2470 cups_open(const char *filename
, /* I - Filename */
2471 int mode
) /* I - Open mode */
2473 int fd
; /* File descriptor */
2474 struct stat fileinfo
; /* File information */
2476 struct stat linkinfo
; /* Link information */
2484 if ((fd
= open(filename
, mode
, 0666)) < 0)
2488 * Then verify that the file descriptor doesn't point to a directory or hard-
2492 if (fstat(fd
, &fileinfo
))
2498 if (fileinfo
.st_nlink
!= 1)
2506 if (fileinfo
.st_mode
& _S_IFDIR
)
2508 if (S_ISDIR(fileinfo
.st_mode
))
2518 * Then use lstat to determine whether the filename is a symlink...
2521 if (lstat(filename
, &linkinfo
))
2527 if (S_ISLNK(linkinfo
.st_mode
) ||
2528 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2529 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2531 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2532 #endif /* HAVE_ST_GEN */
2533 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2534 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2551 * 'cups_read()' - Read from a file descriptor.
2554 static ssize_t
/* O - Number of bytes read or -1 */
2555 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2556 char *buf
, /* I - Buffer */
2557 size_t bytes
) /* I - Number bytes */
2559 ssize_t total
; /* Total bytes read */
2562 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2563 CUPS_LLCAST bytes
));
2566 * Loop until we read at least 0 bytes...
2572 if (fp
->mode
== 's')
2573 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2575 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2577 if (fp
->mode
== 's')
2578 total
= recv(fp
->fd
, buf
, bytes
, 0);
2580 total
= read(fp
->fd
, buf
, bytes
);
2583 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2589 * Reads can be interrupted by signals and unavailable resources...
2592 if (errno
== EAGAIN
|| errno
== EINTR
)
2599 * Return the total number of bytes read...
2607 * 'cups_write()' - Write to a file descriptor.
2610 static ssize_t
/* O - Number of bytes written or -1 */
2611 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2612 const char *buf
, /* I - Buffer */
2613 size_t bytes
) /* I - Number bytes */
2615 size_t total
; /* Total bytes written */
2616 ssize_t count
; /* Count this time */
2619 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2620 CUPS_LLCAST bytes
));
2623 * Loop until all bytes are written...
2630 if (fp
->mode
== 's')
2631 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2633 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2635 if (fp
->mode
== 's')
2636 count
= send(fp
->fd
, buf
, bytes
, 0);
2638 count
= write(fp
->fd
, buf
, bytes
);
2641 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2646 * Writes can be interrupted by signals and unavailable resources...
2649 if (errno
== EAGAIN
|| errno
== EINTR
)
2656 * Update the counts for the last write call...
2665 * Return the total number of bytes written...
2668 return ((ssize_t
)total
);