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
);
84 * '_cupsFileCheck()' - Check the permissions of the given filename.
87 _cups_fc_result_t
/* O - Check result */
89 const char *filename
, /* I - Filename to check */
90 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
91 int dorootchecks
, /* I - Check for root permissions? */
92 _cups_fc_func_t cb
, /* I - Callback function */
93 void *context
) /* I - Context pointer for callback */
96 struct stat fileinfo
; /* File information */
97 char message
[1024], /* Message string */
98 temp
[1024], /* Parent directory filename */
99 *ptr
; /* Pointer into parent directory */
100 _cups_fc_result_t result
; /* Check result */
104 * Does the filename contain a relative path ("../")?
107 if (strstr(filename
, "../"))
113 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
118 * Does the program even exist and is it accessible?
121 if (stat(filename
, &fileinfo
))
127 result
= _CUPS_FILE_CHECK_MISSING
;
132 * Check the execute bit...
135 result
= _CUPS_FILE_CHECK_OK
;
139 case _CUPS_FILE_CHECK_DIRECTORY
:
140 if (!S_ISDIR(fileinfo
.st_mode
))
141 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
145 if (!S_ISREG(fileinfo
.st_mode
))
146 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
154 * Are we doing root checks?
160 * Nope, so anything (else) goes...
167 * Verify permission of the file itself:
169 * 1. Must be owned by root
170 * 2. Must not be writable by group unless group is root/wheel/admin
171 * 3. Must not be setuid
172 * 4. Must not be writable by others
175 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
177 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
&&
178 fileinfo
.st_gid
!= 80) || /* 2. Must not be writable by group */
180 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
) ||
181 /* 2. Must not be writable by group */
182 #endif /* __APPLE__ */
183 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
184 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
186 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
190 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
191 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
195 * Now check the containing directory...
198 strlcpy(temp
, filename
, sizeof(temp
));
199 if ((ptr
= strrchr(temp
, '/')) != NULL
)
207 if (stat(temp
, &fileinfo
))
213 result
= _CUPS_FILE_CHECK_MISSING
;
214 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
220 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
222 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
&&
223 fileinfo
.st_gid
!= 80) || /* 2. Must not be writable by group */
225 ((fileinfo
.st_mode
& S_IWGRP
) && fileinfo
.st_gid
) ||
226 /* 2. Must not be writable by group */
227 #endif /* __APPLE__ */
228 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
229 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
231 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
232 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
237 * Common return point...
244 cups_lang_t
*lang
= cupsLangDefault();
245 /* Localization information */
249 case _CUPS_FILE_CHECK_OK
:
250 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
251 snprintf(message
, sizeof(message
),
252 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
253 "(0%o/uid=%d/gid=%d).")),
254 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
255 (int)fileinfo
.st_gid
);
257 snprintf(message
, sizeof(message
),
258 _cupsLangString(lang
, _("File \"%s\" permissions OK "
259 "(0%o/uid=%d/gid=%d).")),
260 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
261 (int)fileinfo
.st_gid
);
264 case _CUPS_FILE_CHECK_MISSING
:
265 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
266 snprintf(message
, sizeof(message
),
267 _cupsLangString(lang
, _("Directory \"%s\" not available: "
269 filename
, strerror(errno
));
271 snprintf(message
, sizeof(message
),
272 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
273 filename
, strerror(errno
));
276 case _CUPS_FILE_CHECK_PERMISSIONS
:
277 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
278 snprintf(message
, sizeof(message
),
279 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
281 "(0%o/uid=%d/gid=%d).")),
282 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
283 (int)fileinfo
.st_gid
);
285 snprintf(message
, sizeof(message
),
286 _cupsLangString(lang
, _("File \"%s\" has insecure "
288 "(0%o/uid=%d/gid=%d).")),
289 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
290 (int)fileinfo
.st_gid
);
293 case _CUPS_FILE_CHECK_WRONG_TYPE
:
294 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
295 snprintf(message
, sizeof(message
),
296 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
299 snprintf(message
, sizeof(message
),
300 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
304 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
305 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
306 snprintf(message
, sizeof(message
),
307 _cupsLangString(lang
, _("Directory \"%s\" contains a "
308 "relative path.")), filename
);
310 snprintf(message
, sizeof(message
),
311 _cupsLangString(lang
, _("File \"%s\" contains a relative "
312 "path.")), filename
);
316 (*cb
)(context
, result
, message
);
324 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
328 _cupsFileCheckFilter(
329 void *context
, /* I - Context pointer (unused) */
330 _cups_fc_result_t result
, /* I - Result code */
331 const char *message
) /* I - Message text */
333 const char *prefix
; /* Messaging prefix */
341 case _CUPS_FILE_CHECK_OK
:
345 case _CUPS_FILE_CHECK_MISSING
:
346 case _CUPS_FILE_CHECK_WRONG_TYPE
:
348 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
351 case _CUPS_FILE_CHECK_PERMISSIONS
:
352 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
354 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
358 fprintf(stderr
, "%s: %s\n", prefix
, message
);
363 * 'cupsFileClose()' - Close a CUPS file.
365 * @since CUPS 1.2/Mac OS X 10.5@
368 int /* O - 0 on success, -1 on error */
369 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
371 int fd
; /* File descriptor */
372 char mode
; /* Open mode */
373 int status
; /* Return status */
374 int is_stdio
; /* Is a stdio file? */
377 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
387 * Flush pending write data...
391 status
= cupsFileFlush(fp
);
396 if (fp
->compressed
&& status
>= 0)
401 * Free decompression data...
404 inflateEnd(&fp
->stream
);
409 * Flush any remaining compressed data...
412 unsigned char trailer
[8]; /* Trailer CRC and length */
413 int done
; /* Done writing... */
416 fp
->stream
.avail_in
= 0;
420 if (fp
->stream
.next_out
> fp
->cbuf
)
422 if (cups_write(fp
, (char *)fp
->cbuf
,
423 fp
->stream
.next_out
- fp
->cbuf
) < 0)
426 fp
->stream
.next_out
= fp
->cbuf
;
427 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
430 if (done
|| status
< 0)
433 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
434 fp
->stream
.next_out
== fp
->cbuf
;
438 * Write the CRC and length...
441 trailer
[0] = fp
->crc
;
442 trailer
[1] = fp
->crc
>> 8;
443 trailer
[2] = fp
->crc
>> 16;
444 trailer
[3] = fp
->crc
>> 24;
445 trailer
[4] = fp
->pos
;
446 trailer
[5] = fp
->pos
>> 8;
447 trailer
[6] = fp
->pos
>> 16;
448 trailer
[7] = fp
->pos
>> 24;
450 if (cups_write(fp
, (char *)trailer
, 8) < 0)
454 * Free all memory used by the compression stream...
457 deflateEnd(&(fp
->stream
));
460 #endif /* HAVE_LIBZ */
463 * Save the file descriptor we used and free memory...
468 is_stdio
= fp
->is_stdio
;
470 if (fp
->printf_buffer
)
471 free(fp
->printf_buffer
);
476 * Close the file, returning the close status...
481 if (closesocket(fd
) < 0)
495 * 'cupsFileCompression()' - Return whether a file is compressed.
497 * @since CUPS 1.2/Mac OS X 10.5@
500 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
501 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
503 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
508 * 'cupsFileEOF()' - Return the end-of-file status.
510 * @since CUPS 1.2/Mac OS X 10.5@
513 int /* O - 1 on end of file, 0 otherwise */
514 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
516 return (fp
? fp
->eof
: 1);
521 * 'cupsFileFind()' - Find a file using the specified path.
523 * This function allows the paths in the path string to be separated by
524 * colons (UNIX standard) or semicolons (Windows standard) and stores the
525 * result in the buffer supplied. If the file cannot be found in any of
526 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
527 * matches the current directory.
529 * @since CUPS 1.2/Mac OS X 10.5@
532 const char * /* O - Full path to file or @code NULL@ if not found */
533 cupsFileFind(const char *filename
, /* I - File to find */
534 const char *path
, /* I - Colon/semicolon-separated path */
535 int executable
, /* I - 1 = executable files, 0 = any file/dir */
536 char *buffer
, /* I - Filename buffer */
537 int bufsize
) /* I - Size of filename buffer */
539 char *bufptr
, /* Current position in buffer */
540 *bufend
; /* End of buffer */
544 * Range check input...
547 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
548 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
551 if (!filename
|| !buffer
|| bufsize
< 2)
557 * No path, so check current directory...
560 if (!access(filename
, 0))
562 strlcpy(buffer
, filename
, bufsize
);
570 * Now check each path and return the first match...
573 bufend
= buffer
+ bufsize
- 1;
579 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
581 if (*path
== ';' || *path
== ':')
584 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
587 strlcpy(bufptr
, filename
, bufend
- bufptr
);
590 if (!access(buffer
, 0))
592 if (!access(buffer
, executable
? X_OK
: 0))
595 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
601 else if (bufptr
< bufend
)
608 * Check the last path...
611 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
614 strlcpy(bufptr
, filename
, bufend
- bufptr
);
616 if (!access(buffer
, 0))
618 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
623 DEBUG_puts("1cupsFileFind: Returning NULL");
630 * 'cupsFileFlush()' - Flush pending output.
632 * @since CUPS 1.2/Mac OS X 10.5@
635 int /* O - 0 on success, -1 on error */
636 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
638 ssize_t bytes
; /* Bytes to write */
641 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
644 * Range check input...
647 if (!fp
|| fp
->mode
!= 'w')
649 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
653 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
655 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
662 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
664 #endif /* HAVE_LIBZ */
665 bytes
= cups_write(fp
, fp
->buf
, bytes
);
678 * 'cupsFileGetChar()' - Get a single character from a file.
680 * @since CUPS 1.2/Mac OS X 10.5@
683 int /* O - Character or -1 on end of file */
684 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
687 * Range check input...
690 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
692 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
697 * If the input buffer is empty, try to read more data...
700 if (fp
->ptr
>= fp
->end
)
701 if (cups_fill(fp
) < 0)
703 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
708 * Return the next character in the buffer...
711 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
715 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
717 return (*(fp
->ptr
)++ & 255);
722 * 'cupsFileGetConf()' - Get a line from a configuration file.
724 * @since CUPS 1.2/Mac OS X 10.5@
727 char * /* O - Line read or @code NULL@ on end of file or error */
728 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
729 char *buf
, /* O - String buffer */
730 size_t buflen
, /* I - Size of string buffer */
731 char **value
, /* O - Pointer to value */
732 int *linenum
) /* IO - Current line number */
734 char *ptr
; /* Pointer into line */
738 * Range check input...
741 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
742 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
745 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
746 !buf
|| buflen
< 2 || !value
)
755 * Read the next non-comment line...
760 while (cupsFileGets(fp
, buf
, buflen
))
765 * Strip any comments...
768 if ((ptr
= strchr(buf
, '#')) != NULL
)
770 if (ptr
> buf
&& ptr
[-1] == '\\')
773 _cups_strcpy(ptr
- 1, ptr
);
777 // Strip the comment and any trailing whitespace...
780 if (!_cups_isspace(ptr
[-1]))
791 * Strip leading whitespace...
794 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
797 _cups_strcpy(buf
, ptr
);
800 * See if there is anything left...
806 * Yes, grab any value and return...
809 for (ptr
= buf
; *ptr
; ptr
++)
810 if (_cups_isspace(*ptr
))
816 * Have a value, skip any other spaces...
819 while (_cups_isspace(*ptr
))
826 * Strip trailing whitespace and > for lines that begin with <...
829 ptr
+= strlen(ptr
) - 1;
831 if (buf
[0] == '<' && *ptr
== '>')
833 else if (buf
[0] == '<' && *ptr
!= '>')
843 while (ptr
> *value
&& _cups_isspace(*ptr
))
860 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
861 * contain binary data.
863 * This function differs from @link cupsFileGets@ in that the trailing CR
864 * and LF are preserved, as is any binary data on the line. The buffer is
865 * nul-terminated, however you should use the returned length to determine
866 * the number of bytes on the line.
868 * @since CUPS 1.2/Mac OS X 10.5@
871 size_t /* O - Number of bytes on line or 0 on end of file */
872 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
873 char *buf
, /* I - Buffer */
874 size_t buflen
) /* I - Size of buffer */
876 int ch
; /* Character from file */
877 char *ptr
, /* Current position in line buffer */
878 *end
; /* End of line buffer */
882 * Range check input...
885 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
886 fp
, buf
, CUPS_LLCAST buflen
));
888 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
892 * Now loop until we have a valid line...
895 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
897 if (fp
->ptr
>= fp
->end
)
898 if (cups_fill(fp
) <= 0)
901 *ptr
++ = ch
= *(fp
->ptr
)++;
910 if (fp
->ptr
>= fp
->end
)
911 if (cups_fill(fp
) <= 0)
914 if (*(fp
->ptr
) == '\n')
916 *ptr
++ = *(fp
->ptr
)++;
925 * Line feed ends a line...
934 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
941 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
943 * @since CUPS 1.2/Mac OS X 10.5@
946 char * /* O - Line read or @code NULL@ on end of file or error */
947 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
948 char *buf
, /* O - String buffer */
949 size_t buflen
) /* I - Size of string buffer */
951 int ch
; /* Character from file */
952 char *ptr
, /* Current position in line buffer */
953 *end
; /* End of line buffer */
957 * Range check input...
960 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
961 CUPS_LLCAST buflen
));
963 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
967 * Now loop until we have a valid line...
970 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
972 if (fp
->ptr
>= fp
->end
)
973 if (cups_fill(fp
) <= 0)
990 if (fp
->ptr
>= fp
->end
)
991 if (cups_fill(fp
) <= 0)
994 if (*(fp
->ptr
) == '\n')
1002 else if (ch
== '\n')
1005 * Line feed ends a line...
1016 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1023 * 'cupsFileLock()' - Temporarily lock access to a file.
1025 * @since CUPS 1.2/Mac OS X 10.5@
1028 int /* O - 0 on success, -1 on error */
1029 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
1030 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
1036 if (!fp
|| fp
->mode
== 's')
1044 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1046 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1052 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1054 * @since CUPS 1.2/Mac OS X 10.5@
1057 int /* O - File descriptor */
1058 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1068 * 'cupsFileOpen()' - Open a CUPS file.
1070 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1071 * existing file, "a" to append to an existing file or create a new file,
1072 * or "s" to open a socket connection.
1074 * When opening for writing ("w"), an optional number from 1 to 9 can be
1075 * supplied which enables Flate compression of the file. Compression is
1076 * not supported for the "a" (append) mode.
1078 * When opening a socket connection, the filename is a string of the form
1079 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1080 * connection as needed, generally preferring IPv6 connections when there is
1083 * @since CUPS 1.2/Mac OS X 10.5@
1086 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1087 cupsFileOpen(const char *filename
, /* I - Name of file */
1088 const char *mode
) /* I - Open mode */
1090 cups_file_t
*fp
; /* New CUPS file */
1091 int fd
; /* File descriptor */
1092 char hostname
[1024], /* Hostname */
1093 *portname
; /* Port "name" (number or service) */
1094 http_addrlist_t
*addrlist
; /* Host address list */
1097 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1101 * Range check input...
1104 if (!filename
|| !mode
||
1105 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1106 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1115 case 'a' : /* Append file */
1116 fd
= cups_open(filename
,
1117 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1120 case 'r' : /* Read file */
1121 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1124 case 'w' : /* Write file */
1125 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1126 if (fd
< 0 && errno
== ENOENT
)
1128 fd
= cups_open(filename
,
1129 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1130 if (fd
< 0 && errno
== EEXIST
)
1131 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1142 case 's' : /* Read/write socket */
1143 strlcpy(hostname
, filename
, sizeof(hostname
));
1144 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1150 * Lookup the hostname and service...
1153 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1157 * Connect to the server...
1160 if (!httpAddrConnect(addrlist
, &fd
))
1162 httpAddrFreeList(addrlist
);
1166 httpAddrFreeList(addrlist
);
1169 default : /* Remove bogus compiler warning... */
1177 * Create the CUPS file structure...
1180 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1196 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1198 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1199 * or "s" to treat the file descriptor as a bidirectional socket connection.
1201 * When opening for writing ("w"), an optional number from 1 to 9 can be
1202 * supplied which enables Flate compression of the file. Compression is
1203 * not supported for the "a" (append) mode.
1205 * @since CUPS 1.2/Mac OS X 10.5@
1208 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1209 cupsFileOpenFd(int fd
, /* I - File descriptor */
1210 const char *mode
) /* I - Open mode */
1212 cups_file_t
*fp
; /* New CUPS file */
1215 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1218 * Range check input...
1221 if (fd
< 0 || !mode
||
1222 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1223 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1227 * Allocate memory...
1230 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1242 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1247 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1250 if (mode
[1] >= '1' && mode
[1] <= '9')
1253 * Open a compressed stream, so write the standard gzip file
1257 unsigned char header
[10]; /* gzip file header */
1258 time_t curtime
; /* Current time */
1261 curtime
= time(NULL
);
1264 header
[2] = Z_DEFLATED
;
1266 header
[4] = curtime
;
1267 header
[5] = curtime
>> 8;
1268 header
[6] = curtime
>> 16;
1269 header
[7] = curtime
>> 24;
1273 cups_write(fp
, (char *)header
, 10);
1276 * Initialize the compressor...
1279 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1280 Z_DEFAULT_STRATEGY
);
1282 fp
->stream
.next_out
= fp
->cbuf
;
1283 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1285 fp
->crc
= crc32(0L, Z_NULL
, 0);
1287 #endif /* HAVE_LIBZ */
1298 default : /* Remove bogus compiler warning... */
1303 * Don't pass this file to child processes...
1307 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1315 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1317 * @since CUPS 1.2/Mac OS X 10.5@
1320 int /* O - Character or -1 on end of file */
1321 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1324 * Range check input...
1327 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1331 * If the input buffer is empty, try to read more data...
1334 if (fp
->ptr
>= fp
->end
)
1335 if (cups_fill(fp
) < 0)
1339 * Return the next character in the buffer...
1342 return (*(fp
->ptr
) & 255);
1347 * 'cupsFilePrintf()' - Write a formatted string.
1349 * @since CUPS 1.2/Mac OS X 10.5@
1352 int /* O - Number of bytes written or -1 on error */
1353 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1354 const char *format
, /* I - Printf-style format string */
1355 ...) /* I - Additional args as necessary */
1357 va_list ap
; /* Argument list */
1358 ssize_t bytes
; /* Formatted size */
1361 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1363 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1366 if (!fp
->printf_buffer
)
1369 * Start with an 1k printf buffer...
1372 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1375 fp
->printf_size
= 1024;
1378 va_start(ap
, format
);
1379 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1382 if (bytes
>= (ssize_t
)fp
->printf_size
)
1385 * Expand the printf buffer...
1388 char *temp
; /* Temporary buffer pointer */
1394 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1397 fp
->printf_buffer
= temp
;
1398 fp
->printf_size
= bytes
+ 1;
1400 va_start(ap
, format
);
1401 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1405 if (fp
->mode
== 's')
1407 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1412 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1417 if ((fp
->ptr
+ bytes
) > fp
->end
)
1418 if (cupsFileFlush(fp
))
1423 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1425 if (bytes
> sizeof(fp
->buf
))
1429 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1431 #endif /* HAVE_LIBZ */
1432 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1436 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1444 * 'cupsFilePutChar()' - Write a character.
1446 * @since CUPS 1.2/Mac OS X 10.5@
1449 int /* O - 0 on success, -1 on error */
1450 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1451 int c
) /* I - Character to write */
1454 * Range check input...
1457 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1460 if (fp
->mode
== 's')
1463 * Send character immediately over socket...
1466 char ch
; /* Output character */
1471 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1480 if (fp
->ptr
>= fp
->end
)
1481 if (cupsFileFlush(fp
))
1489 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1496 * 'cupsFilePutConf()' - Write a configuration line.
1498 * This function handles any comment escaping of the value.
1500 * @since CUPS 1.4/Mac OS X 10.6@
1503 ssize_t
/* O - Number of bytes written or -1 on error */
1504 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1505 const char *directive
, /* I - Directive */
1506 const char *value
) /* I - Value */
1508 ssize_t bytes
, /* Number of bytes written */
1509 temp
; /* Temporary byte count */
1510 const char *ptr
; /* Pointer into value */
1513 if (!fp
|| !directive
|| !*directive
)
1516 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1519 if (cupsFilePutChar(fp
, ' ') < 0)
1523 if (value
&& *value
)
1525 if ((ptr
= strchr(value
, '#')) != NULL
)
1528 * Need to quote the first # in the info string...
1531 if ((temp
= cupsFileWrite(fp
, value
, ptr
- value
)) < 0)
1535 if (cupsFilePutChar(fp
, '\\') < 0)
1539 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1543 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1549 if (cupsFilePutChar(fp
, '\n') < 0)
1557 * 'cupsFilePuts()' - Write a string.
1559 * Like the @code fputs@ function, no newline is appended to the string.
1561 * @since CUPS 1.2/Mac OS X 10.5@
1564 int /* O - Number of bytes written or -1 on error */
1565 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1566 const char *s
) /* I - String to write */
1568 ssize_t bytes
; /* Bytes to write */
1572 * Range check input...
1575 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1579 * Write the string...
1582 bytes
= (int)strlen(s
);
1584 if (fp
->mode
== 's')
1586 if (cups_write(fp
, s
, bytes
) < 0)
1591 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1596 if ((fp
->ptr
+ bytes
) > fp
->end
)
1597 if (cupsFileFlush(fp
))
1602 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1604 if (bytes
> sizeof(fp
->buf
))
1608 return (cups_compress(fp
, s
, bytes
));
1610 #endif /* HAVE_LIBZ */
1611 return (cups_write(fp
, s
, bytes
));
1615 memcpy(fp
->ptr
, s
, bytes
);
1623 * 'cupsFileRead()' - Read from a file.
1625 * @since CUPS 1.2/Mac OS X 10.5@
1628 ssize_t
/* O - Number of bytes read or -1 on error */
1629 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1630 char *buf
, /* O - Buffer */
1631 size_t bytes
) /* I - Number of bytes to read */
1633 size_t total
; /* Total bytes read */
1634 ssize_t count
; /* Bytes read */
1637 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1638 CUPS_LLCAST bytes
));
1641 * Range check input...
1644 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1651 * Loop until all bytes are read...
1657 if (fp
->ptr
>= fp
->end
)
1658 if (cups_fill(fp
) <= 0)
1660 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1661 CUPS_LLFMT
, CUPS_LLCAST total
));
1664 return ((ssize_t
)total
);
1669 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1670 if (count
> (ssize_t
)bytes
)
1671 count
= (ssize_t
)bytes
;
1673 memcpy(buf
, fp
->ptr
, count
);
1677 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1680 * Update the counts for the last read...
1689 * Return the total number of bytes read...
1692 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1694 return ((ssize_t
)total
);
1699 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1702 * @since CUPS 1.2/Mac OS X 10.5@
1705 off_t
/* O - New file position or -1 on error */
1706 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1709 * Range check input...
1712 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1713 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1715 if (!fp
|| fp
->mode
!= 'r')
1719 * Handle special cases...
1722 if (fp
->bufpos
== 0)
1725 * No seeking necessary...
1736 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1742 * Otherwise, seek in the file and cleanup any compression buffers...
1748 inflateEnd(&fp
->stream
);
1751 #endif /* HAVE_LIBZ */
1753 if (lseek(fp
->fd
, 0, SEEK_SET
))
1755 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1765 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1772 * 'cupsFileSeek()' - Seek in a file.
1774 * @since CUPS 1.2/Mac OS X 10.5@
1777 off_t
/* O - New file position or -1 on error */
1778 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1779 off_t pos
) /* I - Position in file */
1781 ssize_t bytes
; /* Number bytes in buffer */
1784 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1786 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1787 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1790 * Range check input...
1793 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1797 * Handle special cases...
1801 return (cupsFileRewind(fp
));
1805 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1807 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1809 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1812 * No seeking necessary...
1816 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1824 if (!fp
->compressed
&& !fp
->ptr
)
1827 * Preload a buffer to determine whether the file is compressed...
1830 if (cups_fill(fp
) < 0)
1833 #endif /* HAVE_LIBZ */
1836 * Seek forwards or backwards...
1841 if (pos
< fp
->bufpos
)
1844 * Need to seek backwards...
1847 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1852 inflateEnd(&fp
->stream
);
1854 lseek(fp
->fd
, 0, SEEK_SET
);
1860 while ((bytes
= cups_fill(fp
)) > 0)
1861 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1867 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1871 #endif /* HAVE_LIBZ */
1873 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1874 fp
->pos
= fp
->bufpos
;
1878 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1879 CUPS_LLCAST fp
->pos
));
1885 * Need to seek forwards...
1888 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1893 while ((bytes
= cups_fill(fp
)) > 0)
1895 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1902 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1906 #endif /* HAVE_LIBZ */
1908 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1909 fp
->pos
= fp
->bufpos
;
1913 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1914 CUPS_LLCAST fp
->pos
));
1918 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1925 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1927 * @since CUPS 1.2/Mac OS X 10.5@
1930 cups_file_t
* /* O - CUPS file */
1931 cupsFileStderr(void)
1933 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1937 * Open file descriptor 2 as needed...
1940 if (!cg
->stdio_files
[2])
1943 * Flush any pending output on the stdio file...
1949 * Open file descriptor 2...
1952 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1953 cg
->stdio_files
[2]->is_stdio
= 1;
1956 return (cg
->stdio_files
[2]);
1961 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1963 * @since CUPS 1.2/Mac OS X 10.5@
1966 cups_file_t
* /* O - CUPS file */
1969 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1973 * Open file descriptor 0 as needed...
1976 if (!cg
->stdio_files
[0])
1979 * Open file descriptor 0...
1982 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1983 cg
->stdio_files
[0]->is_stdio
= 1;
1986 return (cg
->stdio_files
[0]);
1991 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1993 * @since CUPS 1.2/Mac OS X 10.5@
1996 cups_file_t
* /* O - CUPS file */
1997 cupsFileStdout(void)
1999 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
2003 * Open file descriptor 1 as needed...
2006 if (!cg
->stdio_files
[1])
2009 * Flush any pending output on the stdio file...
2015 * Open file descriptor 1...
2018 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
2019 cg
->stdio_files
[1]->is_stdio
= 1;
2022 return (cg
->stdio_files
[1]);
2027 * 'cupsFileTell()' - Return the current file position.
2029 * @since CUPS 1.2/Mac OS X 10.5@
2032 off_t
/* O - File position */
2033 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
2035 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
2036 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
2037 CUPS_LLCAST (fp
? fp
->pos
: -1)));
2039 return (fp
? fp
->pos
: 0);
2044 * 'cupsFileUnlock()' - Unlock access to a file.
2046 * @since CUPS 1.2/Mac OS X 10.5@
2049 int /* O - 0 on success, -1 on error */
2050 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2056 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2058 if (!fp
|| fp
->mode
== 's')
2066 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2068 return (lockf(fp
->fd
, F_ULOCK
, 0));
2074 * 'cupsFileWrite()' - Write to a file.
2076 * @since CUPS 1.2/Mac OS X 10.5@
2079 ssize_t
/* O - Number of bytes written or -1 on error */
2080 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2081 const char *buf
, /* I - Buffer */
2082 size_t bytes
) /* I - Number of bytes to write */
2085 * Range check input...
2088 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2089 fp
, buf
, CUPS_LLCAST bytes
));
2091 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2098 * Write the buffer...
2101 if (fp
->mode
== 's')
2103 if (cups_write(fp
, buf
, bytes
) < 0)
2106 fp
->pos
+= (off_t
)bytes
;
2108 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2110 return ((ssize_t
)bytes
);
2113 if ((fp
->ptr
+ bytes
) > fp
->end
)
2114 if (cupsFileFlush(fp
))
2117 fp
->pos
+= (off_t
)bytes
;
2119 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2121 if (bytes
> sizeof(fp
->buf
))
2125 return (cups_compress(fp
, buf
, bytes
));
2127 #endif /* HAVE_LIBZ */
2128 return (cups_write(fp
, buf
, bytes
));
2132 memcpy(fp
->ptr
, buf
, bytes
);
2134 return ((ssize_t
)bytes
);
2141 * 'cups_compress()' - Compress a buffer of data.
2144 static ssize_t
/* O - Number of bytes written or -1 */
2145 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2146 const char *buf
, /* I - Buffer */
2147 size_t bytes
) /* I - Number bytes */
2149 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2150 CUPS_LLCAST bytes
));
2156 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
2159 * Deflate the bytes...
2162 fp
->stream
.next_in
= (Bytef
*)buf
;
2163 fp
->stream
.avail_in
= bytes
;
2165 while (fp
->stream
.avail_in
> 0)
2168 * Flush the current buffer...
2171 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2172 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2174 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
2176 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
2179 fp
->stream
.next_out
= fp
->cbuf
;
2180 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2183 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2188 #endif /* HAVE_LIBZ */
2192 * 'cups_fill()' - Fill the input buffer.
2195 static ssize_t
/* O - Number of bytes or -1 */
2196 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2198 ssize_t bytes
; /* Number of bytes read */
2200 int status
; /* Decompression status */
2201 const unsigned char *ptr
, /* Pointer into buffer */
2202 *end
; /* End of buffer */
2203 #endif /* HAVE_LIBZ */
2206 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2207 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2208 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2209 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2211 if (fp
->ptr
&& fp
->end
)
2212 fp
->bufpos
+= fp
->end
- fp
->buf
;
2215 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2217 while (!fp
->ptr
|| fp
->compressed
)
2220 * Check to see if we have read any data yet; if not, see if we have a
2221 * compressed file...
2227 * Reset the file position in case we are seeking...
2233 * Read the first bytes in the file to determine if we have a gzip'd
2237 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2240 * Can't read from file!
2243 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2244 CUPS_LLCAST bytes
));
2249 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2250 (fp
->buf
[1] & 255) != 0x8b ||
2251 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2254 * Not a gzip'd file!
2258 fp
->end
= fp
->buf
+ bytes
;
2260 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2261 CUPS_LLCAST bytes
));
2267 * Parse header junk: extra data, original name, and comment...
2270 ptr
= (unsigned char *)fp
->buf
+ 10;
2271 end
= (unsigned char *)fp
->buf
+ bytes
;
2273 if (fp
->buf
[3] & 0x04)
2276 * Skip extra data...
2279 if ((ptr
+ 2) > end
)
2282 * Can't read from file!
2288 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2294 * Can't read from file!
2301 if (fp
->buf
[3] & 0x08)
2304 * Skip original name data...
2307 while (ptr
< end
&& *ptr
)
2315 * Can't read from file!
2322 if (fp
->buf
[3] & 0x10)
2325 * Skip comment data...
2328 while (ptr
< end
&& *ptr
)
2336 * Can't read from file!
2343 if (fp
->buf
[3] & 0x02)
2346 * Skip header CRC data...
2354 * Can't read from file!
2362 * Copy the flate-compressed data to the compression buffer...
2365 if ((bytes
= end
- ptr
) > 0)
2366 memcpy(fp
->cbuf
, ptr
, bytes
);
2369 * Setup the decompressor data...
2372 fp
->stream
.zalloc
= (alloc_func
)0;
2373 fp
->stream
.zfree
= (free_func
)0;
2374 fp
->stream
.opaque
= (voidpf
)0;
2375 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2376 fp
->stream
.next_out
= NULL
;
2377 fp
->stream
.avail_in
= bytes
;
2378 fp
->stream
.avail_out
= 0;
2379 fp
->crc
= crc32(0L, Z_NULL
, 0);
2381 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2390 * If we have reached end-of-file, return immediately...
2397 * Fill the decompression buffer as needed...
2400 if (fp
->stream
.avail_in
== 0)
2402 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2405 fp
->stream
.next_in
= fp
->cbuf
;
2406 fp
->stream
.avail_in
= bytes
;
2410 * Decompress data from the buffer...
2413 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2414 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2416 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2418 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2419 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2420 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2422 if (status
== Z_STREAM_END
)
2425 * Read the CRC and length...
2428 unsigned char trailer
[8]; /* Trailer bytes */
2429 uLong tcrc
; /* Trailer CRC */
2432 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2435 * Can't get it, so mark end-of-file...
2442 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2445 if (tcrc
!= fp
->crc
)
2448 * Bad CRC, mark end-of-file...
2451 DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
2452 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2460 * Otherwise, reset the compressed flag so that we re-read the
2468 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2471 * Return the decompressed data...
2475 fp
->end
= fp
->buf
+ bytes
;
2481 #endif /* HAVE_LIBZ */
2484 * Read a buffer's full of data...
2487 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2490 * Can't read from file!
2501 * Return the bytes we read...
2506 fp
->end
= fp
->buf
+ bytes
;
2513 * 'cups_open()' - Safely open a file for writing.
2515 * We don't allow appending to directories or files that are hard-linked or
2519 static int /* O - File descriptor or -1 otherwise */
2520 cups_open(const char *filename
, /* I - Filename */
2521 int mode
) /* I - Open mode */
2523 int fd
; /* File descriptor */
2524 struct stat fileinfo
; /* File information */
2526 struct stat linkinfo
; /* Link information */
2534 if ((fd
= open(filename
, mode
, 0666)) < 0)
2538 * Then verify that the file descriptor doesn't point to a directory or hard-
2542 if (fstat(fd
, &fileinfo
))
2548 if (fileinfo
.st_nlink
!= 1)
2556 if (fileinfo
.st_mode
& _S_IFDIR
)
2558 if (S_ISDIR(fileinfo
.st_mode
))
2568 * Then use lstat to determine whether the filename is a symlink...
2571 if (lstat(filename
, &linkinfo
))
2577 if (S_ISLNK(linkinfo
.st_mode
) ||
2578 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2579 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2581 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2582 #endif /* HAVE_ST_GEN */
2583 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2584 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2601 * 'cups_read()' - Read from a file descriptor.
2604 static ssize_t
/* O - Number of bytes read or -1 */
2605 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2606 char *buf
, /* I - Buffer */
2607 size_t bytes
) /* I - Number bytes */
2609 ssize_t total
; /* Total bytes read */
2612 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2613 CUPS_LLCAST bytes
));
2616 * Loop until we read at least 0 bytes...
2622 if (fp
->mode
== 's')
2623 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2625 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2627 if (fp
->mode
== 's')
2628 total
= recv(fp
->fd
, buf
, bytes
, 0);
2630 total
= read(fp
->fd
, buf
, bytes
);
2633 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2639 * Reads can be interrupted by signals and unavailable resources...
2642 if (errno
== EAGAIN
|| errno
== EINTR
)
2649 * Return the total number of bytes read...
2657 * 'cups_write()' - Write to a file descriptor.
2660 static ssize_t
/* O - Number of bytes written or -1 */
2661 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2662 const char *buf
, /* I - Buffer */
2663 size_t bytes
) /* I - Number bytes */
2665 size_t total
; /* Total bytes written */
2666 ssize_t count
; /* Count this time */
2669 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2670 CUPS_LLCAST bytes
));
2673 * Loop until all bytes are written...
2680 if (fp
->mode
== 's')
2681 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2683 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2685 if (fp
->mode
== 's')
2686 count
= send(fp
->fd
, buf
, bytes
, 0);
2688 count
= write(fp
->fd
, buf
, bytes
);
2691 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2696 * Writes can be interrupted by signals and unavailable resources...
2699 if (errno
== EAGAIN
|| errno
== EINTR
)
2706 * Update the counts for the last write call...
2715 * Return the total number of bytes written...
2718 return ((ssize_t
)total
);
2723 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".