2 * "$Id: file.c 7672 2008-06-18 22:03:02Z mike $"
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-2011 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 * _cupsFileCheck() - Check the permissions of the given filename.
23 * _cupsFileCheckFilter() - Report file check results as CUPS filter messages.
24 * cupsFileClose() - Close a CUPS file.
25 * cupsFileCompression() - Return whether a file is compressed.
26 * cupsFileEOF() - Return the end-of-file status.
27 * cupsFileFind() - Find a file using the specified path.
28 * cupsFileFlush() - Flush pending output.
29 * cupsFileGetChar() - Get a single character from a file.
30 * cupsFileGetConf() - Get a line from a configuration file.
31 * cupsFileGetLine() - Get a CR and/or LF-terminated line that may
32 * contain binary data.
33 * cupsFileGets() - Get a CR and/or LF-terminated line.
34 * cupsFileLock() - Temporarily lock access to a file.
35 * cupsFileNumber() - Return the file descriptor associated with a CUPS
37 * cupsFileOpen() - Open a CUPS file.
38 * cupsFileOpenFd() - Open a CUPS file using a file descriptor.
39 * cupsFilePeekChar() - Peek at the next character from a file.
40 * cupsFilePrintf() - Write a formatted string.
41 * cupsFilePutChar() - Write a character.
42 * cupsFilePuts() - Write a string.
43 * cupsFileRead() - Read from a file.
44 * cupsFileRewind() - Set the current file position to the beginning of
46 * cupsFileSeek() - Seek in a file.
47 * cupsFileStderr() - Return a CUPS file associated with stderr.
48 * cupsFileStdin() - Return a CUPS file associated with stdin.
49 * cupsFileStdout() - Return a CUPS file associated with stdout.
50 * cupsFileTell() - Return the current file position.
51 * cupsFileUnlock() - Unlock access to a file.
52 * cupsFileWrite() - Write to a file.
53 * cups_compress() - Compress a buffer of data.
54 * cups_fill() - Fill the input buffer.
55 * cups_read() - Read from a file descriptor.
56 * cups_write() - Write to a file descriptor.
60 * Include necessary headers...
63 #include "file-private.h"
65 #include <sys/types.h>
73 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
74 #endif /* HAVE_LIBZ */
75 static ssize_t
cups_fill(cups_file_t
*fp
);
76 static int cups_open(const char *filename
, int mode
);
77 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
78 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
82 * '_cupsFileCheck()' - Check the permissions of the given filename.
85 _cups_fc_result_t
/* O - Check result */
87 const char *filename
, /* I - Filename to check */
88 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
89 int dorootchecks
, /* I - Check for root permissions? */
90 _cups_fc_func_t cb
, /* I - Callback function */
91 void *context
) /* I - Context pointer for callback */
94 struct stat fileinfo
; /* File information */
95 char message
[1024], /* Message string */
96 temp
[1024], /* Parent directory filename */
97 *ptr
; /* Pointer into parent directory */
98 _cups_fc_result_t result
; /* Check result */
102 * Does the filename contain a relative path ("../")?
105 if (strstr(filename
, "../"))
111 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
116 * Does the program even exist and is it accessible?
119 if (stat(filename
, &fileinfo
))
125 result
= _CUPS_FILE_CHECK_MISSING
;
130 * Check the execute bit...
133 result
= _CUPS_FILE_CHECK_OK
;
137 case _CUPS_FILE_CHECK_DIRECTORY
:
138 if (!S_ISDIR(fileinfo
.st_mode
))
139 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
143 if (!S_ISREG(fileinfo
.st_mode
))
144 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
152 * Are we doing root checks?
158 * Nope, so anything (else) goes...
165 * Verify permission of the file itself:
167 * 1. Must be owned by root
168 * 2. Must not be writable by group unless group is root/wheel/admin
169 * 3. Must not be setuid
170 * 4. Must not be writable by others
173 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
175 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
&&
176 fileinfo
.st_gid
!= 80) || /* 2. Must not be writable by group */
178 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
) ||
179 /* 2. Must not be writable by group */
180 #endif /* __APPLE__ */
181 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
182 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
184 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
188 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
189 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
193 * Now check the containing directory...
196 strlcpy(temp
, filename
, sizeof(temp
));
197 if ((ptr
= strrchr(temp
, '/')) != NULL
)
205 if (stat(temp
, &fileinfo
))
211 result
= _CUPS_FILE_CHECK_MISSING
;
212 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
218 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
220 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
&&
221 fileinfo
.st_gid
!= 80) || /* 2. Must not be writable by group */
223 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
) ||
224 /* 2. Must not be writable by group */
225 #endif /* __APPLE__ */
226 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
227 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
229 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
230 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
235 * Common return point...
242 cups_lang_t
*lang
= cupsLangDefault();
243 /* Localization information */
247 case _CUPS_FILE_CHECK_OK
:
248 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
249 snprintf(message
, sizeof(message
),
250 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
251 "(0%o/uid=%d/gid=%d).")),
252 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
253 (int)fileinfo
.st_gid
);
255 snprintf(message
, sizeof(message
),
256 _cupsLangString(lang
, _("File \"%s\" permissions OK "
257 "(0%o/uid=%d/gid=%d).")),
258 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
259 (int)fileinfo
.st_gid
);
262 case _CUPS_FILE_CHECK_MISSING
:
263 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
264 snprintf(message
, sizeof(message
),
265 _cupsLangString(lang
, _("Directory \"%s\" not available: "
267 filename
, strerror(errno
));
269 snprintf(message
, sizeof(message
),
270 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
271 filename
, strerror(errno
));
274 case _CUPS_FILE_CHECK_PERMISSIONS
:
275 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
276 snprintf(message
, sizeof(message
),
277 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
279 "(0%o/uid=%d/gid=%d).")),
280 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
281 (int)fileinfo
.st_gid
);
283 snprintf(message
, sizeof(message
),
284 _cupsLangString(lang
, _("File \"%s\" has insecure "
286 "(0%o/uid=%d/gid=%d).")),
287 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
288 (int)fileinfo
.st_gid
);
291 case _CUPS_FILE_CHECK_WRONG_TYPE
:
292 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
293 snprintf(message
, sizeof(message
),
294 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
297 snprintf(message
, sizeof(message
),
298 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
302 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
303 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
304 snprintf(message
, sizeof(message
),
305 _cupsLangString(lang
, _("Directory \"%s\" contains a "
306 "relative path.")), filename
);
308 snprintf(message
, sizeof(message
),
309 _cupsLangString(lang
, _("File \"%s\" contains a relative "
310 "path.")), filename
);
314 (*cb
)(context
, result
, message
);
322 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
326 _cupsFileCheckFilter(
327 void *context
, /* I - Context pointer (unused) */
328 _cups_fc_result_t result
, /* I - Result code */
329 const char *message
) /* I - Message text */
331 const char *prefix
; /* Messaging prefix */
339 case _CUPS_FILE_CHECK_OK
:
343 case _CUPS_FILE_CHECK_MISSING
:
344 case _CUPS_FILE_CHECK_WRONG_TYPE
:
346 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
349 case _CUPS_FILE_CHECK_PERMISSIONS
:
350 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
352 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
356 fprintf(stderr
, "%s: %s\n", prefix
, message
);
361 * 'cupsFileClose()' - Close a CUPS file.
363 * @since CUPS 1.2/Mac OS X 10.5@
366 int /* O - 0 on success, -1 on error */
367 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
369 int fd
; /* File descriptor */
370 char mode
; /* Open mode */
371 int status
; /* Return status */
372 int is_stdio
; /* Is a stdio file? */
375 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
385 * Flush pending write data...
389 status
= cupsFileFlush(fp
);
394 if (fp
->compressed
&& status
>= 0)
399 * Free decompression data...
402 inflateEnd(&fp
->stream
);
407 * Flush any remaining compressed data...
410 unsigned char trailer
[8]; /* Trailer CRC and length */
411 int done
; /* Done writing... */
414 fp
->stream
.avail_in
= 0;
418 if (fp
->stream
.next_out
> fp
->cbuf
)
420 if (cups_write(fp
, (char *)fp
->cbuf
,
421 fp
->stream
.next_out
- fp
->cbuf
) < 0)
424 fp
->stream
.next_out
= fp
->cbuf
;
425 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
428 if (done
|| status
< 0)
431 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
432 fp
->stream
.next_out
== fp
->cbuf
;
436 * Write the CRC and length...
439 trailer
[0] = fp
->crc
;
440 trailer
[1] = fp
->crc
>> 8;
441 trailer
[2] = fp
->crc
>> 16;
442 trailer
[3] = fp
->crc
>> 24;
443 trailer
[4] = fp
->pos
;
444 trailer
[5] = fp
->pos
>> 8;
445 trailer
[6] = fp
->pos
>> 16;
446 trailer
[7] = fp
->pos
>> 24;
448 if (cups_write(fp
, (char *)trailer
, 8) < 0)
452 * Free all memory used by the compression stream...
455 deflateEnd(&(fp
->stream
));
458 #endif /* HAVE_LIBZ */
461 * Save the file descriptor we used and free memory...
466 is_stdio
= fp
->is_stdio
;
468 if (fp
->printf_buffer
)
469 free(fp
->printf_buffer
);
474 * Close the file, returning the close status...
479 if (closesocket(fd
) < 0)
493 * 'cupsFileCompression()' - Return whether a file is compressed.
495 * @since CUPS 1.2/Mac OS X 10.5@
498 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
499 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
501 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
506 * 'cupsFileEOF()' - Return the end-of-file status.
508 * @since CUPS 1.2/Mac OS X 10.5@
511 int /* O - 1 on end of file, 0 otherwise */
512 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
514 return (fp
? fp
->eof
: 1);
519 * 'cupsFileFind()' - Find a file using the specified path.
521 * This function allows the paths in the path string to be separated by
522 * colons (UNIX standard) or semicolons (Windows standard) and stores the
523 * result in the buffer supplied. If the file cannot be found in any of
524 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
525 * matches the current directory.
527 * @since CUPS 1.2/Mac OS X 10.5@
530 const char * /* O - Full path to file or @code NULL@ if not found */
531 cupsFileFind(const char *filename
, /* I - File to find */
532 const char *path
, /* I - Colon/semicolon-separated path */
533 int executable
, /* I - 1 = executable files, 0 = any file/dir */
534 char *buffer
, /* I - Filename buffer */
535 int bufsize
) /* I - Size of filename buffer */
537 char *bufptr
, /* Current position in buffer */
538 *bufend
; /* End of buffer */
542 * Range check input...
545 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
546 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
549 if (!filename
|| !buffer
|| bufsize
< 2)
555 * No path, so check current directory...
558 if (!access(filename
, 0))
560 strlcpy(buffer
, filename
, bufsize
);
568 * Now check each path and return the first match...
571 bufend
= buffer
+ bufsize
- 1;
577 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
579 if (*path
== ';' || *path
== ':')
582 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
585 strlcpy(bufptr
, filename
, bufend
- bufptr
);
588 if (!access(buffer
, 0))
590 if (!access(buffer
, executable
? X_OK
: 0))
593 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
599 else if (bufptr
< bufend
)
606 * Check the last path...
609 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
612 strlcpy(bufptr
, filename
, bufend
- bufptr
);
614 if (!access(buffer
, 0))
616 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
621 DEBUG_puts("1cupsFileFind: Returning NULL");
628 * 'cupsFileFlush()' - Flush pending output.
630 * @since CUPS 1.2/Mac OS X 10.5@
633 int /* O - 0 on success, -1 on error */
634 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
636 ssize_t bytes
; /* Bytes to write */
639 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
642 * Range check input...
645 if (!fp
|| fp
->mode
!= 'w')
647 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
651 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
653 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
660 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
662 #endif /* HAVE_LIBZ */
663 bytes
= cups_write(fp
, fp
->buf
, bytes
);
676 * 'cupsFileGetChar()' - Get a single character from a file.
678 * @since CUPS 1.2/Mac OS X 10.5@
681 int /* O - Character or -1 on end of file */
682 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
685 * Range check input...
688 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
690 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
695 * If the input buffer is empty, try to read more data...
698 if (fp
->ptr
>= fp
->end
)
699 if (cups_fill(fp
) < 0)
701 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
706 * Return the next character in the buffer...
709 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
713 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
715 return (*(fp
->ptr
)++ & 255);
720 * 'cupsFileGetConf()' - Get a line from a configuration file.
722 * @since CUPS 1.2/Mac OS X 10.5@
725 char * /* O - Line read or @code NULL@ on end of file or error */
726 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
727 char *buf
, /* O - String buffer */
728 size_t buflen
, /* I - Size of string buffer */
729 char **value
, /* O - Pointer to value */
730 int *linenum
) /* IO - Current line number */
732 char *ptr
; /* Pointer into line */
736 * Range check input...
739 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
740 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
743 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
744 !buf
|| buflen
< 2 || !value
)
753 * Read the next non-comment line...
758 while (cupsFileGets(fp
, buf
, buflen
))
763 * Strip any comments...
766 if ((ptr
= strchr(buf
, '#')) != NULL
)
768 if (ptr
> buf
&& ptr
[-1] == '\\')
771 _cups_strcpy(ptr
- 1, ptr
);
775 // Strip the comment and any trailing whitespace...
778 if (!_cups_isspace(ptr
[-1]))
789 * Strip leading whitespace...
792 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
795 _cups_strcpy(buf
, ptr
);
798 * See if there is anything left...
804 * Yes, grab any value and return...
807 for (ptr
= buf
; *ptr
; ptr
++)
808 if (_cups_isspace(*ptr
))
814 * Have a value, skip any other spaces...
817 while (_cups_isspace(*ptr
))
824 * Strip trailing whitespace and > for lines that begin with <...
827 ptr
+= strlen(ptr
) - 1;
829 if (buf
[0] == '<' && *ptr
== '>')
831 else if (buf
[0] == '<' && *ptr
!= '>')
841 while (ptr
> *value
&& _cups_isspace(*ptr
))
858 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
859 * contain binary data.
861 * This function differs from @link cupsFileGets@ in that the trailing CR
862 * and LF are preserved, as is any binary data on the line. The buffer is
863 * nul-terminated, however you should use the returned length to determine
864 * the number of bytes on the line.
866 * @since CUPS 1.2/Mac OS X 10.5@
869 size_t /* O - Number of bytes on line or 0 on end of file */
870 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
871 char *buf
, /* I - Buffer */
872 size_t buflen
) /* I - Size of buffer */
874 int ch
; /* Character from file */
875 char *ptr
, /* Current position in line buffer */
876 *end
; /* End of line buffer */
880 * Range check input...
883 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
884 fp
, buf
, CUPS_LLCAST buflen
));
886 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
890 * Now loop until we have a valid line...
893 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
895 if (fp
->ptr
>= fp
->end
)
896 if (cups_fill(fp
) <= 0)
899 *ptr
++ = ch
= *(fp
->ptr
)++;
908 if (fp
->ptr
>= fp
->end
)
909 if (cups_fill(fp
) <= 0)
912 if (*(fp
->ptr
) == '\n')
914 *ptr
++ = *(fp
->ptr
)++;
923 * Line feed ends a line...
932 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
939 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
941 * @since CUPS 1.2/Mac OS X 10.5@
944 char * /* O - Line read or @code NULL@ on end of file or error */
945 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
946 char *buf
, /* O - String buffer */
947 size_t buflen
) /* I - Size of string buffer */
949 int ch
; /* Character from file */
950 char *ptr
, /* Current position in line buffer */
951 *end
; /* End of line buffer */
955 * Range check input...
958 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
959 CUPS_LLCAST buflen
));
961 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
965 * Now loop until we have a valid line...
968 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
970 if (fp
->ptr
>= fp
->end
)
971 if (cups_fill(fp
) <= 0)
988 if (fp
->ptr
>= fp
->end
)
989 if (cups_fill(fp
) <= 0)
992 if (*(fp
->ptr
) == '\n')
1000 else if (ch
== '\n')
1003 * Line feed ends a line...
1014 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1021 * 'cupsFileLock()' - Temporarily lock access to a file.
1023 * @since CUPS 1.2/Mac OS X 10.5@
1026 int /* O - 0 on success, -1 on error */
1027 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
1028 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
1034 if (!fp
|| fp
->mode
== 's')
1042 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1044 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1050 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1052 * @since CUPS 1.2/Mac OS X 10.5@
1055 int /* O - File descriptor */
1056 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1066 * 'cupsFileOpen()' - Open a CUPS file.
1068 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1069 * existing file, "a" to append to an existing file or create a new file,
1070 * or "s" to open a socket connection.
1072 * When opening for writing ("w"), an optional number from 1 to 9 can be
1073 * supplied which enables Flate compression of the file. Compression is
1074 * not supported for the "a" (append) mode.
1076 * When opening a socket connection, the filename is a string of the form
1077 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1078 * connection as needed, generally preferring IPv6 connections when there is
1081 * @since CUPS 1.2/Mac OS X 10.5@
1084 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1085 cupsFileOpen(const char *filename
, /* I - Name of file */
1086 const char *mode
) /* I - Open mode */
1088 cups_file_t
*fp
; /* New CUPS file */
1089 int fd
; /* File descriptor */
1090 char hostname
[1024], /* Hostname */
1091 *portname
; /* Port "name" (number or service) */
1092 http_addrlist_t
*addrlist
; /* Host address list */
1095 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1099 * Range check input...
1102 if (!filename
|| !mode
||
1103 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1104 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1113 case 'a' : /* Append file */
1114 fd
= cups_open(filename
,
1115 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1118 case 'r' : /* Read file */
1119 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1122 case 'w' : /* Write file */
1123 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1124 if (fd
< 0 && errno
== ENOENT
)
1126 fd
= cups_open(filename
,
1127 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1128 if (fd
< 0 && errno
== EEXIST
)
1129 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1140 case 's' : /* Read/write socket */
1141 strlcpy(hostname
, filename
, sizeof(hostname
));
1142 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1148 * Lookup the hostname and service...
1151 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1155 * Connect to the server...
1158 if (!httpAddrConnect(addrlist
, &fd
))
1160 httpAddrFreeList(addrlist
);
1164 httpAddrFreeList(addrlist
);
1167 default : /* Remove bogus compiler warning... */
1175 * Create the CUPS file structure...
1178 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1194 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1196 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1197 * or "s" to treat the file descriptor as a bidirectional socket connection.
1199 * When opening for writing ("w"), an optional number from 1 to 9 can be
1200 * supplied which enables Flate compression of the file. Compression is
1201 * not supported for the "a" (append) mode.
1203 * @since CUPS 1.2/Mac OS X 10.5@
1206 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1207 cupsFileOpenFd(int fd
, /* I - File descriptor */
1208 const char *mode
) /* I - Open mode */
1210 cups_file_t
*fp
; /* New CUPS file */
1213 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1216 * Range check input...
1219 if (fd
< 0 || !mode
||
1220 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1221 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1225 * Allocate memory...
1228 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1240 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1245 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1248 if (mode
[1] >= '1' && mode
[1] <= '9')
1251 * Open a compressed stream, so write the standard gzip file
1255 unsigned char header
[10]; /* gzip file header */
1256 time_t curtime
; /* Current time */
1259 curtime
= time(NULL
);
1262 header
[2] = Z_DEFLATED
;
1264 header
[4] = curtime
;
1265 header
[5] = curtime
>> 8;
1266 header
[6] = curtime
>> 16;
1267 header
[7] = curtime
>> 24;
1271 cups_write(fp
, (char *)header
, 10);
1274 * Initialize the compressor...
1277 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1278 Z_DEFAULT_STRATEGY
);
1280 fp
->stream
.next_out
= fp
->cbuf
;
1281 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1283 fp
->crc
= crc32(0L, Z_NULL
, 0);
1285 #endif /* HAVE_LIBZ */
1296 default : /* Remove bogus compiler warning... */
1301 * Don't pass this file to child processes...
1305 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1313 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1315 * @since CUPS 1.2/Mac OS X 10.5@
1318 int /* O - Character or -1 on end of file */
1319 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1322 * Range check input...
1325 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1329 * If the input buffer is empty, try to read more data...
1332 if (fp
->ptr
>= fp
->end
)
1333 if (cups_fill(fp
) < 0)
1337 * Return the next character in the buffer...
1340 return (*(fp
->ptr
) & 255);
1345 * 'cupsFilePrintf()' - Write a formatted string.
1347 * @since CUPS 1.2/Mac OS X 10.5@
1350 int /* O - Number of bytes written or -1 on error */
1351 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1352 const char *format
, /* I - Printf-style format string */
1353 ...) /* I - Additional args as necessary */
1355 va_list ap
; /* Argument list */
1356 ssize_t bytes
; /* Formatted size */
1359 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1361 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1364 if (!fp
->printf_buffer
)
1367 * Start with an 1k printf buffer...
1370 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1373 fp
->printf_size
= 1024;
1376 va_start(ap
, format
);
1377 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1380 if (bytes
>= (ssize_t
)fp
->printf_size
)
1383 * Expand the printf buffer...
1386 char *temp
; /* Temporary buffer pointer */
1392 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1395 fp
->printf_buffer
= temp
;
1396 fp
->printf_size
= bytes
+ 1;
1398 va_start(ap
, format
);
1399 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1403 if (fp
->mode
== 's')
1405 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1410 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1415 if ((fp
->ptr
+ bytes
) > fp
->end
)
1416 if (cupsFileFlush(fp
))
1421 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1423 if (bytes
> sizeof(fp
->buf
))
1427 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1429 #endif /* HAVE_LIBZ */
1430 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1434 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1442 * 'cupsFilePutChar()' - Write a character.
1444 * @since CUPS 1.2/Mac OS X 10.5@
1447 int /* O - 0 on success, -1 on error */
1448 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1449 int c
) /* I - Character to write */
1452 * Range check input...
1455 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1458 if (fp
->mode
== 's')
1461 * Send character immediately over socket...
1464 char ch
; /* Output character */
1469 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1478 if (fp
->ptr
>= fp
->end
)
1479 if (cupsFileFlush(fp
))
1487 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1494 * 'cupsFilePutConf()' - Write a configuration line.
1496 * This function handles any comment escaping of the value.
1498 * @since CUPS 1.4/Mac OS X 10.6@
1501 ssize_t
/* O - Number of bytes written or -1 on error */
1502 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1503 const char *directive
, /* I - Directive */
1504 const char *value
) /* I - Value */
1506 ssize_t bytes
, /* Number of bytes written */
1507 temp
; /* Temporary byte count */
1508 const char *ptr
; /* Pointer into value */
1511 if (!fp
|| !directive
|| !*directive
)
1514 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1517 if (cupsFilePutChar(fp
, ' ') < 0)
1521 if (value
&& *value
)
1523 if ((ptr
= strchr(value
, '#')) != NULL
)
1526 * Need to quote the first # in the info string...
1529 if ((temp
= cupsFileWrite(fp
, value
, ptr
- value
)) < 0)
1533 if (cupsFilePutChar(fp
, '\\') < 0)
1537 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1541 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1547 if (cupsFilePutChar(fp
, '\n') < 0)
1555 * 'cupsFilePuts()' - Write a string.
1557 * Like the @code fputs@ function, no newline is appended to the string.
1559 * @since CUPS 1.2/Mac OS X 10.5@
1562 int /* O - Number of bytes written or -1 on error */
1563 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1564 const char *s
) /* I - String to write */
1566 ssize_t bytes
; /* Bytes to write */
1570 * Range check input...
1573 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1577 * Write the string...
1580 bytes
= (int)strlen(s
);
1582 if (fp
->mode
== 's')
1584 if (cups_write(fp
, s
, bytes
) < 0)
1589 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1594 if ((fp
->ptr
+ bytes
) > fp
->end
)
1595 if (cupsFileFlush(fp
))
1600 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1602 if (bytes
> sizeof(fp
->buf
))
1606 return (cups_compress(fp
, s
, bytes
));
1608 #endif /* HAVE_LIBZ */
1609 return (cups_write(fp
, s
, bytes
));
1613 memcpy(fp
->ptr
, s
, bytes
);
1621 * 'cupsFileRead()' - Read from a file.
1623 * @since CUPS 1.2/Mac OS X 10.5@
1626 ssize_t
/* O - Number of bytes read or -1 on error */
1627 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1628 char *buf
, /* O - Buffer */
1629 size_t bytes
) /* I - Number of bytes to read */
1631 size_t total
; /* Total bytes read */
1632 ssize_t count
; /* Bytes read */
1635 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1636 CUPS_LLCAST bytes
));
1639 * Range check input...
1642 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1649 * Loop until all bytes are read...
1655 if (fp
->ptr
>= fp
->end
)
1656 if (cups_fill(fp
) <= 0)
1658 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1659 CUPS_LLFMT
, CUPS_LLCAST total
));
1662 return ((ssize_t
)total
);
1667 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1668 if (count
> (ssize_t
)bytes
)
1669 count
= (ssize_t
)bytes
;
1671 memcpy(buf
, fp
->ptr
, count
);
1675 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1678 * Update the counts for the last read...
1687 * Return the total number of bytes read...
1690 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1692 return ((ssize_t
)total
);
1697 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1700 * @since CUPS 1.2/Mac OS X 10.5@
1703 off_t
/* O - New file position or -1 on error */
1704 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1707 * Range check input...
1710 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1711 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1713 if (!fp
|| fp
->mode
!= 'r')
1717 * Handle special cases...
1720 if (fp
->bufpos
== 0)
1723 * No seeking necessary...
1734 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1740 * Otherwise, seek in the file and cleanup any compression buffers...
1746 inflateEnd(&fp
->stream
);
1749 #endif /* HAVE_LIBZ */
1751 if (lseek(fp
->fd
, 0, SEEK_SET
))
1753 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1763 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1770 * 'cupsFileSeek()' - Seek in a file.
1772 * @since CUPS 1.2/Mac OS X 10.5@
1775 off_t
/* O - New file position or -1 on error */
1776 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1777 off_t pos
) /* I - Position in file */
1779 ssize_t bytes
; /* Number bytes in buffer */
1782 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1784 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1785 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1788 * Range check input...
1791 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1795 * Handle special cases...
1799 return (cupsFileRewind(fp
));
1803 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1805 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1807 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1810 * No seeking necessary...
1814 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1822 if (!fp
->compressed
&& !fp
->ptr
)
1825 * Preload a buffer to determine whether the file is compressed...
1828 if (cups_fill(fp
) < 0)
1831 #endif /* HAVE_LIBZ */
1834 * Seek forwards or backwards...
1839 if (pos
< fp
->bufpos
)
1842 * Need to seek backwards...
1845 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1850 inflateEnd(&fp
->stream
);
1852 lseek(fp
->fd
, 0, SEEK_SET
);
1858 while ((bytes
= cups_fill(fp
)) > 0)
1859 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1865 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1869 #endif /* HAVE_LIBZ */
1871 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1872 fp
->pos
= fp
->bufpos
;
1876 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1877 CUPS_LLCAST fp
->pos
));
1883 * Need to seek forwards...
1886 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1891 while ((bytes
= cups_fill(fp
)) > 0)
1893 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1900 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1904 #endif /* HAVE_LIBZ */
1906 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1907 fp
->pos
= fp
->bufpos
;
1911 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1912 CUPS_LLCAST fp
->pos
));
1916 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1923 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1925 * @since CUPS 1.2/Mac OS X 10.5@
1928 cups_file_t
* /* O - CUPS file */
1929 cupsFileStderr(void)
1931 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1935 * Open file descriptor 2 as needed...
1938 if (!cg
->stdio_files
[2])
1941 * Flush any pending output on the stdio file...
1947 * Open file descriptor 2...
1950 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1951 cg
->stdio_files
[2]->is_stdio
= 1;
1954 return (cg
->stdio_files
[2]);
1959 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1961 * @since CUPS 1.2/Mac OS X 10.5@
1964 cups_file_t
* /* O - CUPS file */
1967 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1971 * Open file descriptor 0 as needed...
1974 if (!cg
->stdio_files
[0])
1977 * Open file descriptor 0...
1980 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1981 cg
->stdio_files
[0]->is_stdio
= 1;
1984 return (cg
->stdio_files
[0]);
1989 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1991 * @since CUPS 1.2/Mac OS X 10.5@
1994 cups_file_t
* /* O - CUPS file */
1995 cupsFileStdout(void)
1997 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
2001 * Open file descriptor 1 as needed...
2004 if (!cg
->stdio_files
[1])
2007 * Flush any pending output on the stdio file...
2013 * Open file descriptor 1...
2016 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
2017 cg
->stdio_files
[1]->is_stdio
= 1;
2020 return (cg
->stdio_files
[1]);
2025 * 'cupsFileTell()' - Return the current file position.
2027 * @since CUPS 1.2/Mac OS X 10.5@
2030 off_t
/* O - File position */
2031 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
2033 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
2034 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
2035 CUPS_LLCAST (fp
? fp
->pos
: -1)));
2037 return (fp
? fp
->pos
: 0);
2042 * 'cupsFileUnlock()' - Unlock access to a file.
2044 * @since CUPS 1.2/Mac OS X 10.5@
2047 int /* O - 0 on success, -1 on error */
2048 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2054 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2056 if (!fp
|| fp
->mode
== 's')
2064 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2066 return (lockf(fp
->fd
, F_ULOCK
, 0));
2072 * 'cupsFileWrite()' - Write to a file.
2074 * @since CUPS 1.2/Mac OS X 10.5@
2077 ssize_t
/* O - Number of bytes written or -1 on error */
2078 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2079 const char *buf
, /* I - Buffer */
2080 size_t bytes
) /* I - Number of bytes to write */
2083 * Range check input...
2086 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2087 fp
, buf
, CUPS_LLCAST bytes
));
2089 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2096 * Write the buffer...
2099 if (fp
->mode
== 's')
2101 if (cups_write(fp
, buf
, bytes
) < 0)
2104 fp
->pos
+= (off_t
)bytes
;
2106 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2108 return ((ssize_t
)bytes
);
2111 if ((fp
->ptr
+ bytes
) > fp
->end
)
2112 if (cupsFileFlush(fp
))
2115 fp
->pos
+= (off_t
)bytes
;
2117 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2119 if (bytes
> sizeof(fp
->buf
))
2123 return (cups_compress(fp
, buf
, bytes
));
2125 #endif /* HAVE_LIBZ */
2126 return (cups_write(fp
, buf
, bytes
));
2130 memcpy(fp
->ptr
, buf
, bytes
);
2132 return ((ssize_t
)bytes
);
2139 * 'cups_compress()' - Compress a buffer of data.
2142 static ssize_t
/* O - Number of bytes written or -1 */
2143 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2144 const char *buf
, /* I - Buffer */
2145 size_t bytes
) /* I - Number bytes */
2147 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2148 CUPS_LLCAST bytes
));
2154 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
2157 * Deflate the bytes...
2160 fp
->stream
.next_in
= (Bytef
*)buf
;
2161 fp
->stream
.avail_in
= bytes
;
2163 while (fp
->stream
.avail_in
> 0)
2166 * Flush the current buffer...
2169 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2170 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2172 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
2174 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
2177 fp
->stream
.next_out
= fp
->cbuf
;
2178 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2181 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2186 #endif /* HAVE_LIBZ */
2190 * 'cups_fill()' - Fill the input buffer.
2193 static ssize_t
/* O - Number of bytes or -1 */
2194 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2196 ssize_t bytes
; /* Number of bytes read */
2198 int status
; /* Decompression status */
2199 const unsigned char *ptr
, /* Pointer into buffer */
2200 *end
; /* End of buffer */
2201 #endif /* HAVE_LIBZ */
2204 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2205 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2206 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2207 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2209 if (fp
->ptr
&& fp
->end
)
2210 fp
->bufpos
+= fp
->end
- fp
->buf
;
2213 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2215 while (!fp
->ptr
|| fp
->compressed
)
2218 * Check to see if we have read any data yet; if not, see if we have a
2219 * compressed file...
2225 * Reset the file position in case we are seeking...
2231 * Read the first bytes in the file to determine if we have a gzip'd
2235 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2238 * Can't read from file!
2241 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2242 CUPS_LLCAST bytes
));
2247 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2248 (fp
->buf
[1] & 255) != 0x8b ||
2249 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2252 * Not a gzip'd file!
2256 fp
->end
= fp
->buf
+ bytes
;
2258 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2259 CUPS_LLCAST bytes
));
2265 * Parse header junk: extra data, original name, and comment...
2268 ptr
= (unsigned char *)fp
->buf
+ 10;
2269 end
= (unsigned char *)fp
->buf
+ bytes
;
2271 if (fp
->buf
[3] & 0x04)
2274 * Skip extra data...
2277 if ((ptr
+ 2) > end
)
2280 * Can't read from file!
2286 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2292 * Can't read from file!
2299 if (fp
->buf
[3] & 0x08)
2302 * Skip original name data...
2305 while (ptr
< end
&& *ptr
)
2313 * Can't read from file!
2320 if (fp
->buf
[3] & 0x10)
2323 * Skip comment data...
2326 while (ptr
< end
&& *ptr
)
2334 * Can't read from file!
2341 if (fp
->buf
[3] & 0x02)
2344 * Skip header CRC data...
2352 * Can't read from file!
2360 * Copy the flate-compressed data to the compression buffer...
2363 if ((bytes
= end
- ptr
) > 0)
2364 memcpy(fp
->cbuf
, ptr
, bytes
);
2367 * Setup the decompressor data...
2370 fp
->stream
.zalloc
= (alloc_func
)0;
2371 fp
->stream
.zfree
= (free_func
)0;
2372 fp
->stream
.opaque
= (voidpf
)0;
2373 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2374 fp
->stream
.next_out
= NULL
;
2375 fp
->stream
.avail_in
= bytes
;
2376 fp
->stream
.avail_out
= 0;
2377 fp
->crc
= crc32(0L, Z_NULL
, 0);
2379 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2388 * If we have reached end-of-file, return immediately...
2395 * Fill the decompression buffer as needed...
2398 if (fp
->stream
.avail_in
== 0)
2400 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2403 fp
->stream
.next_in
= fp
->cbuf
;
2404 fp
->stream
.avail_in
= bytes
;
2408 * Decompress data from the buffer...
2411 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2412 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2414 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2416 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2417 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2418 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2420 if (status
== Z_STREAM_END
)
2423 * Read the CRC and length...
2426 unsigned char trailer
[8]; /* Trailer bytes */
2427 uLong tcrc
; /* Trailer CRC */
2430 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2433 * Can't get it, so mark end-of-file...
2440 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2443 if (tcrc
!= fp
->crc
)
2446 * Bad CRC, mark end-of-file...
2449 DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
2450 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2458 * Otherwise, reset the compressed flag so that we re-read the
2466 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2469 * Return the decompressed data...
2473 fp
->end
= fp
->buf
+ bytes
;
2479 #endif /* HAVE_LIBZ */
2482 * Read a buffer's full of data...
2485 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2488 * Can't read from file!
2499 * Return the bytes we read...
2504 fp
->end
= fp
->buf
+ bytes
;
2511 * 'cups_open()' - Safely open a file for writing.
2513 * We don't allow appending to directories or files that are hard-linked or
2517 static int /* O - File descriptor or -1 otherwise */
2518 cups_open(const char *filename
, /* I - Filename */
2519 int mode
) /* I - Open mode */
2521 int fd
; /* File descriptor */
2522 struct stat fileinfo
; /* File information */
2524 struct stat linkinfo
; /* Link information */
2532 if ((fd
= open(filename
, mode
, 0666)) < 0)
2536 * Then verify that the file descriptor doesn't point to a directory or hard-
2540 if (fstat(fd
, &fileinfo
))
2546 if (fileinfo
.st_nlink
!= 1)
2554 if (fileinfo
.st_mode
& _S_IFDIR
)
2556 if (S_ISDIR(fileinfo
.st_mode
))
2566 * Then use lstat to determine whether the filename is a symlink...
2569 if (lstat(filename
, &linkinfo
))
2575 if (S_ISLNK(linkinfo
.st_mode
) ||
2576 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2577 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2579 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2580 #endif /* HAVE_ST_GEN */
2581 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2582 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2599 * 'cups_read()' - Read from a file descriptor.
2602 static ssize_t
/* O - Number of bytes read or -1 */
2603 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2604 char *buf
, /* I - Buffer */
2605 size_t bytes
) /* I - Number bytes */
2607 ssize_t total
; /* Total bytes read */
2610 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2611 CUPS_LLCAST bytes
));
2614 * Loop until we read at least 0 bytes...
2620 if (fp
->mode
== 's')
2621 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2623 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2625 if (fp
->mode
== 's')
2626 total
= recv(fp
->fd
, buf
, bytes
, 0);
2628 total
= read(fp
->fd
, buf
, bytes
);
2631 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2637 * Reads can be interrupted by signals and unavailable resources...
2640 if (errno
== EAGAIN
|| errno
== EINTR
)
2647 * Return the total number of bytes read...
2655 * 'cups_write()' - Write to a file descriptor.
2658 static ssize_t
/* O - Number of bytes written or -1 */
2659 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2660 const char *buf
, /* I - Buffer */
2661 size_t bytes
) /* I - Number bytes */
2663 size_t total
; /* Total bytes written */
2664 ssize_t count
; /* Count this time */
2667 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2668 CUPS_LLCAST bytes
));
2671 * Loop until all bytes are written...
2678 if (fp
->mode
== 's')
2679 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2681 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2683 if (fp
->mode
== 's')
2684 count
= send(fp
->fd
, buf
, bytes
, 0);
2686 count
= write(fp
->fd
, buf
, bytes
);
2689 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2694 * Writes can be interrupted by signals and unavailable resources...
2697 if (errno
== EAGAIN
|| errno
== EINTR
)
2704 * Update the counts for the last write call...
2713 * Return the total number of bytes written...
2716 return ((ssize_t
)total
);
2721 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".