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
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 */
176 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
177 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
178 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
180 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
184 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
185 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
189 * Now check the containing directory...
192 strlcpy(temp
, filename
, sizeof(temp
));
193 if ((ptr
= strrchr(temp
, '/')) != NULL
)
201 if (stat(temp
, &fileinfo
))
207 result
= _CUPS_FILE_CHECK_MISSING
;
208 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
214 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
215 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
216 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
217 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
219 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
220 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
225 * Common return point...
232 cups_lang_t
*lang
= cupsLangDefault();
233 /* Localization information */
237 case _CUPS_FILE_CHECK_OK
:
238 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
239 snprintf(message
, sizeof(message
),
240 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
241 "(0%o/uid=%d/gid=%d).")),
242 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
243 (int)fileinfo
.st_gid
);
245 snprintf(message
, sizeof(message
),
246 _cupsLangString(lang
, _("File \"%s\" permissions OK "
247 "(0%o/uid=%d/gid=%d).")),
248 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
249 (int)fileinfo
.st_gid
);
252 case _CUPS_FILE_CHECK_MISSING
:
253 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
254 snprintf(message
, sizeof(message
),
255 _cupsLangString(lang
, _("Directory \"%s\" not available: "
257 filename
, strerror(errno
));
259 snprintf(message
, sizeof(message
),
260 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
261 filename
, strerror(errno
));
264 case _CUPS_FILE_CHECK_PERMISSIONS
:
265 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
266 snprintf(message
, sizeof(message
),
267 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
269 "(0%o/uid=%d/gid=%d).")),
270 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
271 (int)fileinfo
.st_gid
);
273 snprintf(message
, sizeof(message
),
274 _cupsLangString(lang
, _("File \"%s\" has insecure "
276 "(0%o/uid=%d/gid=%d).")),
277 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
278 (int)fileinfo
.st_gid
);
281 case _CUPS_FILE_CHECK_WRONG_TYPE
:
282 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
283 snprintf(message
, sizeof(message
),
284 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
287 snprintf(message
, sizeof(message
),
288 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
292 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
293 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
294 snprintf(message
, sizeof(message
),
295 _cupsLangString(lang
, _("Directory \"%s\" contains a "
296 "relative path.")), filename
);
298 snprintf(message
, sizeof(message
),
299 _cupsLangString(lang
, _("File \"%s\" contains a relative "
300 "path.")), filename
);
304 (*cb
)(context
, result
, message
);
312 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
316 _cupsFileCheckFilter(
317 void *context
, /* I - Context pointer (unused) */
318 _cups_fc_result_t result
, /* I - Result code */
319 const char *message
) /* I - Message text */
321 const char *prefix
; /* Messaging prefix */
329 case _CUPS_FILE_CHECK_OK
:
333 case _CUPS_FILE_CHECK_MISSING
:
334 case _CUPS_FILE_CHECK_WRONG_TYPE
:
336 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
339 case _CUPS_FILE_CHECK_PERMISSIONS
:
340 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
342 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
346 fprintf(stderr
, "%s: %s\n", prefix
, message
);
351 * 'cupsFileClose()' - Close a CUPS file.
353 * @since CUPS 1.2/Mac OS X 10.5@
356 int /* O - 0 on success, -1 on error */
357 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
359 int fd
; /* File descriptor */
360 char mode
; /* Open mode */
361 int status
; /* Return status */
362 int is_stdio
; /* Is a stdio file? */
365 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
375 * Flush pending write data...
379 status
= cupsFileFlush(fp
);
384 if (fp
->compressed
&& status
>= 0)
389 * Free decompression data...
392 inflateEnd(&fp
->stream
);
397 * Flush any remaining compressed data...
400 unsigned char trailer
[8]; /* Trailer CRC and length */
401 int done
; /* Done writing... */
404 fp
->stream
.avail_in
= 0;
408 if (fp
->stream
.next_out
> fp
->cbuf
)
410 if (cups_write(fp
, (char *)fp
->cbuf
,
411 fp
->stream
.next_out
- fp
->cbuf
) < 0)
414 fp
->stream
.next_out
= fp
->cbuf
;
415 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
418 if (done
|| status
< 0)
421 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
422 fp
->stream
.next_out
== fp
->cbuf
;
426 * Write the CRC and length...
429 trailer
[0] = fp
->crc
;
430 trailer
[1] = fp
->crc
>> 8;
431 trailer
[2] = fp
->crc
>> 16;
432 trailer
[3] = fp
->crc
>> 24;
433 trailer
[4] = fp
->pos
;
434 trailer
[5] = fp
->pos
>> 8;
435 trailer
[6] = fp
->pos
>> 16;
436 trailer
[7] = fp
->pos
>> 24;
438 if (cups_write(fp
, (char *)trailer
, 8) < 0)
442 * Free all memory used by the compression stream...
445 deflateEnd(&(fp
->stream
));
448 #endif /* HAVE_LIBZ */
451 * Save the file descriptor we used and free memory...
456 is_stdio
= fp
->is_stdio
;
458 if (fp
->printf_buffer
)
459 free(fp
->printf_buffer
);
464 * Close the file, returning the close status...
469 if (closesocket(fd
) < 0)
483 * 'cupsFileCompression()' - Return whether a file is compressed.
485 * @since CUPS 1.2/Mac OS X 10.5@
488 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
489 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
491 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
496 * 'cupsFileEOF()' - Return the end-of-file status.
498 * @since CUPS 1.2/Mac OS X 10.5@
501 int /* O - 1 on end of file, 0 otherwise */
502 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
504 return (fp
? fp
->eof
: 1);
509 * 'cupsFileFind()' - Find a file using the specified path.
511 * This function allows the paths in the path string to be separated by
512 * colons (UNIX standard) or semicolons (Windows standard) and stores the
513 * result in the buffer supplied. If the file cannot be found in any of
514 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
515 * matches the current directory.
517 * @since CUPS 1.2/Mac OS X 10.5@
520 const char * /* O - Full path to file or @code NULL@ if not found */
521 cupsFileFind(const char *filename
, /* I - File to find */
522 const char *path
, /* I - Colon/semicolon-separated path */
523 int executable
, /* I - 1 = executable files, 0 = any file/dir */
524 char *buffer
, /* I - Filename buffer */
525 int bufsize
) /* I - Size of filename buffer */
527 char *bufptr
, /* Current position in buffer */
528 *bufend
; /* End of buffer */
532 * Range check input...
535 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
536 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
539 if (!filename
|| !buffer
|| bufsize
< 2)
545 * No path, so check current directory...
548 if (!access(filename
, 0))
550 strlcpy(buffer
, filename
, bufsize
);
558 * Now check each path and return the first match...
561 bufend
= buffer
+ bufsize
- 1;
567 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
569 if (*path
== ';' || *path
== ':')
572 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
575 strlcpy(bufptr
, filename
, bufend
- bufptr
);
578 if (!access(buffer
, 0))
580 if (!access(buffer
, executable
? X_OK
: 0))
583 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
589 else if (bufptr
< bufend
)
596 * Check the last path...
599 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
602 strlcpy(bufptr
, filename
, bufend
- bufptr
);
604 if (!access(buffer
, 0))
606 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
611 DEBUG_puts("1cupsFileFind: Returning NULL");
618 * 'cupsFileFlush()' - Flush pending output.
620 * @since CUPS 1.2/Mac OS X 10.5@
623 int /* O - 0 on success, -1 on error */
624 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
626 ssize_t bytes
; /* Bytes to write */
629 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
632 * Range check input...
635 if (!fp
|| fp
->mode
!= 'w')
637 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
641 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
643 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
650 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
652 #endif /* HAVE_LIBZ */
653 bytes
= cups_write(fp
, fp
->buf
, bytes
);
666 * 'cupsFileGetChar()' - Get a single character from a file.
668 * @since CUPS 1.2/Mac OS X 10.5@
671 int /* O - Character or -1 on end of file */
672 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
675 * Range check input...
678 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
680 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
685 * If the input buffer is empty, try to read more data...
688 if (fp
->ptr
>= fp
->end
)
689 if (cups_fill(fp
) < 0)
691 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
696 * Return the next character in the buffer...
699 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
703 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
705 return (*(fp
->ptr
)++ & 255);
710 * 'cupsFileGetConf()' - Get a line from a configuration file.
712 * @since CUPS 1.2/Mac OS X 10.5@
715 char * /* O - Line read or @code NULL@ on end of file or error */
716 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
717 char *buf
, /* O - String buffer */
718 size_t buflen
, /* I - Size of string buffer */
719 char **value
, /* O - Pointer to value */
720 int *linenum
) /* IO - Current line number */
722 char *ptr
; /* Pointer into line */
726 * Range check input...
729 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
730 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
733 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
734 !buf
|| buflen
< 2 || !value
)
743 * Read the next non-comment line...
748 while (cupsFileGets(fp
, buf
, buflen
))
753 * Strip any comments...
756 if ((ptr
= strchr(buf
, '#')) != NULL
)
758 if (ptr
> buf
&& ptr
[-1] == '\\')
761 _cups_strcpy(ptr
- 1, ptr
);
765 // Strip the comment and any trailing whitespace...
768 if (!_cups_isspace(ptr
[-1]))
779 * Strip leading whitespace...
782 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
785 _cups_strcpy(buf
, ptr
);
788 * See if there is anything left...
794 * Yes, grab any value and return...
797 for (ptr
= buf
; *ptr
; ptr
++)
798 if (_cups_isspace(*ptr
))
804 * Have a value, skip any other spaces...
807 while (_cups_isspace(*ptr
))
814 * Strip trailing whitespace and > for lines that begin with <...
817 ptr
+= strlen(ptr
) - 1;
819 if (buf
[0] == '<' && *ptr
== '>')
821 else if (buf
[0] == '<' && *ptr
!= '>')
831 while (ptr
> *value
&& _cups_isspace(*ptr
))
848 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
849 * contain binary data.
851 * This function differs from @link cupsFileGets@ in that the trailing CR
852 * and LF are preserved, as is any binary data on the line. The buffer is
853 * nul-terminated, however you should use the returned length to determine
854 * the number of bytes on the line.
856 * @since CUPS 1.2/Mac OS X 10.5@
859 size_t /* O - Number of bytes on line or 0 on end of file */
860 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
861 char *buf
, /* I - Buffer */
862 size_t buflen
) /* I - Size of buffer */
864 int ch
; /* Character from file */
865 char *ptr
, /* Current position in line buffer */
866 *end
; /* End of line buffer */
870 * Range check input...
873 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
874 fp
, buf
, CUPS_LLCAST buflen
));
876 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
880 * Now loop until we have a valid line...
883 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
885 if (fp
->ptr
>= fp
->end
)
886 if (cups_fill(fp
) <= 0)
889 *ptr
++ = ch
= *(fp
->ptr
)++;
898 if (fp
->ptr
>= fp
->end
)
899 if (cups_fill(fp
) <= 0)
902 if (*(fp
->ptr
) == '\n')
904 *ptr
++ = *(fp
->ptr
)++;
913 * Line feed ends a line...
922 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
929 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
931 * @since CUPS 1.2/Mac OS X 10.5@
934 char * /* O - Line read or @code NULL@ on end of file or error */
935 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
936 char *buf
, /* O - String buffer */
937 size_t buflen
) /* I - Size of string buffer */
939 int ch
; /* Character from file */
940 char *ptr
, /* Current position in line buffer */
941 *end
; /* End of line buffer */
945 * Range check input...
948 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
949 CUPS_LLCAST buflen
));
951 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
955 * Now loop until we have a valid line...
958 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
960 if (fp
->ptr
>= fp
->end
)
961 if (cups_fill(fp
) <= 0)
978 if (fp
->ptr
>= fp
->end
)
979 if (cups_fill(fp
) <= 0)
982 if (*(fp
->ptr
) == '\n')
993 * Line feed ends a line...
1004 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1011 * 'cupsFileLock()' - Temporarily lock access to a file.
1013 * @since CUPS 1.2/Mac OS X 10.5@
1016 int /* O - 0 on success, -1 on error */
1017 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
1018 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
1024 if (!fp
|| fp
->mode
== 's')
1032 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1034 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1040 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1042 * @since CUPS 1.2/Mac OS X 10.5@
1045 int /* O - File descriptor */
1046 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1056 * 'cupsFileOpen()' - Open a CUPS file.
1058 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1059 * existing file, "a" to append to an existing file or create a new file,
1060 * or "s" to open a socket connection.
1062 * When opening for writing ("w"), an optional number from 1 to 9 can be
1063 * supplied which enables Flate compression of the file. Compression is
1064 * not supported for the "a" (append) mode.
1066 * When opening a socket connection, the filename is a string of the form
1067 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1068 * connection as needed, generally preferring IPv6 connections when there is
1071 * @since CUPS 1.2/Mac OS X 10.5@
1074 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1075 cupsFileOpen(const char *filename
, /* I - Name of file */
1076 const char *mode
) /* I - Open mode */
1078 cups_file_t
*fp
; /* New CUPS file */
1079 int fd
; /* File descriptor */
1080 char hostname
[1024], /* Hostname */
1081 *portname
; /* Port "name" (number or service) */
1082 http_addrlist_t
*addrlist
; /* Host address list */
1085 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1089 * Range check input...
1092 if (!filename
|| !mode
||
1093 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1094 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1103 case 'a' : /* Append file */
1104 fd
= cups_open(filename
,
1105 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1108 case 'r' : /* Read file */
1109 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1112 case 'w' : /* Write file */
1113 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1114 if (fd
< 0 && errno
== ENOENT
)
1116 fd
= cups_open(filename
,
1117 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1118 if (fd
< 0 && errno
== EEXIST
)
1119 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1130 case 's' : /* Read/write socket */
1131 strlcpy(hostname
, filename
, sizeof(hostname
));
1132 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1138 * Lookup the hostname and service...
1141 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1145 * Connect to the server...
1148 if (!httpAddrConnect(addrlist
, &fd
))
1150 httpAddrFreeList(addrlist
);
1154 httpAddrFreeList(addrlist
);
1157 default : /* Remove bogus compiler warning... */
1165 * Create the CUPS file structure...
1168 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1184 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1186 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1187 * or "s" to treat the file descriptor as a bidirectional socket connection.
1189 * When opening for writing ("w"), an optional number from 1 to 9 can be
1190 * supplied which enables Flate compression of the file. Compression is
1191 * not supported for the "a" (append) mode.
1193 * @since CUPS 1.2/Mac OS X 10.5@
1196 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1197 cupsFileOpenFd(int fd
, /* I - File descriptor */
1198 const char *mode
) /* I - Open mode */
1200 cups_file_t
*fp
; /* New CUPS file */
1203 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1206 * Range check input...
1209 if (fd
< 0 || !mode
||
1210 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1211 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1215 * Allocate memory...
1218 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1230 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1235 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1238 if (mode
[1] >= '1' && mode
[1] <= '9')
1241 * Open a compressed stream, so write the standard gzip file
1245 unsigned char header
[10]; /* gzip file header */
1246 time_t curtime
; /* Current time */
1249 curtime
= time(NULL
);
1252 header
[2] = Z_DEFLATED
;
1254 header
[4] = curtime
;
1255 header
[5] = curtime
>> 8;
1256 header
[6] = curtime
>> 16;
1257 header
[7] = curtime
>> 24;
1261 cups_write(fp
, (char *)header
, 10);
1264 * Initialize the compressor...
1267 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1268 Z_DEFAULT_STRATEGY
);
1270 fp
->stream
.next_out
= fp
->cbuf
;
1271 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1273 fp
->crc
= crc32(0L, Z_NULL
, 0);
1275 #endif /* HAVE_LIBZ */
1286 default : /* Remove bogus compiler warning... */
1291 * Don't pass this file to child processes...
1295 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1303 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1305 * @since CUPS 1.2/Mac OS X 10.5@
1308 int /* O - Character or -1 on end of file */
1309 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1312 * Range check input...
1315 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1319 * If the input buffer is empty, try to read more data...
1322 if (fp
->ptr
>= fp
->end
)
1323 if (cups_fill(fp
) < 0)
1327 * Return the next character in the buffer...
1330 return (*(fp
->ptr
) & 255);
1335 * 'cupsFilePrintf()' - Write a formatted string.
1337 * @since CUPS 1.2/Mac OS X 10.5@
1340 int /* O - Number of bytes written or -1 on error */
1341 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1342 const char *format
, /* I - Printf-style format string */
1343 ...) /* I - Additional args as necessary */
1345 va_list ap
; /* Argument list */
1346 ssize_t bytes
; /* Formatted size */
1349 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1351 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1354 if (!fp
->printf_buffer
)
1357 * Start with an 1k printf buffer...
1360 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1363 fp
->printf_size
= 1024;
1366 va_start(ap
, format
);
1367 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1370 if (bytes
>= (ssize_t
)fp
->printf_size
)
1373 * Expand the printf buffer...
1376 char *temp
; /* Temporary buffer pointer */
1382 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1385 fp
->printf_buffer
= temp
;
1386 fp
->printf_size
= bytes
+ 1;
1388 va_start(ap
, format
);
1389 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1393 if (fp
->mode
== 's')
1395 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1400 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1405 if ((fp
->ptr
+ bytes
) > fp
->end
)
1406 if (cupsFileFlush(fp
))
1411 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1413 if (bytes
> sizeof(fp
->buf
))
1417 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1419 #endif /* HAVE_LIBZ */
1420 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1424 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1432 * 'cupsFilePutChar()' - Write a character.
1434 * @since CUPS 1.2/Mac OS X 10.5@
1437 int /* O - 0 on success, -1 on error */
1438 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1439 int c
) /* I - Character to write */
1442 * Range check input...
1445 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1448 if (fp
->mode
== 's')
1451 * Send character immediately over socket...
1454 char ch
; /* Output character */
1459 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1468 if (fp
->ptr
>= fp
->end
)
1469 if (cupsFileFlush(fp
))
1477 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1484 * 'cupsFilePutConf()' - Write a configuration line.
1486 * This function handles any comment escaping of the value.
1488 * @since CUPS 1.4/Mac OS X 10.6@
1491 ssize_t
/* O - Number of bytes written or -1 on error */
1492 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1493 const char *directive
, /* I - Directive */
1494 const char *value
) /* I - Value */
1496 ssize_t bytes
, /* Number of bytes written */
1497 temp
; /* Temporary byte count */
1498 const char *ptr
; /* Pointer into value */
1501 if (!fp
|| !directive
|| !*directive
)
1504 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1507 if (cupsFilePutChar(fp
, ' ') < 0)
1511 if (value
&& *value
)
1513 if ((ptr
= strchr(value
, '#')) != NULL
)
1516 * Need to quote the first # in the info string...
1519 if ((temp
= cupsFileWrite(fp
, value
, ptr
- value
)) < 0)
1523 if (cupsFilePutChar(fp
, '\\') < 0)
1527 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1531 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1537 if (cupsFilePutChar(fp
, '\n') < 0)
1545 * 'cupsFilePuts()' - Write a string.
1547 * Like the @code fputs@ function, no newline is appended to the string.
1549 * @since CUPS 1.2/Mac OS X 10.5@
1552 int /* O - Number of bytes written or -1 on error */
1553 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1554 const char *s
) /* I - String to write */
1556 ssize_t bytes
; /* Bytes to write */
1560 * Range check input...
1563 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1567 * Write the string...
1570 bytes
= (int)strlen(s
);
1572 if (fp
->mode
== 's')
1574 if (cups_write(fp
, s
, bytes
) < 0)
1579 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1584 if ((fp
->ptr
+ bytes
) > fp
->end
)
1585 if (cupsFileFlush(fp
))
1590 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1592 if (bytes
> sizeof(fp
->buf
))
1596 return (cups_compress(fp
, s
, bytes
));
1598 #endif /* HAVE_LIBZ */
1599 return (cups_write(fp
, s
, bytes
));
1603 memcpy(fp
->ptr
, s
, bytes
);
1611 * 'cupsFileRead()' - Read from a file.
1613 * @since CUPS 1.2/Mac OS X 10.5@
1616 ssize_t
/* O - Number of bytes read or -1 on error */
1617 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1618 char *buf
, /* O - Buffer */
1619 size_t bytes
) /* I - Number of bytes to read */
1621 size_t total
; /* Total bytes read */
1622 ssize_t count
; /* Bytes read */
1625 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1626 CUPS_LLCAST bytes
));
1629 * Range check input...
1632 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1639 * Loop until all bytes are read...
1645 if (fp
->ptr
>= fp
->end
)
1646 if (cups_fill(fp
) <= 0)
1648 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1649 CUPS_LLFMT
, CUPS_LLCAST total
));
1652 return ((ssize_t
)total
);
1657 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1658 if (count
> (ssize_t
)bytes
)
1659 count
= (ssize_t
)bytes
;
1661 memcpy(buf
, fp
->ptr
, count
);
1665 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1668 * Update the counts for the last read...
1677 * Return the total number of bytes read...
1680 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1682 return ((ssize_t
)total
);
1687 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1690 * @since CUPS 1.2/Mac OS X 10.5@
1693 off_t
/* O - New file position or -1 on error */
1694 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1697 * Range check input...
1700 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1701 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1703 if (!fp
|| fp
->mode
!= 'r')
1707 * Handle special cases...
1710 if (fp
->bufpos
== 0)
1713 * No seeking necessary...
1724 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1730 * Otherwise, seek in the file and cleanup any compression buffers...
1736 inflateEnd(&fp
->stream
);
1739 #endif /* HAVE_LIBZ */
1741 if (lseek(fp
->fd
, 0, SEEK_SET
))
1743 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1753 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1760 * 'cupsFileSeek()' - Seek in a file.
1762 * @since CUPS 1.2/Mac OS X 10.5@
1765 off_t
/* O - New file position or -1 on error */
1766 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1767 off_t pos
) /* I - Position in file */
1769 ssize_t bytes
; /* Number bytes in buffer */
1772 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1774 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1775 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1778 * Range check input...
1781 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1785 * Handle special cases...
1789 return (cupsFileRewind(fp
));
1793 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1795 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1797 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1800 * No seeking necessary...
1804 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1812 if (!fp
->compressed
&& !fp
->ptr
)
1815 * Preload a buffer to determine whether the file is compressed...
1818 if (cups_fill(fp
) < 0)
1821 #endif /* HAVE_LIBZ */
1824 * Seek forwards or backwards...
1829 if (pos
< fp
->bufpos
)
1832 * Need to seek backwards...
1835 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1840 inflateEnd(&fp
->stream
);
1842 lseek(fp
->fd
, 0, SEEK_SET
);
1848 while ((bytes
= cups_fill(fp
)) > 0)
1849 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1855 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1859 #endif /* HAVE_LIBZ */
1861 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1862 fp
->pos
= fp
->bufpos
;
1866 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1867 CUPS_LLCAST fp
->pos
));
1873 * Need to seek forwards...
1876 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1881 while ((bytes
= cups_fill(fp
)) > 0)
1883 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1890 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1894 #endif /* HAVE_LIBZ */
1896 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1897 fp
->pos
= fp
->bufpos
;
1901 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1902 CUPS_LLCAST fp
->pos
));
1906 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1913 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1915 * @since CUPS 1.2/Mac OS X 10.5@
1918 cups_file_t
* /* O - CUPS file */
1919 cupsFileStderr(void)
1921 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1925 * Open file descriptor 2 as needed...
1928 if (!cg
->stdio_files
[2])
1931 * Flush any pending output on the stdio file...
1937 * Open file descriptor 2...
1940 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1941 cg
->stdio_files
[2]->is_stdio
= 1;
1944 return (cg
->stdio_files
[2]);
1949 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1951 * @since CUPS 1.2/Mac OS X 10.5@
1954 cups_file_t
* /* O - CUPS file */
1957 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1961 * Open file descriptor 0 as needed...
1964 if (!cg
->stdio_files
[0])
1967 * Open file descriptor 0...
1970 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1971 cg
->stdio_files
[0]->is_stdio
= 1;
1974 return (cg
->stdio_files
[0]);
1979 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1981 * @since CUPS 1.2/Mac OS X 10.5@
1984 cups_file_t
* /* O - CUPS file */
1985 cupsFileStdout(void)
1987 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1991 * Open file descriptor 1 as needed...
1994 if (!cg
->stdio_files
[1])
1997 * Flush any pending output on the stdio file...
2003 * Open file descriptor 1...
2006 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
2007 cg
->stdio_files
[1]->is_stdio
= 1;
2010 return (cg
->stdio_files
[1]);
2015 * 'cupsFileTell()' - Return the current file position.
2017 * @since CUPS 1.2/Mac OS X 10.5@
2020 off_t
/* O - File position */
2021 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
2023 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
2024 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
2025 CUPS_LLCAST (fp
? fp
->pos
: -1)));
2027 return (fp
? fp
->pos
: 0);
2032 * 'cupsFileUnlock()' - Unlock access to a file.
2034 * @since CUPS 1.2/Mac OS X 10.5@
2037 int /* O - 0 on success, -1 on error */
2038 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2044 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2046 if (!fp
|| fp
->mode
== 's')
2054 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2056 return (lockf(fp
->fd
, F_ULOCK
, 0));
2062 * 'cupsFileWrite()' - Write to a file.
2064 * @since CUPS 1.2/Mac OS X 10.5@
2067 ssize_t
/* O - Number of bytes written or -1 on error */
2068 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2069 const char *buf
, /* I - Buffer */
2070 size_t bytes
) /* I - Number of bytes to write */
2073 * Range check input...
2076 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2077 fp
, buf
, CUPS_LLCAST bytes
));
2079 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2086 * Write the buffer...
2089 if (fp
->mode
== 's')
2091 if (cups_write(fp
, buf
, bytes
) < 0)
2094 fp
->pos
+= (off_t
)bytes
;
2096 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2098 return ((ssize_t
)bytes
);
2101 if ((fp
->ptr
+ bytes
) > fp
->end
)
2102 if (cupsFileFlush(fp
))
2105 fp
->pos
+= (off_t
)bytes
;
2107 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2109 if (bytes
> sizeof(fp
->buf
))
2113 return (cups_compress(fp
, buf
, bytes
));
2115 #endif /* HAVE_LIBZ */
2116 return (cups_write(fp
, buf
, bytes
));
2120 memcpy(fp
->ptr
, buf
, bytes
);
2122 return ((ssize_t
)bytes
);
2129 * 'cups_compress()' - Compress a buffer of data.
2132 static ssize_t
/* O - Number of bytes written or -1 */
2133 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2134 const char *buf
, /* I - Buffer */
2135 size_t bytes
) /* I - Number bytes */
2137 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2138 CUPS_LLCAST bytes
));
2144 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
2147 * Deflate the bytes...
2150 fp
->stream
.next_in
= (Bytef
*)buf
;
2151 fp
->stream
.avail_in
= bytes
;
2153 while (fp
->stream
.avail_in
> 0)
2156 * Flush the current buffer...
2159 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2160 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2162 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
2164 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
2167 fp
->stream
.next_out
= fp
->cbuf
;
2168 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2171 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2176 #endif /* HAVE_LIBZ */
2180 * 'cups_fill()' - Fill the input buffer.
2183 static ssize_t
/* O - Number of bytes or -1 */
2184 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2186 ssize_t bytes
; /* Number of bytes read */
2188 int status
; /* Decompression status */
2189 const unsigned char *ptr
, /* Pointer into buffer */
2190 *end
; /* End of buffer */
2191 #endif /* HAVE_LIBZ */
2194 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2195 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2196 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2197 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2199 if (fp
->ptr
&& fp
->end
)
2200 fp
->bufpos
+= fp
->end
- fp
->buf
;
2203 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2205 while (!fp
->ptr
|| fp
->compressed
)
2208 * Check to see if we have read any data yet; if not, see if we have a
2209 * compressed file...
2215 * Reset the file position in case we are seeking...
2221 * Read the first bytes in the file to determine if we have a gzip'd
2225 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2228 * Can't read from file!
2231 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2232 CUPS_LLCAST bytes
));
2237 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2238 (fp
->buf
[1] & 255) != 0x8b ||
2239 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2242 * Not a gzip'd file!
2246 fp
->end
= fp
->buf
+ bytes
;
2248 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2249 CUPS_LLCAST bytes
));
2255 * Parse header junk: extra data, original name, and comment...
2258 ptr
= (unsigned char *)fp
->buf
+ 10;
2259 end
= (unsigned char *)fp
->buf
+ bytes
;
2261 if (fp
->buf
[3] & 0x04)
2264 * Skip extra data...
2267 if ((ptr
+ 2) > end
)
2270 * Can't read from file!
2276 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2282 * Can't read from file!
2289 if (fp
->buf
[3] & 0x08)
2292 * Skip original name data...
2295 while (ptr
< end
&& *ptr
)
2303 * Can't read from file!
2310 if (fp
->buf
[3] & 0x10)
2313 * Skip comment data...
2316 while (ptr
< end
&& *ptr
)
2324 * Can't read from file!
2331 if (fp
->buf
[3] & 0x02)
2334 * Skip header CRC data...
2342 * Can't read from file!
2350 * Copy the flate-compressed data to the compression buffer...
2353 if ((bytes
= end
- ptr
) > 0)
2354 memcpy(fp
->cbuf
, ptr
, bytes
);
2357 * Setup the decompressor data...
2360 fp
->stream
.zalloc
= (alloc_func
)0;
2361 fp
->stream
.zfree
= (free_func
)0;
2362 fp
->stream
.opaque
= (voidpf
)0;
2363 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2364 fp
->stream
.next_out
= NULL
;
2365 fp
->stream
.avail_in
= bytes
;
2366 fp
->stream
.avail_out
= 0;
2367 fp
->crc
= crc32(0L, Z_NULL
, 0);
2369 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2378 * If we have reached end-of-file, return immediately...
2385 * Fill the decompression buffer as needed...
2388 if (fp
->stream
.avail_in
== 0)
2390 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2393 fp
->stream
.next_in
= fp
->cbuf
;
2394 fp
->stream
.avail_in
= bytes
;
2398 * Decompress data from the buffer...
2401 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2402 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2404 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2406 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2407 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2408 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2410 if (status
== Z_STREAM_END
)
2413 * Read the CRC and length...
2416 unsigned char trailer
[8]; /* Trailer bytes */
2417 uLong tcrc
; /* Trailer CRC */
2420 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2423 * Can't get it, so mark end-of-file...
2430 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2433 if (tcrc
!= fp
->crc
)
2436 * Bad CRC, mark end-of-file...
2439 DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
2440 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2448 * Otherwise, reset the compressed flag so that we re-read the
2456 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2459 * Return the decompressed data...
2463 fp
->end
= fp
->buf
+ bytes
;
2469 #endif /* HAVE_LIBZ */
2472 * Read a buffer's full of data...
2475 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2478 * Can't read from file!
2489 * Return the bytes we read...
2494 fp
->end
= fp
->buf
+ bytes
;
2501 * 'cups_open()' - Safely open a file for writing.
2503 * We don't allow appending to directories or files that are hard-linked or
2507 static int /* O - File descriptor or -1 otherwise */
2508 cups_open(const char *filename
, /* I - Filename */
2509 int mode
) /* I - Open mode */
2511 int fd
; /* File descriptor */
2512 struct stat fileinfo
; /* File information */
2514 struct stat linkinfo
; /* Link information */
2522 if ((fd
= open(filename
, mode
, 0666)) < 0)
2526 * Then verify that the file descriptor doesn't point to a directory or hard-
2530 if (fstat(fd
, &fileinfo
))
2536 if (fileinfo
.st_nlink
!= 1)
2544 if (fileinfo
.st_mode
& _S_IFDIR
)
2546 if (S_ISDIR(fileinfo
.st_mode
))
2556 * Then use lstat to determine whether the filename is a symlink...
2559 if (lstat(filename
, &linkinfo
))
2565 if (S_ISLNK(linkinfo
.st_mode
) ||
2566 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2567 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2569 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2570 #endif /* HAVE_ST_GEN */
2571 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2572 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2589 * 'cups_read()' - Read from a file descriptor.
2592 static ssize_t
/* O - Number of bytes read or -1 */
2593 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2594 char *buf
, /* I - Buffer */
2595 size_t bytes
) /* I - Number bytes */
2597 ssize_t total
; /* Total bytes read */
2600 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2601 CUPS_LLCAST bytes
));
2604 * Loop until we read at least 0 bytes...
2610 if (fp
->mode
== 's')
2611 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2613 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2615 if (fp
->mode
== 's')
2616 total
= recv(fp
->fd
, buf
, bytes
, 0);
2618 total
= read(fp
->fd
, buf
, bytes
);
2621 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2627 * Reads can be interrupted by signals and unavailable resources...
2630 if (errno
== EAGAIN
|| errno
== EINTR
)
2637 * Return the total number of bytes read...
2645 * 'cups_write()' - Write to a file descriptor.
2648 static ssize_t
/* O - Number of bytes written or -1 */
2649 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2650 const char *buf
, /* I - Buffer */
2651 size_t bytes
) /* I - Number bytes */
2653 size_t total
; /* Total bytes written */
2654 ssize_t count
; /* Count this time */
2657 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2658 CUPS_LLCAST bytes
));
2661 * Loop until all bytes are written...
2668 if (fp
->mode
== 's')
2669 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2671 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2673 if (fp
->mode
== 's')
2674 count
= send(fp
->fd
, buf
, bytes
, 0);
2676 count
= write(fp
->fd
, buf
, bytes
);
2679 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2684 * Writes can be interrupted by signals and unavailable resources...
2687 if (errno
== EAGAIN
|| errno
== EINTR
)
2694 * Update the counts for the last write call...
2703 * Return the total number of bytes written...
2706 return ((ssize_t
)total
);
2711 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".