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 * cupsFilePutConf() - Write a configuration line.
43 * cupsFilePuts() - Write a string.
44 * cupsFileRead() - Read from a file.
45 * cupsFileRewind() - Set the current file position to the beginning of
47 * cupsFileSeek() - Seek in a file.
48 * cupsFileStderr() - Return a CUPS file associated with stderr.
49 * cupsFileStdin() - Return a CUPS file associated with stdin.
50 * cupsFileStdout() - Return a CUPS file associated with stdout.
51 * cupsFileTell() - Return the current file position.
52 * cupsFileUnlock() - Unlock access to a file.
53 * cupsFileWrite() - Write to a file.
54 * cups_compress() - Compress a buffer of data.
55 * cups_fill() - Fill the input buffer.
56 * cups_open() - Safely open a file for writing.
57 * cups_read() - Read from a file descriptor.
58 * cups_write() - Write to a file descriptor.
62 * Include necessary headers...
65 #include "file-private.h"
67 #include <sys/types.h>
75 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
76 #endif /* HAVE_LIBZ */
77 static ssize_t
cups_fill(cups_file_t
*fp
);
78 static int cups_open(const char *filename
, int mode
);
79 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
80 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
85 * '_cupsFileCheck()' - Check the permissions of the given filename.
88 _cups_fc_result_t
/* O - Check result */
90 const char *filename
, /* I - Filename to check */
91 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
92 int dorootchecks
, /* I - Check for root permissions? */
93 _cups_fc_func_t cb
, /* I - Callback function */
94 void *context
) /* I - Context pointer for callback */
97 struct stat fileinfo
; /* File information */
98 char message
[1024], /* Message string */
99 temp
[1024], /* Parent directory filename */
100 *ptr
; /* Pointer into parent directory */
101 _cups_fc_result_t result
; /* Check result */
105 * Does the filename contain a relative path ("../")?
108 if (strstr(filename
, "../"))
114 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
119 * Does the program even exist and is it accessible?
122 if (stat(filename
, &fileinfo
))
128 result
= _CUPS_FILE_CHECK_MISSING
;
133 * Check the execute bit...
136 result
= _CUPS_FILE_CHECK_OK
;
140 case _CUPS_FILE_CHECK_DIRECTORY
:
141 if (!S_ISDIR(fileinfo
.st_mode
))
142 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
146 if (!S_ISREG(fileinfo
.st_mode
))
147 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
155 * Are we doing root checks?
161 * Nope, so anything (else) goes...
168 * Verify permission of the file itself:
170 * 1. Must be owned by root
171 * 2. Must not be writable by group
172 * 3. Must not be setuid
173 * 4. Must not be writable by others
176 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
177 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
178 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
179 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
181 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
185 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
186 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
190 * Now check the containing directory...
193 strlcpy(temp
, filename
, sizeof(temp
));
194 if ((ptr
= strrchr(temp
, '/')) != NULL
)
202 if (stat(temp
, &fileinfo
))
208 result
= _CUPS_FILE_CHECK_MISSING
;
209 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
215 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
216 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
217 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
218 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
220 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
221 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
226 * Common return point...
233 cups_lang_t
*lang
= cupsLangDefault();
234 /* Localization information */
238 case _CUPS_FILE_CHECK_OK
:
239 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
240 snprintf(message
, sizeof(message
),
241 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
242 "(0%o/uid=%d/gid=%d).")),
243 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
244 (int)fileinfo
.st_gid
);
246 snprintf(message
, sizeof(message
),
247 _cupsLangString(lang
, _("File \"%s\" permissions OK "
248 "(0%o/uid=%d/gid=%d).")),
249 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
250 (int)fileinfo
.st_gid
);
253 case _CUPS_FILE_CHECK_MISSING
:
254 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
255 snprintf(message
, sizeof(message
),
256 _cupsLangString(lang
, _("Directory \"%s\" not available: "
258 filename
, strerror(errno
));
260 snprintf(message
, sizeof(message
),
261 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
262 filename
, strerror(errno
));
265 case _CUPS_FILE_CHECK_PERMISSIONS
:
266 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
267 snprintf(message
, sizeof(message
),
268 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
270 "(0%o/uid=%d/gid=%d).")),
271 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
272 (int)fileinfo
.st_gid
);
274 snprintf(message
, sizeof(message
),
275 _cupsLangString(lang
, _("File \"%s\" has insecure "
277 "(0%o/uid=%d/gid=%d).")),
278 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
279 (int)fileinfo
.st_gid
);
282 case _CUPS_FILE_CHECK_WRONG_TYPE
:
283 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
284 snprintf(message
, sizeof(message
),
285 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
288 snprintf(message
, sizeof(message
),
289 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
293 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
294 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
295 snprintf(message
, sizeof(message
),
296 _cupsLangString(lang
, _("Directory \"%s\" contains a "
297 "relative path.")), filename
);
299 snprintf(message
, sizeof(message
),
300 _cupsLangString(lang
, _("File \"%s\" contains a relative "
301 "path.")), filename
);
305 (*cb
)(context
, result
, message
);
313 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
317 _cupsFileCheckFilter(
318 void *context
, /* I - Context pointer (unused) */
319 _cups_fc_result_t result
, /* I - Result code */
320 const char *message
) /* I - Message text */
322 const char *prefix
; /* Messaging prefix */
330 case _CUPS_FILE_CHECK_OK
:
334 case _CUPS_FILE_CHECK_MISSING
:
335 case _CUPS_FILE_CHECK_WRONG_TYPE
:
337 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
340 case _CUPS_FILE_CHECK_PERMISSIONS
:
341 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
343 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
347 fprintf(stderr
, "%s: %s\n", prefix
, message
);
353 * 'cupsFileClose()' - Close a CUPS file.
355 * @since CUPS 1.2/Mac OS X 10.5@
358 int /* O - 0 on success, -1 on error */
359 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
361 int fd
; /* File descriptor */
362 char mode
; /* Open mode */
363 int status
; /* Return status */
364 int is_stdio
; /* Is a stdio file? */
367 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
377 * Flush pending write data...
381 status
= cupsFileFlush(fp
);
386 if (fp
->compressed
&& status
>= 0)
391 * Free decompression data...
394 inflateEnd(&fp
->stream
);
399 * Flush any remaining compressed data...
402 unsigned char trailer
[8]; /* Trailer CRC and length */
403 int done
; /* Done writing... */
406 fp
->stream
.avail_in
= 0;
410 if (fp
->stream
.next_out
> fp
->cbuf
)
412 if (cups_write(fp
, (char *)fp
->cbuf
,
413 fp
->stream
.next_out
- fp
->cbuf
) < 0)
416 fp
->stream
.next_out
= fp
->cbuf
;
417 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
420 if (done
|| status
< 0)
423 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
424 fp
->stream
.next_out
== fp
->cbuf
;
428 * Write the CRC and length...
431 trailer
[0] = fp
->crc
;
432 trailer
[1] = fp
->crc
>> 8;
433 trailer
[2] = fp
->crc
>> 16;
434 trailer
[3] = fp
->crc
>> 24;
435 trailer
[4] = fp
->pos
;
436 trailer
[5] = fp
->pos
>> 8;
437 trailer
[6] = fp
->pos
>> 16;
438 trailer
[7] = fp
->pos
>> 24;
440 if (cups_write(fp
, (char *)trailer
, 8) < 0)
444 * Free all memory used by the compression stream...
447 deflateEnd(&(fp
->stream
));
450 #endif /* HAVE_LIBZ */
453 * Save the file descriptor we used and free memory...
458 is_stdio
= fp
->is_stdio
;
460 if (fp
->printf_buffer
)
461 free(fp
->printf_buffer
);
466 * Close the file, returning the close status...
471 if (closesocket(fd
) < 0)
485 * 'cupsFileCompression()' - Return whether a file is compressed.
487 * @since CUPS 1.2/Mac OS X 10.5@
490 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
491 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
493 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
498 * 'cupsFileEOF()' - Return the end-of-file status.
500 * @since CUPS 1.2/Mac OS X 10.5@
503 int /* O - 1 on end of file, 0 otherwise */
504 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
506 return (fp
? fp
->eof
: 1);
511 * 'cupsFileFind()' - Find a file using the specified path.
513 * This function allows the paths in the path string to be separated by
514 * colons (UNIX standard) or semicolons (Windows standard) and stores the
515 * result in the buffer supplied. If the file cannot be found in any of
516 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
517 * matches the current directory.
519 * @since CUPS 1.2/Mac OS X 10.5@
522 const char * /* O - Full path to file or @code NULL@ if not found */
523 cupsFileFind(const char *filename
, /* I - File to find */
524 const char *path
, /* I - Colon/semicolon-separated path */
525 int executable
, /* I - 1 = executable files, 0 = any file/dir */
526 char *buffer
, /* I - Filename buffer */
527 int bufsize
) /* I - Size of filename buffer */
529 char *bufptr
, /* Current position in buffer */
530 *bufend
; /* End of buffer */
534 * Range check input...
537 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
538 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
541 if (!filename
|| !buffer
|| bufsize
< 2)
547 * No path, so check current directory...
550 if (!access(filename
, 0))
552 strlcpy(buffer
, filename
, bufsize
);
560 * Now check each path and return the first match...
563 bufend
= buffer
+ bufsize
- 1;
569 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
571 if (*path
== ';' || *path
== ':')
574 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
577 strlcpy(bufptr
, filename
, bufend
- bufptr
);
580 if (!access(buffer
, 0))
582 if (!access(buffer
, executable
? X_OK
: 0))
585 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
591 else if (bufptr
< bufend
)
598 * Check the last path...
601 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
604 strlcpy(bufptr
, filename
, bufend
- bufptr
);
606 if (!access(buffer
, 0))
608 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
613 DEBUG_puts("1cupsFileFind: Returning NULL");
620 * 'cupsFileFlush()' - Flush pending output.
622 * @since CUPS 1.2/Mac OS X 10.5@
625 int /* O - 0 on success, -1 on error */
626 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
628 ssize_t bytes
; /* Bytes to write */
631 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
634 * Range check input...
637 if (!fp
|| fp
->mode
!= 'w')
639 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
643 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
645 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
652 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
654 #endif /* HAVE_LIBZ */
655 bytes
= cups_write(fp
, fp
->buf
, bytes
);
668 * 'cupsFileGetChar()' - Get a single character from a file.
670 * @since CUPS 1.2/Mac OS X 10.5@
673 int /* O - Character or -1 on end of file */
674 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
677 * Range check input...
680 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
682 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
687 * If the input buffer is empty, try to read more data...
690 if (fp
->ptr
>= fp
->end
)
691 if (cups_fill(fp
) < 0)
693 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
698 * Return the next character in the buffer...
701 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
705 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
707 return (*(fp
->ptr
)++ & 255);
712 * 'cupsFileGetConf()' - Get a line from a configuration file.
714 * @since CUPS 1.2/Mac OS X 10.5@
717 char * /* O - Line read or @code NULL@ on end of file or error */
718 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
719 char *buf
, /* O - String buffer */
720 size_t buflen
, /* I - Size of string buffer */
721 char **value
, /* O - Pointer to value */
722 int *linenum
) /* IO - Current line number */
724 char *ptr
; /* Pointer into line */
728 * Range check input...
731 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
732 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
735 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
736 !buf
|| buflen
< 2 || !value
)
745 * Read the next non-comment line...
750 while (cupsFileGets(fp
, buf
, buflen
))
755 * Strip any comments...
758 if ((ptr
= strchr(buf
, '#')) != NULL
)
760 if (ptr
> buf
&& ptr
[-1] == '\\')
763 _cups_strcpy(ptr
- 1, ptr
);
767 // Strip the comment and any trailing whitespace...
770 if (!_cups_isspace(ptr
[-1]))
781 * Strip leading whitespace...
784 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
787 _cups_strcpy(buf
, ptr
);
790 * See if there is anything left...
796 * Yes, grab any value and return...
799 for (ptr
= buf
; *ptr
; ptr
++)
800 if (_cups_isspace(*ptr
))
806 * Have a value, skip any other spaces...
809 while (_cups_isspace(*ptr
))
816 * Strip trailing whitespace and > for lines that begin with <...
819 ptr
+= strlen(ptr
) - 1;
821 if (buf
[0] == '<' && *ptr
== '>')
823 else if (buf
[0] == '<' && *ptr
!= '>')
833 while (ptr
> *value
&& _cups_isspace(*ptr
))
850 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
851 * contain binary data.
853 * This function differs from @link cupsFileGets@ in that the trailing CR
854 * and LF are preserved, as is any binary data on the line. The buffer is
855 * nul-terminated, however you should use the returned length to determine
856 * the number of bytes on the line.
858 * @since CUPS 1.2/Mac OS X 10.5@
861 size_t /* O - Number of bytes on line or 0 on end of file */
862 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
863 char *buf
, /* I - Buffer */
864 size_t buflen
) /* I - Size of buffer */
866 int ch
; /* Character from file */
867 char *ptr
, /* Current position in line buffer */
868 *end
; /* End of line buffer */
872 * Range check input...
875 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
876 fp
, buf
, CUPS_LLCAST buflen
));
878 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
882 * Now loop until we have a valid line...
885 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
887 if (fp
->ptr
>= fp
->end
)
888 if (cups_fill(fp
) <= 0)
891 *ptr
++ = ch
= *(fp
->ptr
)++;
900 if (fp
->ptr
>= fp
->end
)
901 if (cups_fill(fp
) <= 0)
904 if (*(fp
->ptr
) == '\n')
906 *ptr
++ = *(fp
->ptr
)++;
915 * Line feed ends a line...
924 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
931 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
933 * @since CUPS 1.2/Mac OS X 10.5@
936 char * /* O - Line read or @code NULL@ on end of file or error */
937 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
938 char *buf
, /* O - String buffer */
939 size_t buflen
) /* I - Size of string buffer */
941 int ch
; /* Character from file */
942 char *ptr
, /* Current position in line buffer */
943 *end
; /* End of line buffer */
947 * Range check input...
950 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
951 CUPS_LLCAST buflen
));
953 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
957 * Now loop until we have a valid line...
960 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
962 if (fp
->ptr
>= fp
->end
)
963 if (cups_fill(fp
) <= 0)
980 if (fp
->ptr
>= fp
->end
)
981 if (cups_fill(fp
) <= 0)
984 if (*(fp
->ptr
) == '\n')
995 * Line feed ends a line...
1006 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1013 * 'cupsFileLock()' - Temporarily lock access to a file.
1015 * @since CUPS 1.2/Mac OS X 10.5@
1018 int /* O - 0 on success, -1 on error */
1019 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
1020 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
1026 if (!fp
|| fp
->mode
== 's')
1034 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1036 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1042 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1044 * @since CUPS 1.2/Mac OS X 10.5@
1047 int /* O - File descriptor */
1048 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1058 * 'cupsFileOpen()' - Open a CUPS file.
1060 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1061 * existing file, "a" to append to an existing file or create a new file,
1062 * or "s" to open a socket connection.
1064 * When opening for writing ("w"), an optional number from 1 to 9 can be
1065 * supplied which enables Flate compression of the file. Compression is
1066 * not supported for the "a" (append) mode.
1068 * When opening a socket connection, the filename is a string of the form
1069 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1070 * connection as needed, generally preferring IPv6 connections when there is
1073 * @since CUPS 1.2/Mac OS X 10.5@
1076 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1077 cupsFileOpen(const char *filename
, /* I - Name of file */
1078 const char *mode
) /* I - Open mode */
1080 cups_file_t
*fp
; /* New CUPS file */
1081 int fd
; /* File descriptor */
1082 char hostname
[1024], /* Hostname */
1083 *portname
; /* Port "name" (number or service) */
1084 http_addrlist_t
*addrlist
; /* Host address list */
1087 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1091 * Range check input...
1094 if (!filename
|| !mode
||
1095 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1096 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1105 case 'a' : /* Append file */
1106 fd
= cups_open(filename
,
1107 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1110 case 'r' : /* Read file */
1111 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1114 case 'w' : /* Write file */
1115 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1116 if (fd
< 0 && errno
== ENOENT
)
1118 fd
= cups_open(filename
,
1119 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1120 if (fd
< 0 && errno
== EEXIST
)
1121 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1132 case 's' : /* Read/write socket */
1133 strlcpy(hostname
, filename
, sizeof(hostname
));
1134 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1140 * Lookup the hostname and service...
1143 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1147 * Connect to the server...
1150 if (!httpAddrConnect(addrlist
, &fd
))
1152 httpAddrFreeList(addrlist
);
1156 httpAddrFreeList(addrlist
);
1159 default : /* Remove bogus compiler warning... */
1167 * Create the CUPS file structure...
1170 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1186 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1188 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1189 * or "s" to treat the file descriptor as a bidirectional socket connection.
1191 * When opening for writing ("w"), an optional number from 1 to 9 can be
1192 * supplied which enables Flate compression of the file. Compression is
1193 * not supported for the "a" (append) mode.
1195 * @since CUPS 1.2/Mac OS X 10.5@
1198 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1199 cupsFileOpenFd(int fd
, /* I - File descriptor */
1200 const char *mode
) /* I - Open mode */
1202 cups_file_t
*fp
; /* New CUPS file */
1205 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1208 * Range check input...
1211 if (fd
< 0 || !mode
||
1212 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1213 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1217 * Allocate memory...
1220 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1232 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1237 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1240 if (mode
[1] >= '1' && mode
[1] <= '9')
1243 * Open a compressed stream, so write the standard gzip file
1247 unsigned char header
[10]; /* gzip file header */
1248 time_t curtime
; /* Current time */
1251 curtime
= time(NULL
);
1254 header
[2] = Z_DEFLATED
;
1256 header
[4] = curtime
;
1257 header
[5] = curtime
>> 8;
1258 header
[6] = curtime
>> 16;
1259 header
[7] = curtime
>> 24;
1263 cups_write(fp
, (char *)header
, 10);
1266 * Initialize the compressor...
1269 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1270 Z_DEFAULT_STRATEGY
);
1272 fp
->stream
.next_out
= fp
->cbuf
;
1273 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1275 fp
->crc
= crc32(0L, Z_NULL
, 0);
1277 #endif /* HAVE_LIBZ */
1288 default : /* Remove bogus compiler warning... */
1293 * Don't pass this file to child processes...
1297 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1305 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1307 * @since CUPS 1.2/Mac OS X 10.5@
1310 int /* O - Character or -1 on end of file */
1311 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1314 * Range check input...
1317 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1321 * If the input buffer is empty, try to read more data...
1324 if (fp
->ptr
>= fp
->end
)
1325 if (cups_fill(fp
) < 0)
1329 * Return the next character in the buffer...
1332 return (*(fp
->ptr
) & 255);
1337 * 'cupsFilePrintf()' - Write a formatted string.
1339 * @since CUPS 1.2/Mac OS X 10.5@
1342 int /* O - Number of bytes written or -1 on error */
1343 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1344 const char *format
, /* I - Printf-style format string */
1345 ...) /* I - Additional args as necessary */
1347 va_list ap
; /* Argument list */
1348 ssize_t bytes
; /* Formatted size */
1351 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1353 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1356 if (!fp
->printf_buffer
)
1359 * Start with an 1k printf buffer...
1362 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1365 fp
->printf_size
= 1024;
1368 va_start(ap
, format
);
1369 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1372 if (bytes
>= (ssize_t
)fp
->printf_size
)
1375 * Expand the printf buffer...
1378 char *temp
; /* Temporary buffer pointer */
1384 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1387 fp
->printf_buffer
= temp
;
1388 fp
->printf_size
= bytes
+ 1;
1390 va_start(ap
, format
);
1391 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1395 if (fp
->mode
== 's')
1397 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1402 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1407 if ((fp
->ptr
+ bytes
) > fp
->end
)
1408 if (cupsFileFlush(fp
))
1413 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1415 if (bytes
> sizeof(fp
->buf
))
1419 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1421 #endif /* HAVE_LIBZ */
1422 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1426 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1434 * 'cupsFilePutChar()' - Write a character.
1436 * @since CUPS 1.2/Mac OS X 10.5@
1439 int /* O - 0 on success, -1 on error */
1440 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1441 int c
) /* I - Character to write */
1444 * Range check input...
1447 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1450 if (fp
->mode
== 's')
1453 * Send character immediately over socket...
1456 char ch
; /* Output character */
1461 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1470 if (fp
->ptr
>= fp
->end
)
1471 if (cupsFileFlush(fp
))
1479 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1486 * 'cupsFilePutConf()' - Write a configuration line.
1488 * This function handles any comment escaping of the value.
1490 * @since CUPS 1.4/Mac OS X 10.6@
1493 ssize_t
/* O - Number of bytes written or -1 on error */
1494 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1495 const char *directive
, /* I - Directive */
1496 const char *value
) /* I - Value */
1498 ssize_t bytes
, /* Number of bytes written */
1499 temp
; /* Temporary byte count */
1500 const char *ptr
; /* Pointer into value */
1503 if (!fp
|| !directive
|| !*directive
)
1506 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1509 if (cupsFilePutChar(fp
, ' ') < 0)
1513 if (value
&& *value
)
1515 if ((ptr
= strchr(value
, '#')) != NULL
)
1518 * Need to quote the first # in the info string...
1521 if ((temp
= cupsFileWrite(fp
, value
, ptr
- value
)) < 0)
1525 if (cupsFilePutChar(fp
, '\\') < 0)
1529 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1533 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1539 if (cupsFilePutChar(fp
, '\n') < 0)
1547 * 'cupsFilePuts()' - Write a string.
1549 * Like the @code fputs@ function, no newline is appended to the string.
1551 * @since CUPS 1.2/Mac OS X 10.5@
1554 int /* O - Number of bytes written or -1 on error */
1555 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1556 const char *s
) /* I - String to write */
1558 ssize_t bytes
; /* Bytes to write */
1562 * Range check input...
1565 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1569 * Write the string...
1572 bytes
= (int)strlen(s
);
1574 if (fp
->mode
== 's')
1576 if (cups_write(fp
, s
, bytes
) < 0)
1581 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1586 if ((fp
->ptr
+ bytes
) > fp
->end
)
1587 if (cupsFileFlush(fp
))
1592 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1594 if (bytes
> sizeof(fp
->buf
))
1598 return (cups_compress(fp
, s
, bytes
));
1600 #endif /* HAVE_LIBZ */
1601 return (cups_write(fp
, s
, bytes
));
1605 memcpy(fp
->ptr
, s
, bytes
);
1613 * 'cupsFileRead()' - Read from a file.
1615 * @since CUPS 1.2/Mac OS X 10.5@
1618 ssize_t
/* O - Number of bytes read or -1 on error */
1619 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1620 char *buf
, /* O - Buffer */
1621 size_t bytes
) /* I - Number of bytes to read */
1623 size_t total
; /* Total bytes read */
1624 ssize_t count
; /* Bytes read */
1627 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1628 CUPS_LLCAST bytes
));
1631 * Range check input...
1634 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1641 * Loop until all bytes are read...
1647 if (fp
->ptr
>= fp
->end
)
1648 if (cups_fill(fp
) <= 0)
1650 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1651 CUPS_LLFMT
, CUPS_LLCAST total
));
1654 return ((ssize_t
)total
);
1659 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1660 if (count
> (ssize_t
)bytes
)
1661 count
= (ssize_t
)bytes
;
1663 memcpy(buf
, fp
->ptr
, count
);
1667 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1670 * Update the counts for the last read...
1679 * Return the total number of bytes read...
1682 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1684 return ((ssize_t
)total
);
1689 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1692 * @since CUPS 1.2/Mac OS X 10.5@
1695 off_t
/* O - New file position or -1 on error */
1696 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1699 * Range check input...
1702 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1703 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1705 if (!fp
|| fp
->mode
!= 'r')
1709 * Handle special cases...
1712 if (fp
->bufpos
== 0)
1715 * No seeking necessary...
1726 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1732 * Otherwise, seek in the file and cleanup any compression buffers...
1738 inflateEnd(&fp
->stream
);
1741 #endif /* HAVE_LIBZ */
1743 if (lseek(fp
->fd
, 0, SEEK_SET
))
1745 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1755 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1762 * 'cupsFileSeek()' - Seek in a file.
1764 * @since CUPS 1.2/Mac OS X 10.5@
1767 off_t
/* O - New file position or -1 on error */
1768 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1769 off_t pos
) /* I - Position in file */
1771 ssize_t bytes
; /* Number bytes in buffer */
1774 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1776 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1777 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1780 * Range check input...
1783 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1787 * Handle special cases...
1791 return (cupsFileRewind(fp
));
1795 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1797 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1799 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1802 * No seeking necessary...
1806 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1814 if (!fp
->compressed
&& !fp
->ptr
)
1817 * Preload a buffer to determine whether the file is compressed...
1820 if (cups_fill(fp
) < 0)
1823 #endif /* HAVE_LIBZ */
1826 * Seek forwards or backwards...
1831 if (pos
< fp
->bufpos
)
1834 * Need to seek backwards...
1837 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1842 inflateEnd(&fp
->stream
);
1844 lseek(fp
->fd
, 0, SEEK_SET
);
1850 while ((bytes
= cups_fill(fp
)) > 0)
1851 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1857 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1861 #endif /* HAVE_LIBZ */
1863 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1864 fp
->pos
= fp
->bufpos
;
1868 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1869 CUPS_LLCAST fp
->pos
));
1875 * Need to seek forwards...
1878 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1883 while ((bytes
= cups_fill(fp
)) > 0)
1885 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1892 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1896 #endif /* HAVE_LIBZ */
1898 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1899 fp
->pos
= fp
->bufpos
;
1903 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1904 CUPS_LLCAST fp
->pos
));
1908 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1915 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1917 * @since CUPS 1.2/Mac OS X 10.5@
1920 cups_file_t
* /* O - CUPS file */
1921 cupsFileStderr(void)
1923 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1927 * Open file descriptor 2 as needed...
1930 if (!cg
->stdio_files
[2])
1933 * Flush any pending output on the stdio file...
1939 * Open file descriptor 2...
1942 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1943 cg
->stdio_files
[2]->is_stdio
= 1;
1946 return (cg
->stdio_files
[2]);
1951 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1953 * @since CUPS 1.2/Mac OS X 10.5@
1956 cups_file_t
* /* O - CUPS file */
1959 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1963 * Open file descriptor 0 as needed...
1966 if (!cg
->stdio_files
[0])
1969 * Open file descriptor 0...
1972 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1973 cg
->stdio_files
[0]->is_stdio
= 1;
1976 return (cg
->stdio_files
[0]);
1981 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1983 * @since CUPS 1.2/Mac OS X 10.5@
1986 cups_file_t
* /* O - CUPS file */
1987 cupsFileStdout(void)
1989 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1993 * Open file descriptor 1 as needed...
1996 if (!cg
->stdio_files
[1])
1999 * Flush any pending output on the stdio file...
2005 * Open file descriptor 1...
2008 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
2009 cg
->stdio_files
[1]->is_stdio
= 1;
2012 return (cg
->stdio_files
[1]);
2017 * 'cupsFileTell()' - Return the current file position.
2019 * @since CUPS 1.2/Mac OS X 10.5@
2022 off_t
/* O - File position */
2023 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
2025 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
2026 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
2027 CUPS_LLCAST (fp
? fp
->pos
: -1)));
2029 return (fp
? fp
->pos
: 0);
2034 * 'cupsFileUnlock()' - Unlock access to a file.
2036 * @since CUPS 1.2/Mac OS X 10.5@
2039 int /* O - 0 on success, -1 on error */
2040 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2046 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2048 if (!fp
|| fp
->mode
== 's')
2056 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2058 return (lockf(fp
->fd
, F_ULOCK
, 0));
2064 * 'cupsFileWrite()' - Write to a file.
2066 * @since CUPS 1.2/Mac OS X 10.5@
2069 ssize_t
/* O - Number of bytes written or -1 on error */
2070 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2071 const char *buf
, /* I - Buffer */
2072 size_t bytes
) /* I - Number of bytes to write */
2075 * Range check input...
2078 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2079 fp
, buf
, CUPS_LLCAST bytes
));
2081 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2088 * Write the buffer...
2091 if (fp
->mode
== 's')
2093 if (cups_write(fp
, buf
, bytes
) < 0)
2096 fp
->pos
+= (off_t
)bytes
;
2098 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2100 return ((ssize_t
)bytes
);
2103 if ((fp
->ptr
+ bytes
) > fp
->end
)
2104 if (cupsFileFlush(fp
))
2107 fp
->pos
+= (off_t
)bytes
;
2109 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2111 if (bytes
> sizeof(fp
->buf
))
2115 return (cups_compress(fp
, buf
, bytes
));
2117 #endif /* HAVE_LIBZ */
2118 return (cups_write(fp
, buf
, bytes
));
2122 memcpy(fp
->ptr
, buf
, bytes
);
2124 return ((ssize_t
)bytes
);
2131 * 'cups_compress()' - Compress a buffer of data.
2134 static ssize_t
/* O - Number of bytes written or -1 */
2135 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2136 const char *buf
, /* I - Buffer */
2137 size_t bytes
) /* I - Number bytes */
2139 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2140 CUPS_LLCAST bytes
));
2146 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
2149 * Deflate the bytes...
2152 fp
->stream
.next_in
= (Bytef
*)buf
;
2153 fp
->stream
.avail_in
= bytes
;
2155 while (fp
->stream
.avail_in
> 0)
2158 * Flush the current buffer...
2161 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2162 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2164 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
2166 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
2169 fp
->stream
.next_out
= fp
->cbuf
;
2170 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2173 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2178 #endif /* HAVE_LIBZ */
2182 * 'cups_fill()' - Fill the input buffer.
2185 static ssize_t
/* O - Number of bytes or -1 */
2186 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2188 ssize_t bytes
; /* Number of bytes read */
2190 int status
; /* Decompression status */
2191 const unsigned char *ptr
, /* Pointer into buffer */
2192 *end
; /* End of buffer */
2193 #endif /* HAVE_LIBZ */
2196 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2197 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2198 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2199 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2201 if (fp
->ptr
&& fp
->end
)
2202 fp
->bufpos
+= fp
->end
- fp
->buf
;
2205 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2207 while (!fp
->ptr
|| fp
->compressed
)
2210 * Check to see if we have read any data yet; if not, see if we have a
2211 * compressed file...
2217 * Reset the file position in case we are seeking...
2223 * Read the first bytes in the file to determine if we have a gzip'd
2227 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2230 * Can't read from file!
2233 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2234 CUPS_LLCAST bytes
));
2239 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2240 (fp
->buf
[1] & 255) != 0x8b ||
2241 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2244 * Not a gzip'd file!
2248 fp
->end
= fp
->buf
+ bytes
;
2250 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2251 CUPS_LLCAST bytes
));
2257 * Parse header junk: extra data, original name, and comment...
2260 ptr
= (unsigned char *)fp
->buf
+ 10;
2261 end
= (unsigned char *)fp
->buf
+ bytes
;
2263 if (fp
->buf
[3] & 0x04)
2266 * Skip extra data...
2269 if ((ptr
+ 2) > end
)
2272 * Can't read from file!
2278 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2284 * Can't read from file!
2291 if (fp
->buf
[3] & 0x08)
2294 * Skip original name data...
2297 while (ptr
< end
&& *ptr
)
2305 * Can't read from file!
2312 if (fp
->buf
[3] & 0x10)
2315 * Skip comment data...
2318 while (ptr
< end
&& *ptr
)
2326 * Can't read from file!
2333 if (fp
->buf
[3] & 0x02)
2336 * Skip header CRC data...
2344 * Can't read from file!
2352 * Copy the flate-compressed data to the compression buffer...
2355 if ((bytes
= end
- ptr
) > 0)
2356 memcpy(fp
->cbuf
, ptr
, bytes
);
2359 * Setup the decompressor data...
2362 fp
->stream
.zalloc
= (alloc_func
)0;
2363 fp
->stream
.zfree
= (free_func
)0;
2364 fp
->stream
.opaque
= (voidpf
)0;
2365 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2366 fp
->stream
.next_out
= NULL
;
2367 fp
->stream
.avail_in
= bytes
;
2368 fp
->stream
.avail_out
= 0;
2369 fp
->crc
= crc32(0L, Z_NULL
, 0);
2371 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2380 * If we have reached end-of-file, return immediately...
2387 * Fill the decompression buffer as needed...
2390 if (fp
->stream
.avail_in
== 0)
2392 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2395 fp
->stream
.next_in
= fp
->cbuf
;
2396 fp
->stream
.avail_in
= bytes
;
2400 * Decompress data from the buffer...
2403 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2404 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2406 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2408 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2409 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2410 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2412 if (status
== Z_STREAM_END
)
2415 * Read the CRC and length...
2418 unsigned char trailer
[8]; /* Trailer bytes */
2419 uLong tcrc
; /* Trailer CRC */
2422 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2425 * Can't get it, so mark end-of-file...
2432 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2435 if (tcrc
!= fp
->crc
)
2438 * Bad CRC, mark end-of-file...
2441 DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
2442 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2450 * Otherwise, reset the compressed flag so that we re-read the
2458 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2461 * Return the decompressed data...
2465 fp
->end
= fp
->buf
+ bytes
;
2471 #endif /* HAVE_LIBZ */
2474 * Read a buffer's full of data...
2477 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2480 * Can't read from file!
2491 * Return the bytes we read...
2496 fp
->end
= fp
->buf
+ bytes
;
2503 * 'cups_open()' - Safely open a file for writing.
2505 * We don't allow appending to directories or files that are hard-linked or
2509 static int /* O - File descriptor or -1 otherwise */
2510 cups_open(const char *filename
, /* I - Filename */
2511 int mode
) /* I - Open mode */
2513 int fd
; /* File descriptor */
2514 struct stat fileinfo
; /* File information */
2516 struct stat linkinfo
; /* Link information */
2524 if ((fd
= open(filename
, mode
, 0666)) < 0)
2528 * Then verify that the file descriptor doesn't point to a directory or hard-
2532 if (fstat(fd
, &fileinfo
))
2538 if (fileinfo
.st_nlink
!= 1)
2546 if (fileinfo
.st_mode
& _S_IFDIR
)
2548 if (S_ISDIR(fileinfo
.st_mode
))
2558 * Then use lstat to determine whether the filename is a symlink...
2561 if (lstat(filename
, &linkinfo
))
2567 if (S_ISLNK(linkinfo
.st_mode
) ||
2568 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2569 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2571 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2572 #endif /* HAVE_ST_GEN */
2573 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2574 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2591 * 'cups_read()' - Read from a file descriptor.
2594 static ssize_t
/* O - Number of bytes read or -1 */
2595 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2596 char *buf
, /* I - Buffer */
2597 size_t bytes
) /* I - Number bytes */
2599 ssize_t total
; /* Total bytes read */
2602 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2603 CUPS_LLCAST bytes
));
2606 * Loop until we read at least 0 bytes...
2612 if (fp
->mode
== 's')
2613 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2615 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2617 if (fp
->mode
== 's')
2618 total
= recv(fp
->fd
, buf
, bytes
, 0);
2620 total
= read(fp
->fd
, buf
, bytes
);
2623 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2629 * Reads can be interrupted by signals and unavailable resources...
2632 if (errno
== EAGAIN
|| errno
== EINTR
)
2639 * Return the total number of bytes read...
2647 * 'cups_write()' - Write to a file descriptor.
2650 static ssize_t
/* O - Number of bytes written or -1 */
2651 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2652 const char *buf
, /* I - Buffer */
2653 size_t bytes
) /* I - Number bytes */
2655 size_t total
; /* Total bytes written */
2656 ssize_t count
; /* Count this time */
2659 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2660 CUPS_LLCAST bytes
));
2663 * Loop until all bytes are written...
2670 if (fp
->mode
== 's')
2671 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2673 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2675 if (fp
->mode
== 's')
2676 count
= send(fp
->fd
, buf
, bytes
, 0);
2678 count
= write(fp
->fd
, buf
, bytes
);
2681 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2686 * Writes can be interrupted by signals and unavailable resources...
2689 if (errno
== EAGAIN
|| errno
== EINTR
)
2696 * Update the counts for the last write call...
2705 * Return the total number of bytes written...
2708 return ((ssize_t
)total
);
2713 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".