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 program even exist and is it accessible?
105 if (stat(filename
, &fileinfo
))
111 result
= _CUPS_FILE_CHECK_MISSING
;
116 * Check the execute bit...
119 result
= _CUPS_FILE_CHECK_OK
;
123 case _CUPS_FILE_CHECK_DIRECTORY
:
124 if (!S_ISDIR(fileinfo
.st_mode
))
125 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
129 if (!S_ISREG(fileinfo
.st_mode
))
130 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
138 * Are we doing root checks?
144 * Nope, so anything (else) goes...
151 * Verify permission of the file itself:
153 * 1. Must be owned by root
154 * 2. Must not be writable by group unless group is root/wheel/admin
155 * 3. Must not be setuid
156 * 4. Must not be writable by others
159 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
161 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
&&
162 fileinfo
.st_gid
!= 80) || /* 2. Must not be writable by group */
164 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
) ||
165 /* 2. Must not be writable by group */
166 #endif /* __APPLE__ */
167 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
168 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
170 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
174 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
175 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
179 * Now check the containing directory...
182 strlcpy(temp
, filename
, sizeof(temp
));
183 if ((ptr
= strrchr(temp
, '/')) != NULL
)
191 if (stat(temp
, &fileinfo
))
197 result
= _CUPS_FILE_CHECK_MISSING
;
198 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
204 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
206 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
&&
207 fileinfo
.st_gid
!= 80) || /* 2. Must not be writable by group */
209 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
) ||
210 /* 2. Must not be writable by group */
211 #endif /* __APPLE__ */
212 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
213 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
215 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
216 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
221 * Common return point...
228 cups_lang_t
*lang
= cupsLangDefault();
229 /* Localization information */
233 case _CUPS_FILE_CHECK_OK
:
234 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
235 snprintf(message
, sizeof(message
),
236 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
237 "(0%o/uid=%d/gid=%d).")),
238 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
239 (int)fileinfo
.st_gid
);
241 snprintf(message
, sizeof(message
),
242 _cupsLangString(lang
, _("File \"%s\" permissions OK "
243 "(0%o/uid=%d/gid=%d).")),
244 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
245 (int)fileinfo
.st_gid
);
248 case _CUPS_FILE_CHECK_MISSING
:
249 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
250 snprintf(message
, sizeof(message
),
251 _cupsLangString(lang
, _("Directory \"%s\" not available: "
253 filename
, strerror(errno
));
255 snprintf(message
, sizeof(message
),
256 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
257 filename
, strerror(errno
));
260 case _CUPS_FILE_CHECK_PERMISSIONS
:
261 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
262 snprintf(message
, sizeof(message
),
263 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
265 "(0%o/uid=%d/gid=%d).")),
266 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
267 (int)fileinfo
.st_gid
);
269 snprintf(message
, sizeof(message
),
270 _cupsLangString(lang
, _("File \"%s\" has insecure "
272 "(0%o/uid=%d/gid=%d).")),
273 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
274 (int)fileinfo
.st_gid
);
277 case _CUPS_FILE_CHECK_WRONG_TYPE
:
278 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
279 snprintf(message
, sizeof(message
),
280 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
283 snprintf(message
, sizeof(message
),
284 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
289 (*cb
)(context
, result
, message
);
297 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
301 _cupsFileCheckFilter(
302 void *context
, /* I - Context pointer (unused) */
303 _cups_fc_result_t result
, /* I - Result code */
304 const char *message
) /* I - Message text */
306 const char *prefix
; /* Messaging prefix */
311 case _CUPS_FILE_CHECK_OK
:
315 case _CUPS_FILE_CHECK_MISSING
:
316 case _CUPS_FILE_CHECK_WRONG_TYPE
:
318 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
321 case _CUPS_FILE_CHECK_PERMISSIONS
:
323 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
327 fprintf(stderr
, "%s: %s\n", prefix
, message
);
332 * 'cupsFileClose()' - Close a CUPS file.
334 * @since CUPS 1.2/Mac OS X 10.5@
337 int /* O - 0 on success, -1 on error */
338 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
340 int fd
; /* File descriptor */
341 char mode
; /* Open mode */
342 int status
; /* Return status */
343 int is_stdio
; /* Is a stdio file? */
346 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
356 * Flush pending write data...
360 status
= cupsFileFlush(fp
);
365 if (fp
->compressed
&& status
>= 0)
370 * Free decompression data...
373 inflateEnd(&fp
->stream
);
378 * Flush any remaining compressed data...
381 unsigned char trailer
[8]; /* Trailer CRC and length */
382 int done
; /* Done writing... */
385 fp
->stream
.avail_in
= 0;
389 if (fp
->stream
.next_out
> fp
->cbuf
)
391 if (cups_write(fp
, (char *)fp
->cbuf
,
392 fp
->stream
.next_out
- fp
->cbuf
) < 0)
395 fp
->stream
.next_out
= fp
->cbuf
;
396 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
399 if (done
|| status
< 0)
402 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
403 fp
->stream
.next_out
== fp
->cbuf
;
407 * Write the CRC and length...
410 trailer
[0] = fp
->crc
;
411 trailer
[1] = fp
->crc
>> 8;
412 trailer
[2] = fp
->crc
>> 16;
413 trailer
[3] = fp
->crc
>> 24;
414 trailer
[4] = fp
->pos
;
415 trailer
[5] = fp
->pos
>> 8;
416 trailer
[6] = fp
->pos
>> 16;
417 trailer
[7] = fp
->pos
>> 24;
419 if (cups_write(fp
, (char *)trailer
, 8) < 0)
423 * Free all memory used by the compression stream...
426 deflateEnd(&(fp
->stream
));
429 #endif /* HAVE_LIBZ */
432 * Save the file descriptor we used and free memory...
437 is_stdio
= fp
->is_stdio
;
439 if (fp
->printf_buffer
)
440 free(fp
->printf_buffer
);
445 * Close the file, returning the close status...
450 if (closesocket(fd
) < 0)
464 * 'cupsFileCompression()' - Return whether a file is compressed.
466 * @since CUPS 1.2/Mac OS X 10.5@
469 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
470 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
472 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
477 * 'cupsFileEOF()' - Return the end-of-file status.
479 * @since CUPS 1.2/Mac OS X 10.5@
482 int /* O - 1 on end of file, 0 otherwise */
483 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
485 return (fp
? fp
->eof
: 1);
490 * 'cupsFileFind()' - Find a file using the specified path.
492 * This function allows the paths in the path string to be separated by
493 * colons (UNIX standard) or semicolons (Windows standard) and stores the
494 * result in the buffer supplied. If the file cannot be found in any of
495 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
496 * matches the current directory.
498 * @since CUPS 1.2/Mac OS X 10.5@
501 const char * /* O - Full path to file or @code NULL@ if not found */
502 cupsFileFind(const char *filename
, /* I - File to find */
503 const char *path
, /* I - Colon/semicolon-separated path */
504 int executable
, /* I - 1 = executable files, 0 = any file/dir */
505 char *buffer
, /* I - Filename buffer */
506 int bufsize
) /* I - Size of filename buffer */
508 char *bufptr
, /* Current position in buffer */
509 *bufend
; /* End of buffer */
513 * Range check input...
516 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
517 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
520 if (!filename
|| !buffer
|| bufsize
< 2)
526 * No path, so check current directory...
529 if (!access(filename
, 0))
531 strlcpy(buffer
, filename
, bufsize
);
539 * Now check each path and return the first match...
542 bufend
= buffer
+ bufsize
- 1;
548 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
550 if (*path
== ';' || *path
== ':')
553 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
556 strlcpy(bufptr
, filename
, bufend
- bufptr
);
559 if (!access(buffer
, 0))
561 if (!access(buffer
, executable
? X_OK
: 0))
564 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
570 else if (bufptr
< bufend
)
577 * Check the last path...
580 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
583 strlcpy(bufptr
, filename
, bufend
- bufptr
);
585 if (!access(buffer
, 0))
587 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
592 DEBUG_puts("1cupsFileFind: Returning NULL");
599 * 'cupsFileFlush()' - Flush pending output.
601 * @since CUPS 1.2/Mac OS X 10.5@
604 int /* O - 0 on success, -1 on error */
605 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
607 ssize_t bytes
; /* Bytes to write */
610 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
613 * Range check input...
616 if (!fp
|| fp
->mode
!= 'w')
618 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
622 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
624 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
631 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
633 #endif /* HAVE_LIBZ */
634 bytes
= cups_write(fp
, fp
->buf
, bytes
);
647 * 'cupsFileGetChar()' - Get a single character from a file.
649 * @since CUPS 1.2/Mac OS X 10.5@
652 int /* O - Character or -1 on end of file */
653 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
656 * Range check input...
659 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
661 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
666 * If the input buffer is empty, try to read more data...
669 if (fp
->ptr
>= fp
->end
)
670 if (cups_fill(fp
) < 0)
672 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
677 * Return the next character in the buffer...
680 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
684 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
686 return (*(fp
->ptr
)++ & 255);
691 * 'cupsFileGetConf()' - Get a line from a configuration file.
693 * @since CUPS 1.2/Mac OS X 10.5@
696 char * /* O - Line read or @code NULL@ on end of file or error */
697 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
698 char *buf
, /* O - String buffer */
699 size_t buflen
, /* I - Size of string buffer */
700 char **value
, /* O - Pointer to value */
701 int *linenum
) /* IO - Current line number */
703 char *ptr
; /* Pointer into line */
707 * Range check input...
710 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
711 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
714 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
715 !buf
|| buflen
< 2 || !value
)
724 * Read the next non-comment line...
729 while (cupsFileGets(fp
, buf
, buflen
))
734 * Strip any comments...
737 if ((ptr
= strchr(buf
, '#')) != NULL
)
739 if (ptr
> buf
&& ptr
[-1] == '\\')
742 _cups_strcpy(ptr
- 1, ptr
);
746 // Strip the comment and any trailing whitespace...
749 if (!_cups_isspace(ptr
[-1]))
760 * Strip leading whitespace...
763 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
766 _cups_strcpy(buf
, ptr
);
769 * See if there is anything left...
775 * Yes, grab any value and return...
778 for (ptr
= buf
; *ptr
; ptr
++)
779 if (_cups_isspace(*ptr
))
785 * Have a value, skip any other spaces...
788 while (_cups_isspace(*ptr
))
795 * Strip trailing whitespace and > for lines that begin with <...
798 ptr
+= strlen(ptr
) - 1;
800 if (buf
[0] == '<' && *ptr
== '>')
802 else if (buf
[0] == '<' && *ptr
!= '>')
812 while (ptr
> *value
&& _cups_isspace(*ptr
))
829 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
830 * contain binary data.
832 * This function differs from @link cupsFileGets@ in that the trailing CR
833 * and LF are preserved, as is any binary data on the line. The buffer is
834 * nul-terminated, however you should use the returned length to determine
835 * the number of bytes on the line.
837 * @since CUPS 1.2/Mac OS X 10.5@
840 size_t /* O - Number of bytes on line or 0 on end of file */
841 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
842 char *buf
, /* I - Buffer */
843 size_t buflen
) /* I - Size of buffer */
845 int ch
; /* Character from file */
846 char *ptr
, /* Current position in line buffer */
847 *end
; /* End of line buffer */
851 * Range check input...
854 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
855 fp
, buf
, CUPS_LLCAST buflen
));
857 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
861 * Now loop until we have a valid line...
864 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
866 if (fp
->ptr
>= fp
->end
)
867 if (cups_fill(fp
) <= 0)
870 *ptr
++ = ch
= *(fp
->ptr
)++;
879 if (fp
->ptr
>= fp
->end
)
880 if (cups_fill(fp
) <= 0)
883 if (*(fp
->ptr
) == '\n')
885 *ptr
++ = *(fp
->ptr
)++;
894 * Line feed ends a line...
903 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
910 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
912 * @since CUPS 1.2/Mac OS X 10.5@
915 char * /* O - Line read or @code NULL@ on end of file or error */
916 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
917 char *buf
, /* O - String buffer */
918 size_t buflen
) /* I - Size of string buffer */
920 int ch
; /* Character from file */
921 char *ptr
, /* Current position in line buffer */
922 *end
; /* End of line buffer */
926 * Range check input...
929 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
930 CUPS_LLCAST buflen
));
932 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
936 * Now loop until we have a valid line...
939 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
941 if (fp
->ptr
>= fp
->end
)
942 if (cups_fill(fp
) <= 0)
959 if (fp
->ptr
>= fp
->end
)
960 if (cups_fill(fp
) <= 0)
963 if (*(fp
->ptr
) == '\n')
974 * Line feed ends a line...
985 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
992 * 'cupsFileLock()' - Temporarily lock access to a file.
994 * @since CUPS 1.2/Mac OS X 10.5@
997 int /* O - 0 on success, -1 on error */
998 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
999 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
1005 if (!fp
|| fp
->mode
== 's')
1013 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1015 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1021 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1023 * @since CUPS 1.2/Mac OS X 10.5@
1026 int /* O - File descriptor */
1027 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1037 * 'cupsFileOpen()' - Open a CUPS file.
1039 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1040 * existing file, "a" to append to an existing file or create a new file,
1041 * or "s" to open a socket connection.
1043 * When opening for writing ("w"), an optional number from 1 to 9 can be
1044 * supplied which enables Flate compression of the file. Compression is
1045 * not supported for the "a" (append) mode.
1047 * When opening a socket connection, the filename is a string of the form
1048 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1049 * connection as needed, generally preferring IPv6 connections when there is
1052 * @since CUPS 1.2/Mac OS X 10.5@
1055 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1056 cupsFileOpen(const char *filename
, /* I - Name of file */
1057 const char *mode
) /* I - Open mode */
1059 cups_file_t
*fp
; /* New CUPS file */
1060 int fd
; /* File descriptor */
1061 char hostname
[1024], /* Hostname */
1062 *portname
; /* Port "name" (number or service) */
1063 http_addrlist_t
*addrlist
; /* Host address list */
1066 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1070 * Range check input...
1073 if (!filename
|| !mode
||
1074 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1075 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1084 case 'a' : /* Append file */
1085 fd
= cups_open(filename
,
1086 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1089 case 'r' : /* Read file */
1090 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1093 case 'w' : /* Write file */
1094 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1095 if (fd
< 0 && errno
== ENOENT
)
1097 fd
= cups_open(filename
,
1098 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1099 if (fd
< 0 && errno
== EEXIST
)
1100 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1111 case 's' : /* Read/write socket */
1112 strlcpy(hostname
, filename
, sizeof(hostname
));
1113 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1119 * Lookup the hostname and service...
1122 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1126 * Connect to the server...
1129 if (!httpAddrConnect(addrlist
, &fd
))
1131 httpAddrFreeList(addrlist
);
1135 httpAddrFreeList(addrlist
);
1138 default : /* Remove bogus compiler warning... */
1146 * Create the CUPS file structure...
1149 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1165 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1167 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1168 * or "s" to treat the file descriptor as a bidirectional socket connection.
1170 * When opening for writing ("w"), an optional number from 1 to 9 can be
1171 * supplied which enables Flate compression of the file. Compression is
1172 * not supported for the "a" (append) mode.
1174 * @since CUPS 1.2/Mac OS X 10.5@
1177 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1178 cupsFileOpenFd(int fd
, /* I - File descriptor */
1179 const char *mode
) /* I - Open mode */
1181 cups_file_t
*fp
; /* New CUPS file */
1184 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1187 * Range check input...
1190 if (fd
< 0 || !mode
||
1191 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1192 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1196 * Allocate memory...
1199 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1211 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1216 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1219 if (mode
[1] >= '1' && mode
[1] <= '9')
1222 * Open a compressed stream, so write the standard gzip file
1226 unsigned char header
[10]; /* gzip file header */
1227 time_t curtime
; /* Current time */
1230 curtime
= time(NULL
);
1233 header
[2] = Z_DEFLATED
;
1235 header
[4] = curtime
;
1236 header
[5] = curtime
>> 8;
1237 header
[6] = curtime
>> 16;
1238 header
[7] = curtime
>> 24;
1242 cups_write(fp
, (char *)header
, 10);
1245 * Initialize the compressor...
1248 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1249 Z_DEFAULT_STRATEGY
);
1251 fp
->stream
.next_out
= fp
->cbuf
;
1252 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1254 fp
->crc
= crc32(0L, Z_NULL
, 0);
1256 #endif /* HAVE_LIBZ */
1267 default : /* Remove bogus compiler warning... */
1272 * Don't pass this file to child processes...
1276 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1284 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1286 * @since CUPS 1.2/Mac OS X 10.5@
1289 int /* O - Character or -1 on end of file */
1290 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1293 * Range check input...
1296 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1300 * If the input buffer is empty, try to read more data...
1303 if (fp
->ptr
>= fp
->end
)
1304 if (cups_fill(fp
) < 0)
1308 * Return the next character in the buffer...
1311 return (*(fp
->ptr
) & 255);
1316 * 'cupsFilePrintf()' - Write a formatted string.
1318 * @since CUPS 1.2/Mac OS X 10.5@
1321 int /* O - Number of bytes written or -1 on error */
1322 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1323 const char *format
, /* I - Printf-style format string */
1324 ...) /* I - Additional args as necessary */
1326 va_list ap
; /* Argument list */
1327 ssize_t bytes
; /* Formatted size */
1330 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1332 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1335 if (!fp
->printf_buffer
)
1338 * Start with an 1k printf buffer...
1341 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1344 fp
->printf_size
= 1024;
1347 va_start(ap
, format
);
1348 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1351 if (bytes
>= (ssize_t
)fp
->printf_size
)
1354 * Expand the printf buffer...
1357 char *temp
; /* Temporary buffer pointer */
1363 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1366 fp
->printf_buffer
= temp
;
1367 fp
->printf_size
= bytes
+ 1;
1369 va_start(ap
, format
);
1370 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1374 if (fp
->mode
== 's')
1376 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1381 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1386 if ((fp
->ptr
+ bytes
) > fp
->end
)
1387 if (cupsFileFlush(fp
))
1392 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1394 if (bytes
> sizeof(fp
->buf
))
1398 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1400 #endif /* HAVE_LIBZ */
1401 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1405 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1413 * 'cupsFilePutChar()' - Write a character.
1415 * @since CUPS 1.2/Mac OS X 10.5@
1418 int /* O - 0 on success, -1 on error */
1419 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1420 int c
) /* I - Character to write */
1423 * Range check input...
1426 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1429 if (fp
->mode
== 's')
1432 * Send character immediately over socket...
1435 char ch
; /* Output character */
1440 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1449 if (fp
->ptr
>= fp
->end
)
1450 if (cupsFileFlush(fp
))
1458 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1465 * 'cupsFilePutConf()' - Write a configuration line.
1467 * This function handles any comment escaping of the value.
1469 * @since CUPS 1.4/Mac OS X 10.6@
1472 ssize_t
/* O - Number of bytes written or -1 on error */
1473 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1474 const char *directive
, /* I - Directive */
1475 const char *value
) /* I - Value */
1477 ssize_t bytes
, /* Number of bytes written */
1478 temp
; /* Temporary byte count */
1479 const char *ptr
; /* Pointer into value */
1482 if (!fp
|| !directive
|| !*directive
)
1485 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1488 if (cupsFilePutChar(fp
, ' ') < 0)
1492 if (value
&& *value
)
1494 if ((ptr
= strchr(value
, '#')) != NULL
)
1497 * Need to quote the first # in the info string...
1500 if ((temp
= cupsFileWrite(fp
, value
, ptr
- value
)) < 0)
1504 if (cupsFilePutChar(fp
, '\\') < 0)
1508 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1512 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1518 if (cupsFilePutChar(fp
, '\n') < 0)
1526 * 'cupsFilePuts()' - Write a string.
1528 * Like the @code fputs@ function, no newline is appended to the string.
1530 * @since CUPS 1.2/Mac OS X 10.5@
1533 int /* O - Number of bytes written or -1 on error */
1534 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1535 const char *s
) /* I - String to write */
1537 ssize_t bytes
; /* Bytes to write */
1541 * Range check input...
1544 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1548 * Write the string...
1551 bytes
= (int)strlen(s
);
1553 if (fp
->mode
== 's')
1555 if (cups_write(fp
, s
, bytes
) < 0)
1560 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1565 if ((fp
->ptr
+ bytes
) > fp
->end
)
1566 if (cupsFileFlush(fp
))
1571 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1573 if (bytes
> sizeof(fp
->buf
))
1577 return (cups_compress(fp
, s
, bytes
));
1579 #endif /* HAVE_LIBZ */
1580 return (cups_write(fp
, s
, bytes
));
1584 memcpy(fp
->ptr
, s
, bytes
);
1592 * 'cupsFileRead()' - Read from a file.
1594 * @since CUPS 1.2/Mac OS X 10.5@
1597 ssize_t
/* O - Number of bytes read or -1 on error */
1598 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1599 char *buf
, /* O - Buffer */
1600 size_t bytes
) /* I - Number of bytes to read */
1602 size_t total
; /* Total bytes read */
1603 ssize_t count
; /* Bytes read */
1606 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1607 CUPS_LLCAST bytes
));
1610 * Range check input...
1613 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1620 * Loop until all bytes are read...
1626 if (fp
->ptr
>= fp
->end
)
1627 if (cups_fill(fp
) <= 0)
1629 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1630 CUPS_LLFMT
, CUPS_LLCAST total
));
1633 return ((ssize_t
)total
);
1638 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1639 if (count
> (ssize_t
)bytes
)
1640 count
= (ssize_t
)bytes
;
1642 memcpy(buf
, fp
->ptr
, count
);
1646 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1649 * Update the counts for the last read...
1658 * Return the total number of bytes read...
1661 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1663 return ((ssize_t
)total
);
1668 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1671 * @since CUPS 1.2/Mac OS X 10.5@
1674 off_t
/* O - New file position or -1 on error */
1675 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1678 * Range check input...
1681 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1682 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1684 if (!fp
|| fp
->mode
!= 'r')
1688 * Handle special cases...
1691 if (fp
->bufpos
== 0)
1694 * No seeking necessary...
1705 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1711 * Otherwise, seek in the file and cleanup any compression buffers...
1717 inflateEnd(&fp
->stream
);
1720 #endif /* HAVE_LIBZ */
1722 if (lseek(fp
->fd
, 0, SEEK_SET
))
1724 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1734 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1741 * 'cupsFileSeek()' - Seek in a file.
1743 * @since CUPS 1.2/Mac OS X 10.5@
1746 off_t
/* O - New file position or -1 on error */
1747 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1748 off_t pos
) /* I - Position in file */
1750 ssize_t bytes
; /* Number bytes in buffer */
1753 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1755 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1756 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1759 * Range check input...
1762 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1766 * Handle special cases...
1770 return (cupsFileRewind(fp
));
1774 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1776 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1778 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1781 * No seeking necessary...
1785 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1793 if (!fp
->compressed
&& !fp
->ptr
)
1796 * Preload a buffer to determine whether the file is compressed...
1799 if (cups_fill(fp
) < 0)
1802 #endif /* HAVE_LIBZ */
1805 * Seek forwards or backwards...
1810 if (pos
< fp
->bufpos
)
1813 * Need to seek backwards...
1816 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1821 inflateEnd(&fp
->stream
);
1823 lseek(fp
->fd
, 0, SEEK_SET
);
1829 while ((bytes
= cups_fill(fp
)) > 0)
1830 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1836 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1840 #endif /* HAVE_LIBZ */
1842 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1843 fp
->pos
= fp
->bufpos
;
1847 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1848 CUPS_LLCAST fp
->pos
));
1854 * Need to seek forwards...
1857 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1862 while ((bytes
= cups_fill(fp
)) > 0)
1864 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1871 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1875 #endif /* HAVE_LIBZ */
1877 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1878 fp
->pos
= fp
->bufpos
;
1882 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1883 CUPS_LLCAST fp
->pos
));
1887 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1894 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1896 * @since CUPS 1.2/Mac OS X 10.5@
1899 cups_file_t
* /* O - CUPS file */
1900 cupsFileStderr(void)
1902 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1906 * Open file descriptor 2 as needed...
1909 if (!cg
->stdio_files
[2])
1912 * Flush any pending output on the stdio file...
1918 * Open file descriptor 2...
1921 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1922 cg
->stdio_files
[2]->is_stdio
= 1;
1925 return (cg
->stdio_files
[2]);
1930 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1932 * @since CUPS 1.2/Mac OS X 10.5@
1935 cups_file_t
* /* O - CUPS file */
1938 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1942 * Open file descriptor 0 as needed...
1945 if (!cg
->stdio_files
[0])
1948 * Open file descriptor 0...
1951 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1952 cg
->stdio_files
[0]->is_stdio
= 1;
1955 return (cg
->stdio_files
[0]);
1960 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1962 * @since CUPS 1.2/Mac OS X 10.5@
1965 cups_file_t
* /* O - CUPS file */
1966 cupsFileStdout(void)
1968 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1972 * Open file descriptor 1 as needed...
1975 if (!cg
->stdio_files
[1])
1978 * Flush any pending output on the stdio file...
1984 * Open file descriptor 1...
1987 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1988 cg
->stdio_files
[1]->is_stdio
= 1;
1991 return (cg
->stdio_files
[1]);
1996 * 'cupsFileTell()' - Return the current file position.
1998 * @since CUPS 1.2/Mac OS X 10.5@
2001 off_t
/* O - File position */
2002 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
2004 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
2005 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
2006 CUPS_LLCAST (fp
? fp
->pos
: -1)));
2008 return (fp
? fp
->pos
: 0);
2013 * 'cupsFileUnlock()' - Unlock access to a file.
2015 * @since CUPS 1.2/Mac OS X 10.5@
2018 int /* O - 0 on success, -1 on error */
2019 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2025 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2027 if (!fp
|| fp
->mode
== 's')
2035 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2037 return (lockf(fp
->fd
, F_ULOCK
, 0));
2043 * 'cupsFileWrite()' - Write to a file.
2045 * @since CUPS 1.2/Mac OS X 10.5@
2048 ssize_t
/* O - Number of bytes written or -1 on error */
2049 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2050 const char *buf
, /* I - Buffer */
2051 size_t bytes
) /* I - Number of bytes to write */
2054 * Range check input...
2057 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2058 fp
, buf
, CUPS_LLCAST bytes
));
2060 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2067 * Write the buffer...
2070 if (fp
->mode
== 's')
2072 if (cups_write(fp
, buf
, bytes
) < 0)
2075 fp
->pos
+= (off_t
)bytes
;
2077 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2079 return ((ssize_t
)bytes
);
2082 if ((fp
->ptr
+ bytes
) > fp
->end
)
2083 if (cupsFileFlush(fp
))
2086 fp
->pos
+= (off_t
)bytes
;
2088 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2090 if (bytes
> sizeof(fp
->buf
))
2094 return (cups_compress(fp
, buf
, bytes
));
2096 #endif /* HAVE_LIBZ */
2097 return (cups_write(fp
, buf
, bytes
));
2101 memcpy(fp
->ptr
, buf
, bytes
);
2103 return ((ssize_t
)bytes
);
2110 * 'cups_compress()' - Compress a buffer of data.
2113 static ssize_t
/* O - Number of bytes written or -1 */
2114 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2115 const char *buf
, /* I - Buffer */
2116 size_t bytes
) /* I - Number bytes */
2118 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2119 CUPS_LLCAST bytes
));
2125 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
2128 * Deflate the bytes...
2131 fp
->stream
.next_in
= (Bytef
*)buf
;
2132 fp
->stream
.avail_in
= bytes
;
2134 while (fp
->stream
.avail_in
> 0)
2137 * Flush the current buffer...
2140 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2141 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2143 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
2145 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
2148 fp
->stream
.next_out
= fp
->cbuf
;
2149 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2152 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2157 #endif /* HAVE_LIBZ */
2161 * 'cups_fill()' - Fill the input buffer.
2164 static ssize_t
/* O - Number of bytes or -1 */
2165 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2167 ssize_t bytes
; /* Number of bytes read */
2169 int status
; /* Decompression status */
2170 const unsigned char *ptr
, /* Pointer into buffer */
2171 *end
; /* End of buffer */
2172 #endif /* HAVE_LIBZ */
2175 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2176 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2177 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2178 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2180 if (fp
->ptr
&& fp
->end
)
2181 fp
->bufpos
+= fp
->end
- fp
->buf
;
2184 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2186 while (!fp
->ptr
|| fp
->compressed
)
2189 * Check to see if we have read any data yet; if not, see if we have a
2190 * compressed file...
2196 * Reset the file position in case we are seeking...
2202 * Read the first bytes in the file to determine if we have a gzip'd
2206 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2209 * Can't read from file!
2212 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2213 CUPS_LLCAST bytes
));
2218 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2219 (fp
->buf
[1] & 255) != 0x8b ||
2220 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2223 * Not a gzip'd file!
2227 fp
->end
= fp
->buf
+ bytes
;
2229 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2230 CUPS_LLCAST bytes
));
2236 * Parse header junk: extra data, original name, and comment...
2239 ptr
= (unsigned char *)fp
->buf
+ 10;
2240 end
= (unsigned char *)fp
->buf
+ bytes
;
2242 if (fp
->buf
[3] & 0x04)
2245 * Skip extra data...
2248 if ((ptr
+ 2) > end
)
2251 * Can't read from file!
2257 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2263 * Can't read from file!
2270 if (fp
->buf
[3] & 0x08)
2273 * Skip original name data...
2276 while (ptr
< end
&& *ptr
)
2284 * Can't read from file!
2291 if (fp
->buf
[3] & 0x10)
2294 * Skip comment data...
2297 while (ptr
< end
&& *ptr
)
2305 * Can't read from file!
2312 if (fp
->buf
[3] & 0x02)
2315 * Skip header CRC data...
2323 * Can't read from file!
2331 * Copy the flate-compressed data to the compression buffer...
2334 if ((bytes
= end
- ptr
) > 0)
2335 memcpy(fp
->cbuf
, ptr
, bytes
);
2338 * Setup the decompressor data...
2341 fp
->stream
.zalloc
= (alloc_func
)0;
2342 fp
->stream
.zfree
= (free_func
)0;
2343 fp
->stream
.opaque
= (voidpf
)0;
2344 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2345 fp
->stream
.next_out
= NULL
;
2346 fp
->stream
.avail_in
= bytes
;
2347 fp
->stream
.avail_out
= 0;
2348 fp
->crc
= crc32(0L, Z_NULL
, 0);
2350 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2359 * If we have reached end-of-file, return immediately...
2366 * Fill the decompression buffer as needed...
2369 if (fp
->stream
.avail_in
== 0)
2371 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2374 fp
->stream
.next_in
= fp
->cbuf
;
2375 fp
->stream
.avail_in
= bytes
;
2379 * Decompress data from the buffer...
2382 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2383 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2385 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2387 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2388 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2389 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2391 if (status
== Z_STREAM_END
)
2394 * Read the CRC and length...
2397 unsigned char trailer
[8]; /* Trailer bytes */
2398 uLong tcrc
; /* Trailer CRC */
2401 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2404 * Can't get it, so mark end-of-file...
2411 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2414 if (tcrc
!= fp
->crc
)
2417 * Bad CRC, mark end-of-file...
2420 DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
2421 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2429 * Otherwise, reset the compressed flag so that we re-read the
2437 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2440 * Return the decompressed data...
2444 fp
->end
= fp
->buf
+ bytes
;
2450 #endif /* HAVE_LIBZ */
2453 * Read a buffer's full of data...
2456 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2459 * Can't read from file!
2470 * Return the bytes we read...
2475 fp
->end
= fp
->buf
+ bytes
;
2482 * 'cups_open()' - Safely open a file for writing.
2484 * We don't allow appending to directories or files that are hard-linked or
2488 static int /* O - File descriptor or -1 otherwise */
2489 cups_open(const char *filename
, /* I - Filename */
2490 int mode
) /* I - Open mode */
2492 int fd
; /* File descriptor */
2493 struct stat fileinfo
; /* File information */
2495 struct stat linkinfo
; /* Link information */
2503 if ((fd
= open(filename
, mode
, 0666)) < 0)
2507 * Then verify that the file descriptor doesn't point to a directory or hard-
2511 if (fstat(fd
, &fileinfo
))
2517 if (fileinfo
.st_nlink
!= 1)
2525 if (fileinfo
.st_mode
& _S_IFDIR
)
2527 if (S_ISDIR(fileinfo
.st_mode
))
2537 * Then use lstat to determine whether the filename is a symlink...
2540 if (lstat(filename
, &linkinfo
))
2546 if (S_ISLNK(linkinfo
.st_mode
) ||
2547 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2548 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2550 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2551 #endif /* HAVE_ST_GEN */
2552 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2553 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2570 * 'cups_read()' - Read from a file descriptor.
2573 static ssize_t
/* O - Number of bytes read or -1 */
2574 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2575 char *buf
, /* I - Buffer */
2576 size_t bytes
) /* I - Number bytes */
2578 ssize_t total
; /* Total bytes read */
2581 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2582 CUPS_LLCAST bytes
));
2585 * Loop until we read at least 0 bytes...
2591 if (fp
->mode
== 's')
2592 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2594 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2596 if (fp
->mode
== 's')
2597 total
= recv(fp
->fd
, buf
, bytes
, 0);
2599 total
= read(fp
->fd
, buf
, bytes
);
2602 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2608 * Reads can be interrupted by signals and unavailable resources...
2611 if (errno
== EAGAIN
|| errno
== EINTR
)
2618 * Return the total number of bytes read...
2626 * 'cups_write()' - Write to a file descriptor.
2629 static ssize_t
/* O - Number of bytes written or -1 */
2630 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2631 const char *buf
, /* I - Buffer */
2632 size_t bytes
) /* I - Number bytes */
2634 size_t total
; /* Total bytes written */
2635 ssize_t count
; /* Count this time */
2638 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2639 CUPS_LLCAST bytes
));
2642 * Loop until all bytes are written...
2649 if (fp
->mode
== 's')
2650 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2652 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2654 if (fp
->mode
== 's')
2655 count
= send(fp
->fd
, buf
, bytes
, 0);
2657 count
= write(fp
->fd
, buf
, bytes
);
2660 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2665 * Writes can be interrupted by signals and unavailable resources...
2668 if (errno
== EAGAIN
|| errno
== EINTR
)
2675 * Update the counts for the last write call...
2684 * Return the total number of bytes written...
2687 return ((ssize_t
)total
);
2692 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".