]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
Merge changes from CUPS 1.5svn-r9717.
[thirdparty/cups.git] / cups / file.c
1 /*
2 * "$Id: file.c 7672 2008-06-18 22:03:02Z mike $"
3 *
4 * File functions for CUPS.
5 *
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.
10 *
11 * Copyright 2007-2011 by Apple Inc.
12 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
13 *
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/".
19 *
20 * Contents:
21 *
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
36 * file.
37 * cupsFileOpen() - Open a CUPS file.
38 * cupsFileOpenFd() - Open a CUPS file using a file descriptor.
39 * cupsFilePeekChar() - Peek at the next character from a file.
40 * cupsFilePrintf() - Write a formatted string.
41 * cupsFilePutChar() - Write a character.
42 * cupsFilePuts() - Write a string.
43 * cupsFileRead() - Read from a file.
44 * cupsFileRewind() - Set the current file position to the beginning of
45 * the file.
46 * cupsFileSeek() - Seek in a file.
47 * cupsFileStderr() - Return a CUPS file associated with stderr.
48 * cupsFileStdin() - Return a CUPS file associated with stdin.
49 * cupsFileStdout() - Return a CUPS file associated with stdout.
50 * cupsFileTell() - Return the current file position.
51 * cupsFileUnlock() - Unlock access to a file.
52 * cupsFileWrite() - Write to a file.
53 * cups_compress() - Compress a buffer of data.
54 * cups_fill() - Fill the input buffer.
55 * cups_read() - Read from a file descriptor.
56 * cups_write() - Write to a file descriptor.
57 */
58
59 /*
60 * Include necessary headers...
61 */
62
63 #include "file-private.h"
64 #include <sys/stat.h>
65 #include <sys/types.h>
66
67
68 /*
69 * Local functions...
70 */
71
72 #ifdef HAVE_LIBZ
73 static ssize_t cups_compress(cups_file_t *fp, const char *buf, size_t bytes);
74 #endif /* HAVE_LIBZ */
75 static ssize_t cups_fill(cups_file_t *fp);
76 static int cups_open(const char *filename, int mode);
77 static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes);
78 static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes);
79
80
81 /*
82 * '_cupsFileCheck()' - Check the permissions of the given filename.
83 */
84
85 _cups_fc_result_t /* O - Check result */
86 _cupsFileCheck(
87 const char *filename, /* I - Filename to check */
88 _cups_fc_filetype_t filetype, /* I - Type of file checks? */
89 int dorootchecks, /* I - Check for root permissions? */
90 _cups_fc_func_t cb, /* I - Callback function */
91 void *context) /* I - Context pointer for callback */
92
93 {
94 struct stat fileinfo; /* File information */
95 char message[1024], /* Message string */
96 temp[1024], /* Parent directory filename */
97 *ptr; /* Pointer into parent directory */
98 _cups_fc_result_t result; /* Check result */
99
100
101 /*
102 * Does the program even exist and is it accessible?
103 */
104
105 if (stat(filename, &fileinfo))
106 {
107 /*
108 * Nope...
109 */
110
111 result = _CUPS_FILE_CHECK_MISSING;
112 goto finishup;
113 }
114
115 /*
116 * Check the execute bit...
117 */
118
119 result = _CUPS_FILE_CHECK_OK;
120
121 switch (filetype)
122 {
123 case _CUPS_FILE_CHECK_DIRECTORY :
124 if (!S_ISDIR(fileinfo.st_mode))
125 result = _CUPS_FILE_CHECK_WRONG_TYPE;
126 break;
127
128 default :
129 if (!S_ISREG(fileinfo.st_mode))
130 result = _CUPS_FILE_CHECK_WRONG_TYPE;
131 break;
132 }
133
134 if (result)
135 goto finishup;
136
137 /*
138 * Are we doing root checks?
139 */
140
141 if (!dorootchecks)
142 {
143 /*
144 * Nope, so anything (else) goes...
145 */
146
147 goto finishup;
148 }
149
150 /*
151 * Verify permission of the file itself:
152 *
153 * 1. Must be owned by root
154 * 2. Must not be writable by group unless group is root/wheel/admin
155 * 3. Must not be setuid
156 * 4. Must not be writable by others
157 */
158
159 if (fileinfo.st_uid || /* 1. Must be owned by root */
160 #ifdef __APPLE__
161 ((fileinfo.st_mode & S_IWGRP) && fileinfo.st_gid &&
162 fileinfo.st_gid != 80) || /* 2. Must not be writable by group */
163 #else
164 ((fileinfo.st_mode & S_IWGRP) && fileinfo.st_gid) ||
165 /* 2. Must not be writable by group */
166 #endif /* __APPLE__ */
167 (fileinfo.st_mode & S_ISUID) || /* 3. Must not be setuid */
168 (fileinfo.st_mode & S_IWOTH)) /* 4. Must not be writable by others */
169 {
170 result = _CUPS_FILE_CHECK_PERMISSIONS;
171 goto finishup;
172 }
173
174 if (filetype == _CUPS_FILE_CHECK_DIRECTORY ||
175 filetype == _CUPS_FILE_CHECK_FILE_ONLY)
176 goto finishup;
177
178 /*
179 * Now check the containing directory...
180 */
181
182 strlcpy(temp, filename, sizeof(temp));
183 if ((ptr = strrchr(temp, '/')) != NULL)
184 {
185 if (ptr == temp)
186 ptr[1] = '\0';
187 else
188 *ptr = '\0';
189 }
190
191 if (stat(temp, &fileinfo))
192 {
193 /*
194 * Doesn't exist?!?
195 */
196
197 result = _CUPS_FILE_CHECK_MISSING;
198 filetype = _CUPS_FILE_CHECK_DIRECTORY;
199 filename = temp;
200
201 goto finishup;
202 }
203
204 if (fileinfo.st_uid || /* 1. Must be owned by root */
205 #ifdef __APPLE__
206 ((fileinfo.st_mode & S_IWGRP) && fileinfo.st_gid &&
207 fileinfo.st_gid != 80) || /* 2. Must not be writable by group */
208 #else
209 ((fileinfo.st_mode & S_IWGRP) && fileinfo.st_gid) ||
210 /* 2. Must not be writable by group */
211 #endif /* __APPLE__ */
212 (fileinfo.st_mode & S_ISUID) || /* 3. Must not be setuid */
213 (fileinfo.st_mode & S_IWOTH)) /* 4. Must not be writable by others */
214 {
215 result = _CUPS_FILE_CHECK_PERMISSIONS;
216 filetype = _CUPS_FILE_CHECK_DIRECTORY;
217 filename = temp;
218 }
219
220 /*
221 * Common return point...
222 */
223
224 finishup:
225
226 if (cb)
227 {
228 cups_lang_t *lang = cupsLangDefault();
229 /* Localization information */
230
231 switch (result)
232 {
233 case _CUPS_FILE_CHECK_OK :
234 if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
235 snprintf(message, sizeof(message),
236 _cupsLangString(lang, _("Directory \"%s\" permissions OK "
237 "(0%o/uid=%d/gid=%d).")),
238 filename, fileinfo.st_mode, (int)fileinfo.st_uid,
239 (int)fileinfo.st_gid);
240 else
241 snprintf(message, sizeof(message),
242 _cupsLangString(lang, _("File \"%s\" permissions OK "
243 "(0%o/uid=%d/gid=%d).")),
244 filename, fileinfo.st_mode, (int)fileinfo.st_uid,
245 (int)fileinfo.st_gid);
246 break;
247
248 case _CUPS_FILE_CHECK_MISSING :
249 if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
250 snprintf(message, sizeof(message),
251 _cupsLangString(lang, _("Directory \"%s\" not available: "
252 "%s")),
253 filename, strerror(errno));
254 else
255 snprintf(message, sizeof(message),
256 _cupsLangString(lang, _("File \"%s\" not available: %s")),
257 filename, strerror(errno));
258 break;
259
260 case _CUPS_FILE_CHECK_PERMISSIONS :
261 if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
262 snprintf(message, sizeof(message),
263 _cupsLangString(lang, _("Directory \"%s\" has insecure "
264 "permissions "
265 "(0%o/uid=%d/gid=%d).")),
266 filename, fileinfo.st_mode, (int)fileinfo.st_uid,
267 (int)fileinfo.st_gid);
268 else
269 snprintf(message, sizeof(message),
270 _cupsLangString(lang, _("File \"%s\" has insecure "
271 "permissions "
272 "(0%o/uid=%d/gid=%d).")),
273 filename, fileinfo.st_mode, (int)fileinfo.st_uid,
274 (int)fileinfo.st_gid);
275 break;
276
277 case _CUPS_FILE_CHECK_WRONG_TYPE :
278 if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
279 snprintf(message, sizeof(message),
280 _cupsLangString(lang, _("Directory \"%s\" is a file.")),
281 filename);
282 else
283 snprintf(message, sizeof(message),
284 _cupsLangString(lang, _("File \"%s\" is a directory.")),
285 filename);
286 break;
287 }
288
289 (*cb)(context, result, message);
290 }
291
292 return (result);
293 }
294
295
296 /*
297 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
298 */
299
300 void
301 _cupsFileCheckFilter(
302 void *context, /* I - Context pointer (unused) */
303 _cups_fc_result_t result, /* I - Result code */
304 const char *message) /* I - Message text */
305 {
306 const char *prefix; /* Messaging prefix */
307
308
309 switch (result)
310 {
311 case _CUPS_FILE_CHECK_OK :
312 prefix = "DEBUG2";
313 break;
314
315 case _CUPS_FILE_CHECK_MISSING :
316 case _CUPS_FILE_CHECK_WRONG_TYPE :
317 prefix = "ERROR";
318 fputs("STATE: +cups-missing-filter-warning\n", stderr);
319 break;
320
321 case _CUPS_FILE_CHECK_PERMISSIONS :
322 prefix = "ERROR";
323 fputs("STATE: +cups-insecure-filter-warning\n", stderr);
324 break;
325 }
326
327 fprintf(stderr, "%s: %s\n", prefix, message);
328 }
329
330
331 /*
332 * 'cupsFileClose()' - Close a CUPS file.
333 *
334 * @since CUPS 1.2/Mac OS X 10.5@
335 */
336
337 int /* O - 0 on success, -1 on error */
338 cupsFileClose(cups_file_t *fp) /* I - CUPS file */
339 {
340 int fd; /* File descriptor */
341 char mode; /* Open mode */
342 int status; /* Return status */
343 int is_stdio; /* Is a stdio file? */
344
345
346 DEBUG_printf(("cupsFileClose(fp=%p)", fp));
347
348 /*
349 * Range check...
350 */
351
352 if (!fp)
353 return (-1);
354
355 /*
356 * Flush pending write data...
357 */
358
359 if (fp->mode == 'w')
360 status = cupsFileFlush(fp);
361 else
362 status = 0;
363
364 #ifdef HAVE_LIBZ
365 if (fp->compressed && status >= 0)
366 {
367 if (fp->mode == 'r')
368 {
369 /*
370 * Free decompression data...
371 */
372
373 inflateEnd(&fp->stream);
374 }
375 else
376 {
377 /*
378 * Flush any remaining compressed data...
379 */
380
381 unsigned char trailer[8]; /* Trailer CRC and length */
382 int done; /* Done writing... */
383
384
385 fp->stream.avail_in = 0;
386
387 for (done = 0;;)
388 {
389 if (fp->stream.next_out > fp->cbuf)
390 {
391 if (cups_write(fp, (char *)fp->cbuf,
392 fp->stream.next_out - fp->cbuf) < 0)
393 status = -1;
394
395 fp->stream.next_out = fp->cbuf;
396 fp->stream.avail_out = sizeof(fp->cbuf);
397 }
398
399 if (done || status < 0)
400 break;
401
402 done = deflate(&fp->stream, Z_FINISH) == Z_STREAM_END &&
403 fp->stream.next_out == fp->cbuf;
404 }
405
406 /*
407 * Write the CRC and length...
408 */
409
410 trailer[0] = fp->crc;
411 trailer[1] = fp->crc >> 8;
412 trailer[2] = fp->crc >> 16;
413 trailer[3] = fp->crc >> 24;
414 trailer[4] = fp->pos;
415 trailer[5] = fp->pos >> 8;
416 trailer[6] = fp->pos >> 16;
417 trailer[7] = fp->pos >> 24;
418
419 if (cups_write(fp, (char *)trailer, 8) < 0)
420 status = -1;
421
422 /*
423 * Free all memory used by the compression stream...
424 */
425
426 deflateEnd(&(fp->stream));
427 }
428 }
429 #endif /* HAVE_LIBZ */
430
431 /*
432 * Save the file descriptor we used and free memory...
433 */
434
435 fd = fp->fd;
436 mode = fp->mode;
437 is_stdio = fp->is_stdio;
438
439 if (fp->printf_buffer)
440 free(fp->printf_buffer);
441
442 free(fp);
443
444 /*
445 * Close the file, returning the close status...
446 */
447
448 if (mode == 's')
449 {
450 if (closesocket(fd) < 0)
451 status = -1;
452 }
453 else if (!is_stdio)
454 {
455 if (close(fd) < 0)
456 status = -1;
457 }
458
459 return (status);
460 }
461
462
463 /*
464 * 'cupsFileCompression()' - Return whether a file is compressed.
465 *
466 * @since CUPS 1.2/Mac OS X 10.5@
467 */
468
469 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
470 cupsFileCompression(cups_file_t *fp) /* I - CUPS file */
471 {
472 return (fp ? fp->compressed : CUPS_FILE_NONE);
473 }
474
475
476 /*
477 * 'cupsFileEOF()' - Return the end-of-file status.
478 *
479 * @since CUPS 1.2/Mac OS X 10.5@
480 */
481
482 int /* O - 1 on end of file, 0 otherwise */
483 cupsFileEOF(cups_file_t *fp) /* I - CUPS file */
484 {
485 return (fp ? fp->eof : 1);
486 }
487
488
489 /*
490 * 'cupsFileFind()' - Find a file using the specified path.
491 *
492 * This function allows the paths in the path string to be separated by
493 * colons (UNIX standard) or semicolons (Windows standard) and stores the
494 * result in the buffer supplied. If the file cannot be found in any of
495 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
496 * matches the current directory.
497 *
498 * @since CUPS 1.2/Mac OS X 10.5@
499 */
500
501 const char * /* O - Full path to file or @code NULL@ if not found */
502 cupsFileFind(const char *filename, /* I - File to find */
503 const char *path, /* I - Colon/semicolon-separated path */
504 int executable, /* I - 1 = executable files, 0 = any file/dir */
505 char *buffer, /* I - Filename buffer */
506 int bufsize) /* I - Size of filename buffer */
507 {
508 char *bufptr, /* Current position in buffer */
509 *bufend; /* End of buffer */
510
511
512 /*
513 * Range check input...
514 */
515
516 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
517 "buffer=%p, bufsize=%d)", filename, path, executable, buffer,
518 bufsize));
519
520 if (!filename || !buffer || bufsize < 2)
521 return (NULL);
522
523 if (!path)
524 {
525 /*
526 * No path, so check current directory...
527 */
528
529 if (!access(filename, 0))
530 {
531 strlcpy(buffer, filename, bufsize);
532 return (buffer);
533 }
534 else
535 return (NULL);
536 }
537
538 /*
539 * Now check each path and return the first match...
540 */
541
542 bufend = buffer + bufsize - 1;
543 bufptr = buffer;
544
545 while (*path)
546 {
547 #ifdef WIN32
548 if (*path == ';' || (*path == ':' && ((bufptr - buffer) > 1 || !isalpha(buffer[0] & 255))))
549 #else
550 if (*path == ';' || *path == ':')
551 #endif /* WIN32 */
552 {
553 if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
554 *bufptr++ = '/';
555
556 strlcpy(bufptr, filename, bufend - bufptr);
557
558 #ifdef WIN32
559 if (!access(buffer, 0))
560 #else
561 if (!access(buffer, executable ? X_OK : 0))
562 #endif /* WIN32 */
563 {
564 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer));
565 return (buffer);
566 }
567
568 bufptr = buffer;
569 }
570 else if (bufptr < bufend)
571 *bufptr++ = *path;
572
573 path ++;
574 }
575
576 /*
577 * Check the last path...
578 */
579
580 if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
581 *bufptr++ = '/';
582
583 strlcpy(bufptr, filename, bufend - bufptr);
584
585 if (!access(buffer, 0))
586 {
587 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer));
588 return (buffer);
589 }
590 else
591 {
592 DEBUG_puts("1cupsFileFind: Returning NULL");
593 return (NULL);
594 }
595 }
596
597
598 /*
599 * 'cupsFileFlush()' - Flush pending output.
600 *
601 * @since CUPS 1.2/Mac OS X 10.5@
602 */
603
604 int /* O - 0 on success, -1 on error */
605 cupsFileFlush(cups_file_t *fp) /* I - CUPS file */
606 {
607 ssize_t bytes; /* Bytes to write */
608
609
610 DEBUG_printf(("cupsFileFlush(fp=%p)", fp));
611
612 /*
613 * Range check input...
614 */
615
616 if (!fp || fp->mode != 'w')
617 {
618 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
619 return (-1);
620 }
621
622 bytes = (ssize_t)(fp->ptr - fp->buf);
623
624 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT " bytes...",
625 CUPS_LLCAST bytes));
626
627 if (bytes > 0)
628 {
629 #ifdef HAVE_LIBZ
630 if (fp->compressed)
631 bytes = cups_compress(fp, fp->buf, bytes);
632 else
633 #endif /* HAVE_LIBZ */
634 bytes = cups_write(fp, fp->buf, bytes);
635
636 if (bytes < 0)
637 return (-1);
638
639 fp->ptr = fp->buf;
640 }
641
642 return (0);
643 }
644
645
646 /*
647 * 'cupsFileGetChar()' - Get a single character from a file.
648 *
649 * @since CUPS 1.2/Mac OS X 10.5@
650 */
651
652 int /* O - Character or -1 on end of file */
653 cupsFileGetChar(cups_file_t *fp) /* I - CUPS file */
654 {
655 /*
656 * Range check input...
657 */
658
659 if (!fp || (fp->mode != 'r' && fp->mode != 's'))
660 {
661 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
662 return (-1);
663 }
664
665 /*
666 * If the input buffer is empty, try to read more data...
667 */
668
669 if (fp->ptr >= fp->end)
670 if (cups_fill(fp) < 0)
671 {
672 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
673 return (-1);
674 }
675
676 /*
677 * Return the next character in the buffer...
678 */
679
680 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp->ptr) & 255));
681
682 fp->pos ++;
683
684 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
685
686 return (*(fp->ptr)++ & 255);
687 }
688
689
690 /*
691 * 'cupsFileGetConf()' - Get a line from a configuration file.
692 *
693 * @since CUPS 1.2/Mac OS X 10.5@
694 */
695
696 char * /* O - Line read or @code NULL@ on end of file or error */
697 cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */
698 char *buf, /* O - String buffer */
699 size_t buflen, /* I - Size of string buffer */
700 char **value, /* O - Pointer to value */
701 int *linenum) /* IO - Current line number */
702 {
703 char *ptr; /* Pointer into line */
704
705
706 /*
707 * Range check input...
708 */
709
710 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
711 ", value=%p, linenum=%p)", fp, buf, CUPS_LLCAST buflen,
712 value, linenum));
713
714 if (!fp || (fp->mode != 'r' && fp->mode != 's') ||
715 !buf || buflen < 2 || !value)
716 {
717 if (value)
718 *value = NULL;
719
720 return (NULL);
721 }
722
723 /*
724 * Read the next non-comment line...
725 */
726
727 *value = NULL;
728
729 while (cupsFileGets(fp, buf, buflen))
730 {
731 (*linenum) ++;
732
733 /*
734 * Strip any comments...
735 */
736
737 if ((ptr = strchr(buf, '#')) != NULL)
738 {
739 if (ptr > buf && ptr[-1] == '\\')
740 {
741 // Unquote the #...
742 _cups_strcpy(ptr - 1, ptr);
743 }
744 else
745 {
746 // Strip the comment and any trailing whitespace...
747 while (ptr > buf)
748 {
749 if (!_cups_isspace(ptr[-1]))
750 break;
751
752 ptr --;
753 }
754
755 *ptr = '\0';
756 }
757 }
758
759 /*
760 * Strip leading whitespace...
761 */
762
763 for (ptr = buf; _cups_isspace(*ptr); ptr ++);
764
765 if (ptr > buf)
766 _cups_strcpy(buf, ptr);
767
768 /*
769 * See if there is anything left...
770 */
771
772 if (buf[0])
773 {
774 /*
775 * Yes, grab any value and return...
776 */
777
778 for (ptr = buf; *ptr; ptr ++)
779 if (_cups_isspace(*ptr))
780 break;
781
782 if (*ptr)
783 {
784 /*
785 * Have a value, skip any other spaces...
786 */
787
788 while (_cups_isspace(*ptr))
789 *ptr++ = '\0';
790
791 if (*ptr)
792 *value = ptr;
793
794 /*
795 * Strip trailing whitespace and > for lines that begin with <...
796 */
797
798 ptr += strlen(ptr) - 1;
799
800 if (buf[0] == '<' && *ptr == '>')
801 *ptr-- = '\0';
802 else if (buf[0] == '<' && *ptr != '>')
803 {
804 /*
805 * Syntax error...
806 */
807
808 *value = NULL;
809 return (buf);
810 }
811
812 while (ptr > *value && _cups_isspace(*ptr))
813 *ptr-- = '\0';
814 }
815
816 /*
817 * Return the line...
818 */
819
820 return (buf);
821 }
822 }
823
824 return (NULL);
825 }
826
827
828 /*
829 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
830 * contain binary data.
831 *
832 * This function differs from @link cupsFileGets@ in that the trailing CR
833 * and LF are preserved, as is any binary data on the line. The buffer is
834 * nul-terminated, however you should use the returned length to determine
835 * the number of bytes on the line.
836 *
837 * @since CUPS 1.2/Mac OS X 10.5@
838 */
839
840 size_t /* O - Number of bytes on line or 0 on end of file */
841 cupsFileGetLine(cups_file_t *fp, /* I - File to read from */
842 char *buf, /* I - Buffer */
843 size_t buflen) /* I - Size of buffer */
844 {
845 int ch; /* Character from file */
846 char *ptr, /* Current position in line buffer */
847 *end; /* End of line buffer */
848
849
850 /*
851 * Range check input...
852 */
853
854 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")",
855 fp, buf, CUPS_LLCAST buflen));
856
857 if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 3)
858 return (0);
859
860 /*
861 * Now loop until we have a valid line...
862 */
863
864 for (ptr = buf, end = buf + buflen - 2; ptr < end ;)
865 {
866 if (fp->ptr >= fp->end)
867 if (cups_fill(fp) <= 0)
868 break;
869
870 *ptr++ = ch = *(fp->ptr)++;
871 fp->pos ++;
872
873 if (ch == '\r')
874 {
875 /*
876 * Check for CR LF...
877 */
878
879 if (fp->ptr >= fp->end)
880 if (cups_fill(fp) <= 0)
881 break;
882
883 if (*(fp->ptr) == '\n')
884 {
885 *ptr++ = *(fp->ptr)++;
886 fp->pos ++;
887 }
888
889 break;
890 }
891 else if (ch == '\n')
892 {
893 /*
894 * Line feed ends a line...
895 */
896
897 break;
898 }
899 }
900
901 *ptr = '\0';
902
903 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
904
905 return (ptr - buf);
906 }
907
908
909 /*
910 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
911 *
912 * @since CUPS 1.2/Mac OS X 10.5@
913 */
914
915 char * /* O - Line read or @code NULL@ on end of file or error */
916 cupsFileGets(cups_file_t *fp, /* I - CUPS file */
917 char *buf, /* O - String buffer */
918 size_t buflen) /* I - Size of string buffer */
919 {
920 int ch; /* Character from file */
921 char *ptr, /* Current position in line buffer */
922 *end; /* End of line buffer */
923
924
925 /*
926 * Range check input...
927 */
928
929 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")", fp, buf,
930 CUPS_LLCAST buflen));
931
932 if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 2)
933 return (NULL);
934
935 /*
936 * Now loop until we have a valid line...
937 */
938
939 for (ptr = buf, end = buf + buflen - 1; ptr < end ;)
940 {
941 if (fp->ptr >= fp->end)
942 if (cups_fill(fp) <= 0)
943 {
944 if (ptr == buf)
945 return (NULL);
946 else
947 break;
948 }
949
950 ch = *(fp->ptr)++;
951 fp->pos ++;
952
953 if (ch == '\r')
954 {
955 /*
956 * Check for CR LF...
957 */
958
959 if (fp->ptr >= fp->end)
960 if (cups_fill(fp) <= 0)
961 break;
962
963 if (*(fp->ptr) == '\n')
964 {
965 fp->ptr ++;
966 fp->pos ++;
967 }
968
969 break;
970 }
971 else if (ch == '\n')
972 {
973 /*
974 * Line feed ends a line...
975 */
976
977 break;
978 }
979 else
980 *ptr++ = ch;
981 }
982
983 *ptr = '\0';
984
985 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
986
987 return (buf);
988 }
989
990
991 /*
992 * 'cupsFileLock()' - Temporarily lock access to a file.
993 *
994 * @since CUPS 1.2/Mac OS X 10.5@
995 */
996
997 int /* O - 0 on success, -1 on error */
998 cupsFileLock(cups_file_t *fp, /* I - CUPS file */
999 int block) /* I - 1 to wait for the lock, 0 to fail right away */
1000 {
1001 /*
1002 * Range check...
1003 */
1004
1005 if (!fp || fp->mode == 's')
1006 return (-1);
1007
1008 /*
1009 * Try the lock...
1010 */
1011
1012 #ifdef WIN32
1013 return (_locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0));
1014 #else
1015 return (lockf(fp->fd, block ? F_LOCK : F_TLOCK, 0));
1016 #endif /* WIN32 */
1017 }
1018
1019
1020 /*
1021 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1022 *
1023 * @since CUPS 1.2/Mac OS X 10.5@
1024 */
1025
1026 int /* O - File descriptor */
1027 cupsFileNumber(cups_file_t *fp) /* I - CUPS file */
1028 {
1029 if (fp)
1030 return (fp->fd);
1031 else
1032 return (-1);
1033 }
1034
1035
1036 /*
1037 * 'cupsFileOpen()' - Open a CUPS file.
1038 *
1039 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1040 * existing file, "a" to append to an existing file or create a new file,
1041 * or "s" to open a socket connection.
1042 *
1043 * When opening for writing ("w"), an optional number from 1 to 9 can be
1044 * supplied which enables Flate compression of the file. Compression is
1045 * not supported for the "a" (append) mode.
1046 *
1047 * When opening a socket connection, the filename is a string of the form
1048 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1049 * connection as needed, generally preferring IPv6 connections when there is
1050 * a choice.
1051 *
1052 * @since CUPS 1.2/Mac OS X 10.5@
1053 */
1054
1055 cups_file_t * /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1056 cupsFileOpen(const char *filename, /* I - Name of file */
1057 const char *mode) /* I - Open mode */
1058 {
1059 cups_file_t *fp; /* New CUPS file */
1060 int fd; /* File descriptor */
1061 char hostname[1024], /* Hostname */
1062 *portname; /* Port "name" (number or service) */
1063 http_addrlist_t *addrlist; /* Host address list */
1064
1065
1066 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename,
1067 mode));
1068
1069 /*
1070 * Range check input...
1071 */
1072
1073 if (!filename || !mode ||
1074 (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') ||
1075 (*mode == 'a' && isdigit(mode[1] & 255)))
1076 return (NULL);
1077
1078 /*
1079 * Open the file...
1080 */
1081
1082 switch (*mode)
1083 {
1084 case 'a' : /* Append file */
1085 fd = cups_open(filename,
1086 O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY);
1087 break;
1088
1089 case 'r' : /* Read file */
1090 fd = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0);
1091 break;
1092
1093 case 'w' : /* Write file */
1094 fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY);
1095 if (fd < 0 && errno == ENOENT)
1096 {
1097 fd = cups_open(filename,
1098 O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE | O_BINARY);
1099 if (fd < 0 && errno == EEXIST)
1100 fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY);
1101 }
1102
1103 if (fd >= 0)
1104 #ifdef WIN32
1105 _chsize(fd, 0);
1106 #else
1107 ftruncate(fd, 0);
1108 #endif /* WIN32 */
1109 break;
1110
1111 case 's' : /* Read/write socket */
1112 strlcpy(hostname, filename, sizeof(hostname));
1113 if ((portname = strrchr(hostname, ':')) != NULL)
1114 *portname++ = '\0';
1115 else
1116 return (NULL);
1117
1118 /*
1119 * Lookup the hostname and service...
1120 */
1121
1122 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
1123 return (NULL);
1124
1125 /*
1126 * Connect to the server...
1127 */
1128
1129 if (!httpAddrConnect(addrlist, &fd))
1130 {
1131 httpAddrFreeList(addrlist);
1132 return (NULL);
1133 }
1134
1135 httpAddrFreeList(addrlist);
1136 break;
1137
1138 default : /* Remove bogus compiler warning... */
1139 return (NULL);
1140 }
1141
1142 if (fd < 0)
1143 return (NULL);
1144
1145 /*
1146 * Create the CUPS file structure...
1147 */
1148
1149 if ((fp = cupsFileOpenFd(fd, mode)) == NULL)
1150 {
1151 if (*mode == 's')
1152 closesocket(fd);
1153 else
1154 close(fd);
1155 }
1156
1157 /*
1158 * Return it...
1159 */
1160
1161 return (fp);
1162 }
1163
1164 /*
1165 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1166 *
1167 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1168 * or "s" to treat the file descriptor as a bidirectional socket connection.
1169 *
1170 * When opening for writing ("w"), an optional number from 1 to 9 can be
1171 * supplied which enables Flate compression of the file. Compression is
1172 * not supported for the "a" (append) mode.
1173 *
1174 * @since CUPS 1.2/Mac OS X 10.5@
1175 */
1176
1177 cups_file_t * /* O - CUPS file or @code NULL@ if the file could not be opened */
1178 cupsFileOpenFd(int fd, /* I - File descriptor */
1179 const char *mode) /* I - Open mode */
1180 {
1181 cups_file_t *fp; /* New CUPS file */
1182
1183
1184 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd, mode));
1185
1186 /*
1187 * Range check input...
1188 */
1189
1190 if (fd < 0 || !mode ||
1191 (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') ||
1192 (*mode == 'a' && isdigit(mode[1] & 255)))
1193 return (NULL);
1194
1195 /*
1196 * Allocate memory...
1197 */
1198
1199 if ((fp = calloc(1, sizeof(cups_file_t))) == NULL)
1200 return (NULL);
1201
1202 /*
1203 * Open the file...
1204 */
1205
1206 fp->fd = fd;
1207
1208 switch (*mode)
1209 {
1210 case 'a' :
1211 fp->pos = lseek(fd, 0, SEEK_END);
1212
1213 case 'w' :
1214 fp->mode = 'w';
1215 fp->ptr = fp->buf;
1216 fp->end = fp->buf + sizeof(fp->buf);
1217
1218 #ifdef HAVE_LIBZ
1219 if (mode[1] >= '1' && mode[1] <= '9')
1220 {
1221 /*
1222 * Open a compressed stream, so write the standard gzip file
1223 * header...
1224 */
1225
1226 unsigned char header[10]; /* gzip file header */
1227 time_t curtime; /* Current time */
1228
1229
1230 curtime = time(NULL);
1231 header[0] = 0x1f;
1232 header[1] = 0x8b;
1233 header[2] = Z_DEFLATED;
1234 header[3] = 0;
1235 header[4] = curtime;
1236 header[5] = curtime >> 8;
1237 header[6] = curtime >> 16;
1238 header[7] = curtime >> 24;
1239 header[8] = 0;
1240 header[9] = 0x03;
1241
1242 cups_write(fp, (char *)header, 10);
1243
1244 /*
1245 * Initialize the compressor...
1246 */
1247
1248 deflateInit2(&(fp->stream), mode[1] - '0', Z_DEFLATED, -15, 8,
1249 Z_DEFAULT_STRATEGY);
1250
1251 fp->stream.next_out = fp->cbuf;
1252 fp->stream.avail_out = sizeof(fp->cbuf);
1253 fp->compressed = 1;
1254 fp->crc = crc32(0L, Z_NULL, 0);
1255 }
1256 #endif /* HAVE_LIBZ */
1257 break;
1258
1259 case 'r' :
1260 fp->mode = 'r';
1261 break;
1262
1263 case 's' :
1264 fp->mode = 's';
1265 break;
1266
1267 default : /* Remove bogus compiler warning... */
1268 return (NULL);
1269 }
1270
1271 /*
1272 * Don't pass this file to child processes...
1273 */
1274
1275 #ifndef WIN32
1276 fcntl(fp->fd, F_SETFD, fcntl(fp->fd, F_GETFD) | FD_CLOEXEC);
1277 #endif /* !WIN32 */
1278
1279 return (fp);
1280 }
1281
1282
1283 /*
1284 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1285 *
1286 * @since CUPS 1.2/Mac OS X 10.5@
1287 */
1288
1289 int /* O - Character or -1 on end of file */
1290 cupsFilePeekChar(cups_file_t *fp) /* I - CUPS file */
1291 {
1292 /*
1293 * Range check input...
1294 */
1295
1296 if (!fp || (fp->mode != 'r' && fp->mode != 's'))
1297 return (-1);
1298
1299 /*
1300 * If the input buffer is empty, try to read more data...
1301 */
1302
1303 if (fp->ptr >= fp->end)
1304 if (cups_fill(fp) < 0)
1305 return (-1);
1306
1307 /*
1308 * Return the next character in the buffer...
1309 */
1310
1311 return (*(fp->ptr) & 255);
1312 }
1313
1314
1315 /*
1316 * 'cupsFilePrintf()' - Write a formatted string.
1317 *
1318 * @since CUPS 1.2/Mac OS X 10.5@
1319 */
1320
1321 int /* O - Number of bytes written or -1 on error */
1322 cupsFilePrintf(cups_file_t *fp, /* I - CUPS file */
1323 const char *format, /* I - Printf-style format string */
1324 ...) /* I - Additional args as necessary */
1325 {
1326 va_list ap; /* Argument list */
1327 ssize_t bytes; /* Formatted size */
1328
1329
1330 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp, format));
1331
1332 if (!fp || !format || (fp->mode != 'w' && fp->mode != 's'))
1333 return (-1);
1334
1335 if (!fp->printf_buffer)
1336 {
1337 /*
1338 * Start with an 1k printf buffer...
1339 */
1340
1341 if ((fp->printf_buffer = malloc(1024)) == NULL)
1342 return (-1);
1343
1344 fp->printf_size = 1024;
1345 }
1346
1347 va_start(ap, format);
1348 bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap);
1349 va_end(ap);
1350
1351 if (bytes >= (ssize_t)fp->printf_size)
1352 {
1353 /*
1354 * Expand the printf buffer...
1355 */
1356
1357 char *temp; /* Temporary buffer pointer */
1358
1359
1360 if (bytes > 65535)
1361 return (-1);
1362
1363 if ((temp = realloc(fp->printf_buffer, bytes + 1)) == NULL)
1364 return (-1);
1365
1366 fp->printf_buffer = temp;
1367 fp->printf_size = bytes + 1;
1368
1369 va_start(ap, format);
1370 bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap);
1371 va_end(ap);
1372 }
1373
1374 if (fp->mode == 's')
1375 {
1376 if (cups_write(fp, fp->printf_buffer, bytes) < 0)
1377 return (-1);
1378
1379 fp->pos += bytes;
1380
1381 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1382
1383 return (bytes);
1384 }
1385
1386 if ((fp->ptr + bytes) > fp->end)
1387 if (cupsFileFlush(fp))
1388 return (-1);
1389
1390 fp->pos += bytes;
1391
1392 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1393
1394 if (bytes > sizeof(fp->buf))
1395 {
1396 #ifdef HAVE_LIBZ
1397 if (fp->compressed)
1398 return (cups_compress(fp, fp->printf_buffer, bytes));
1399 else
1400 #endif /* HAVE_LIBZ */
1401 return (cups_write(fp, fp->printf_buffer, bytes));
1402 }
1403 else
1404 {
1405 memcpy(fp->ptr, fp->printf_buffer, bytes);
1406 fp->ptr += bytes;
1407 return (bytes);
1408 }
1409 }
1410
1411
1412 /*
1413 * 'cupsFilePutChar()' - Write a character.
1414 *
1415 * @since CUPS 1.2/Mac OS X 10.5@
1416 */
1417
1418 int /* O - 0 on success, -1 on error */
1419 cupsFilePutChar(cups_file_t *fp, /* I - CUPS file */
1420 int c) /* I - Character to write */
1421 {
1422 /*
1423 * Range check input...
1424 */
1425
1426 if (!fp || (fp->mode != 'w' && fp->mode != 's'))
1427 return (-1);
1428
1429 if (fp->mode == 's')
1430 {
1431 /*
1432 * Send character immediately over socket...
1433 */
1434
1435 char ch; /* Output character */
1436
1437
1438 ch = c;
1439
1440 if (send(fp->fd, &ch, 1, 0) < 1)
1441 return (-1);
1442 }
1443 else
1444 {
1445 /*
1446 * Buffer it up...
1447 */
1448
1449 if (fp->ptr >= fp->end)
1450 if (cupsFileFlush(fp))
1451 return (-1);
1452
1453 *(fp->ptr) ++ = c;
1454 }
1455
1456 fp->pos ++;
1457
1458 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1459
1460 return (0);
1461 }
1462
1463
1464 /*
1465 * 'cupsFilePutConf()' - Write a configuration line.
1466 *
1467 * This function handles any comment escaping of the value.
1468 *
1469 * @since CUPS 1.4/Mac OS X 10.6@
1470 */
1471
1472 ssize_t /* O - Number of bytes written or -1 on error */
1473 cupsFilePutConf(cups_file_t *fp, /* I - CUPS file */
1474 const char *directive, /* I - Directive */
1475 const char *value) /* I - Value */
1476 {
1477 ssize_t bytes, /* Number of bytes written */
1478 temp; /* Temporary byte count */
1479 const char *ptr; /* Pointer into value */
1480
1481
1482 if (!fp || !directive || !*directive)
1483 return (-1);
1484
1485 if ((bytes = cupsFilePuts(fp, directive)) < 0)
1486 return (-1);
1487
1488 if (cupsFilePutChar(fp, ' ') < 0)
1489 return (-1);
1490 bytes ++;
1491
1492 if (value && *value)
1493 {
1494 if ((ptr = strchr(value, '#')) != NULL)
1495 {
1496 /*
1497 * Need to quote the first # in the info string...
1498 */
1499
1500 if ((temp = cupsFileWrite(fp, value, ptr - value)) < 0)
1501 return (-1);
1502 bytes += temp;
1503
1504 if (cupsFilePutChar(fp, '\\') < 0)
1505 return (-1);
1506 bytes ++;
1507
1508 if ((temp = cupsFilePuts(fp, ptr)) < 0)
1509 return (-1);
1510 bytes += temp;
1511 }
1512 else if ((temp = cupsFilePuts(fp, value)) < 0)
1513 return (-1);
1514 else
1515 bytes += temp;
1516 }
1517
1518 if (cupsFilePutChar(fp, '\n') < 0)
1519 return (-1);
1520 else
1521 return (bytes + 1);
1522 }
1523
1524
1525 /*
1526 * 'cupsFilePuts()' - Write a string.
1527 *
1528 * Like the @code fputs@ function, no newline is appended to the string.
1529 *
1530 * @since CUPS 1.2/Mac OS X 10.5@
1531 */
1532
1533 int /* O - Number of bytes written or -1 on error */
1534 cupsFilePuts(cups_file_t *fp, /* I - CUPS file */
1535 const char *s) /* I - String to write */
1536 {
1537 ssize_t bytes; /* Bytes to write */
1538
1539
1540 /*
1541 * Range check input...
1542 */
1543
1544 if (!fp || !s || (fp->mode != 'w' && fp->mode != 's'))
1545 return (-1);
1546
1547 /*
1548 * Write the string...
1549 */
1550
1551 bytes = (int)strlen(s);
1552
1553 if (fp->mode == 's')
1554 {
1555 if (cups_write(fp, s, bytes) < 0)
1556 return (-1);
1557
1558 fp->pos += bytes;
1559
1560 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1561
1562 return (bytes);
1563 }
1564
1565 if ((fp->ptr + bytes) > fp->end)
1566 if (cupsFileFlush(fp))
1567 return (-1);
1568
1569 fp->pos += bytes;
1570
1571 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1572
1573 if (bytes > sizeof(fp->buf))
1574 {
1575 #ifdef HAVE_LIBZ
1576 if (fp->compressed)
1577 return (cups_compress(fp, s, bytes));
1578 else
1579 #endif /* HAVE_LIBZ */
1580 return (cups_write(fp, s, bytes));
1581 }
1582 else
1583 {
1584 memcpy(fp->ptr, s, bytes);
1585 fp->ptr += bytes;
1586 return (bytes);
1587 }
1588 }
1589
1590
1591 /*
1592 * 'cupsFileRead()' - Read from a file.
1593 *
1594 * @since CUPS 1.2/Mac OS X 10.5@
1595 */
1596
1597 ssize_t /* O - Number of bytes read or -1 on error */
1598 cupsFileRead(cups_file_t *fp, /* I - CUPS file */
1599 char *buf, /* O - Buffer */
1600 size_t bytes) /* I - Number of bytes to read */
1601 {
1602 size_t total; /* Total bytes read */
1603 ssize_t count; /* Bytes read */
1604
1605
1606 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
1607 CUPS_LLCAST bytes));
1608
1609 /*
1610 * Range check input...
1611 */
1612
1613 if (!fp || !buf || (fp->mode != 'r' && fp->mode != 's'))
1614 return (-1);
1615
1616 if (bytes == 0)
1617 return (0);
1618
1619 /*
1620 * Loop until all bytes are read...
1621 */
1622
1623 total = 0;
1624 while (bytes > 0)
1625 {
1626 if (fp->ptr >= fp->end)
1627 if (cups_fill(fp) <= 0)
1628 {
1629 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1630 CUPS_LLFMT, CUPS_LLCAST total));
1631
1632 if (total > 0)
1633 return ((ssize_t)total);
1634 else
1635 return (-1);
1636 }
1637
1638 count = (ssize_t)(fp->end - fp->ptr);
1639 if (count > (ssize_t)bytes)
1640 count = (ssize_t)bytes;
1641
1642 memcpy(buf, fp->ptr, count);
1643 fp->ptr += count;
1644 fp->pos += count;
1645
1646 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1647
1648 /*
1649 * Update the counts for the last read...
1650 */
1651
1652 bytes -= count;
1653 total += count;
1654 buf += count;
1655 }
1656
1657 /*
1658 * Return the total number of bytes read...
1659 */
1660
1661 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT, CUPS_LLCAST total));
1662
1663 return ((ssize_t)total);
1664 }
1665
1666
1667 /*
1668 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1669 * file.
1670 *
1671 * @since CUPS 1.2/Mac OS X 10.5@
1672 */
1673
1674 off_t /* O - New file position or -1 on error */
1675 cupsFileRewind(cups_file_t *fp) /* I - CUPS file */
1676 {
1677 /*
1678 * Range check input...
1679 */
1680
1681 DEBUG_printf(("cupsFileRewind(fp=%p)", fp));
1682 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1683
1684 if (!fp || fp->mode != 'r')
1685 return (-1);
1686
1687 /*
1688 * Handle special cases...
1689 */
1690
1691 if (fp->bufpos == 0)
1692 {
1693 /*
1694 * No seeking necessary...
1695 */
1696
1697 fp->pos = 0;
1698
1699 if (fp->ptr)
1700 {
1701 fp->ptr = fp->buf;
1702 fp->eof = 0;
1703 }
1704
1705 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1706
1707 return (0);
1708 }
1709
1710 /*
1711 * Otherwise, seek in the file and cleanup any compression buffers...
1712 */
1713
1714 #ifdef HAVE_LIBZ
1715 if (fp->compressed)
1716 {
1717 inflateEnd(&fp->stream);
1718 fp->compressed = 0;
1719 }
1720 #endif /* HAVE_LIBZ */
1721
1722 if (lseek(fp->fd, 0, SEEK_SET))
1723 {
1724 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno)));
1725 return (-1);
1726 }
1727
1728 fp->bufpos = 0;
1729 fp->pos = 0;
1730 fp->ptr = NULL;
1731 fp->end = NULL;
1732 fp->eof = 0;
1733
1734 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1735
1736 return (0);
1737 }
1738
1739
1740 /*
1741 * 'cupsFileSeek()' - Seek in a file.
1742 *
1743 * @since CUPS 1.2/Mac OS X 10.5@
1744 */
1745
1746 off_t /* O - New file position or -1 on error */
1747 cupsFileSeek(cups_file_t *fp, /* I - CUPS file */
1748 off_t pos) /* I - Position in file */
1749 {
1750 ssize_t bytes; /* Number bytes in buffer */
1751
1752
1753 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")", fp,
1754 CUPS_LLCAST pos));
1755 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1756 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp->ptr, fp->end));
1757
1758 /*
1759 * Range check input...
1760 */
1761
1762 if (!fp || pos < 0 || fp->mode != 'r')
1763 return (-1);
1764
1765 /*
1766 * Handle special cases...
1767 */
1768
1769 if (pos == 0)
1770 return (cupsFileRewind(fp));
1771
1772 if (fp->ptr)
1773 {
1774 bytes = (ssize_t)(fp->end - fp->buf);
1775
1776 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT, CUPS_LLCAST bytes));
1777
1778 if (pos >= fp->bufpos && pos < (fp->bufpos + bytes))
1779 {
1780 /*
1781 * No seeking necessary...
1782 */
1783
1784 fp->pos = pos;
1785 fp->ptr = fp->buf + pos - fp->bufpos;
1786 fp->eof = 0;
1787
1788 return (pos);
1789 }
1790 }
1791
1792 #ifdef HAVE_LIBZ
1793 if (!fp->compressed && !fp->ptr)
1794 {
1795 /*
1796 * Preload a buffer to determine whether the file is compressed...
1797 */
1798
1799 if (cups_fill(fp) < 0)
1800 return (-1);
1801 }
1802 #endif /* HAVE_LIBZ */
1803
1804 /*
1805 * Seek forwards or backwards...
1806 */
1807
1808 fp->eof = 0;
1809
1810 if (pos < fp->bufpos)
1811 {
1812 /*
1813 * Need to seek backwards...
1814 */
1815
1816 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1817
1818 #ifdef HAVE_LIBZ
1819 if (fp->compressed)
1820 {
1821 inflateEnd(&fp->stream);
1822
1823 lseek(fp->fd, 0, SEEK_SET);
1824 fp->bufpos = 0;
1825 fp->pos = 0;
1826 fp->ptr = NULL;
1827 fp->end = NULL;
1828
1829 while ((bytes = cups_fill(fp)) > 0)
1830 if (pos >= fp->bufpos && pos < (fp->bufpos + bytes))
1831 break;
1832
1833 if (bytes <= 0)
1834 return (-1);
1835
1836 fp->ptr = fp->buf + pos - fp->bufpos;
1837 fp->pos = pos;
1838 }
1839 else
1840 #endif /* HAVE_LIBZ */
1841 {
1842 fp->bufpos = lseek(fp->fd, pos, SEEK_SET);
1843 fp->pos = fp->bufpos;
1844 fp->ptr = NULL;
1845 fp->end = NULL;
1846
1847 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT,
1848 CUPS_LLCAST fp->pos));
1849 }
1850 }
1851 else
1852 {
1853 /*
1854 * Need to seek forwards...
1855 */
1856
1857 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1858
1859 #ifdef HAVE_LIBZ
1860 if (fp->compressed)
1861 {
1862 while ((bytes = cups_fill(fp)) > 0)
1863 {
1864 if (pos >= fp->bufpos && pos < (fp->bufpos + bytes))
1865 break;
1866 }
1867
1868 if (bytes <= 0)
1869 return (-1);
1870
1871 fp->ptr = fp->buf + pos - fp->bufpos;
1872 fp->pos = pos;
1873 }
1874 else
1875 #endif /* HAVE_LIBZ */
1876 {
1877 fp->bufpos = lseek(fp->fd, pos, SEEK_SET);
1878 fp->pos = fp->bufpos;
1879 fp->ptr = NULL;
1880 fp->end = NULL;
1881
1882 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT,
1883 CUPS_LLCAST fp->pos));
1884 }
1885 }
1886
1887 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
1888
1889 return (fp->pos);
1890 }
1891
1892
1893 /*
1894 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1895 *
1896 * @since CUPS 1.2/Mac OS X 10.5@
1897 */
1898
1899 cups_file_t * /* O - CUPS file */
1900 cupsFileStderr(void)
1901 {
1902 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */
1903
1904
1905 /*
1906 * Open file descriptor 2 as needed...
1907 */
1908
1909 if (!cg->stdio_files[2])
1910 {
1911 /*
1912 * Flush any pending output on the stdio file...
1913 */
1914
1915 fflush(stderr);
1916
1917 /*
1918 * Open file descriptor 2...
1919 */
1920
1921 if ((cg->stdio_files[2] = cupsFileOpenFd(2, "w")) != NULL)
1922 cg->stdio_files[2]->is_stdio = 1;
1923 }
1924
1925 return (cg->stdio_files[2]);
1926 }
1927
1928
1929 /*
1930 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1931 *
1932 * @since CUPS 1.2/Mac OS X 10.5@
1933 */
1934
1935 cups_file_t * /* O - CUPS file */
1936 cupsFileStdin(void)
1937 {
1938 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */
1939
1940
1941 /*
1942 * Open file descriptor 0 as needed...
1943 */
1944
1945 if (!cg->stdio_files[0])
1946 {
1947 /*
1948 * Open file descriptor 0...
1949 */
1950
1951 if ((cg->stdio_files[0] = cupsFileOpenFd(0, "r")) != NULL)
1952 cg->stdio_files[0]->is_stdio = 1;
1953 }
1954
1955 return (cg->stdio_files[0]);
1956 }
1957
1958
1959 /*
1960 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1961 *
1962 * @since CUPS 1.2/Mac OS X 10.5@
1963 */
1964
1965 cups_file_t * /* O - CUPS file */
1966 cupsFileStdout(void)
1967 {
1968 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */
1969
1970
1971 /*
1972 * Open file descriptor 1 as needed...
1973 */
1974
1975 if (!cg->stdio_files[1])
1976 {
1977 /*
1978 * Flush any pending output on the stdio file...
1979 */
1980
1981 fflush(stdout);
1982
1983 /*
1984 * Open file descriptor 1...
1985 */
1986
1987 if ((cg->stdio_files[1] = cupsFileOpenFd(1, "w")) != NULL)
1988 cg->stdio_files[1]->is_stdio = 1;
1989 }
1990
1991 return (cg->stdio_files[1]);
1992 }
1993
1994
1995 /*
1996 * 'cupsFileTell()' - Return the current file position.
1997 *
1998 * @since CUPS 1.2/Mac OS X 10.5@
1999 */
2000
2001 off_t /* O - File position */
2002 cupsFileTell(cups_file_t *fp) /* I - CUPS file */
2003 {
2004 DEBUG_printf(("2cupsFileTell(fp=%p)", fp));
2005 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT,
2006 CUPS_LLCAST (fp ? fp->pos : -1)));
2007
2008 return (fp ? fp->pos : 0);
2009 }
2010
2011
2012 /*
2013 * 'cupsFileUnlock()' - Unlock access to a file.
2014 *
2015 * @since CUPS 1.2/Mac OS X 10.5@
2016 */
2017
2018 int /* O - 0 on success, -1 on error */
2019 cupsFileUnlock(cups_file_t *fp) /* I - CUPS file */
2020 {
2021 /*
2022 * Range check...
2023 */
2024
2025 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp));
2026
2027 if (!fp || fp->mode == 's')
2028 return (-1);
2029
2030 /*
2031 * Unlock...
2032 */
2033
2034 #ifdef WIN32
2035 return (_locking(fp->fd, _LK_UNLCK, 0));
2036 #else
2037 return (lockf(fp->fd, F_ULOCK, 0));
2038 #endif /* WIN32 */
2039 }
2040
2041
2042 /*
2043 * 'cupsFileWrite()' - Write to a file.
2044 *
2045 * @since CUPS 1.2/Mac OS X 10.5@
2046 */
2047
2048 ssize_t /* O - Number of bytes written or -1 on error */
2049 cupsFileWrite(cups_file_t *fp, /* I - CUPS file */
2050 const char *buf, /* I - Buffer */
2051 size_t bytes) /* I - Number of bytes to write */
2052 {
2053 /*
2054 * Range check input...
2055 */
2056
2057 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")",
2058 fp, buf, CUPS_LLCAST bytes));
2059
2060 if (!fp || !buf || (fp->mode != 'w' && fp->mode != 's'))
2061 return (-1);
2062
2063 if (bytes == 0)
2064 return (0);
2065
2066 /*
2067 * Write the buffer...
2068 */
2069
2070 if (fp->mode == 's')
2071 {
2072 if (cups_write(fp, buf, bytes) < 0)
2073 return (-1);
2074
2075 fp->pos += (off_t)bytes;
2076
2077 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
2078
2079 return ((ssize_t)bytes);
2080 }
2081
2082 if ((fp->ptr + bytes) > fp->end)
2083 if (cupsFileFlush(fp))
2084 return (-1);
2085
2086 fp->pos += (off_t)bytes;
2087
2088 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
2089
2090 if (bytes > sizeof(fp->buf))
2091 {
2092 #ifdef HAVE_LIBZ
2093 if (fp->compressed)
2094 return (cups_compress(fp, buf, bytes));
2095 else
2096 #endif /* HAVE_LIBZ */
2097 return (cups_write(fp, buf, bytes));
2098 }
2099 else
2100 {
2101 memcpy(fp->ptr, buf, bytes);
2102 fp->ptr += bytes;
2103 return ((ssize_t)bytes);
2104 }
2105 }
2106
2107
2108 #ifdef HAVE_LIBZ
2109 /*
2110 * 'cups_compress()' - Compress a buffer of data.
2111 */
2112
2113 static ssize_t /* O - Number of bytes written or -1 */
2114 cups_compress(cups_file_t *fp, /* I - CUPS file */
2115 const char *buf, /* I - Buffer */
2116 size_t bytes) /* I - Number bytes */
2117 {
2118 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
2119 CUPS_LLCAST bytes));
2120
2121 /*
2122 * Update the CRC...
2123 */
2124
2125 fp->crc = crc32(fp->crc, (const Bytef *)buf, bytes);
2126
2127 /*
2128 * Deflate the bytes...
2129 */
2130
2131 fp->stream.next_in = (Bytef *)buf;
2132 fp->stream.avail_in = bytes;
2133
2134 while (fp->stream.avail_in > 0)
2135 {
2136 /*
2137 * Flush the current buffer...
2138 */
2139
2140 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2141 fp->stream.avail_in, fp->stream.avail_out));
2142
2143 if (fp->stream.avail_out < (int)(sizeof(fp->cbuf) / 8))
2144 {
2145 if (cups_write(fp, (char *)fp->cbuf, fp->stream.next_out - fp->cbuf) < 0)
2146 return (-1);
2147
2148 fp->stream.next_out = fp->cbuf;
2149 fp->stream.avail_out = sizeof(fp->cbuf);
2150 }
2151
2152 deflate(&(fp->stream), Z_NO_FLUSH);
2153 }
2154
2155 return (bytes);
2156 }
2157 #endif /* HAVE_LIBZ */
2158
2159
2160 /*
2161 * 'cups_fill()' - Fill the input buffer.
2162 */
2163
2164 static ssize_t /* O - Number of bytes or -1 */
2165 cups_fill(cups_file_t *fp) /* I - CUPS file */
2166 {
2167 ssize_t bytes; /* Number of bytes read */
2168 #ifdef HAVE_LIBZ
2169 int status; /* Decompression status */
2170 const unsigned char *ptr, /* Pointer into buffer */
2171 *end; /* End of buffer */
2172 #endif /* HAVE_LIBZ */
2173
2174
2175 DEBUG_printf(("7cups_fill(fp=%p)", fp));
2176 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2177 "fp->bufpos=" CUPS_LLFMT ", fp->eof=%d",
2178 fp->ptr, fp->end, fp->buf, CUPS_LLCAST fp->bufpos, fp->eof));
2179
2180 if (fp->ptr && fp->end)
2181 fp->bufpos += fp->end - fp->buf;
2182
2183 #ifdef HAVE_LIBZ
2184 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp->compressed));
2185
2186 while (!fp->ptr || fp->compressed)
2187 {
2188 /*
2189 * Check to see if we have read any data yet; if not, see if we have a
2190 * compressed file...
2191 */
2192
2193 if (!fp->ptr)
2194 {
2195 /*
2196 * Reset the file position in case we are seeking...
2197 */
2198
2199 fp->compressed = 0;
2200
2201 /*
2202 * Read the first bytes in the file to determine if we have a gzip'd
2203 * file...
2204 */
2205
2206 if ((bytes = cups_read(fp, (char *)fp->buf, sizeof(fp->buf))) < 0)
2207 {
2208 /*
2209 * Can't read from file!
2210 */
2211
2212 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT,
2213 CUPS_LLCAST bytes));
2214
2215 return (-1);
2216 }
2217
2218 if (bytes < 10 || fp->buf[0] != 0x1f ||
2219 (fp->buf[1] & 255) != 0x8b ||
2220 fp->buf[2] != 8 || (fp->buf[3] & 0xe0) != 0)
2221 {
2222 /*
2223 * Not a gzip'd file!
2224 */
2225
2226 fp->ptr = fp->buf;
2227 fp->end = fp->buf + bytes;
2228
2229 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT,
2230 CUPS_LLCAST bytes));
2231
2232 return (bytes);
2233 }
2234
2235 /*
2236 * Parse header junk: extra data, original name, and comment...
2237 */
2238
2239 ptr = (unsigned char *)fp->buf + 10;
2240 end = (unsigned char *)fp->buf + bytes;
2241
2242 if (fp->buf[3] & 0x04)
2243 {
2244 /*
2245 * Skip extra data...
2246 */
2247
2248 if ((ptr + 2) > end)
2249 {
2250 /*
2251 * Can't read from file!
2252 */
2253
2254 return (-1);
2255 }
2256
2257 bytes = ((unsigned char)ptr[1] << 8) | (unsigned char)ptr[0];
2258 ptr += 2 + bytes;
2259
2260 if (ptr > end)
2261 {
2262 /*
2263 * Can't read from file!
2264 */
2265
2266 return (-1);
2267 }
2268 }
2269
2270 if (fp->buf[3] & 0x08)
2271 {
2272 /*
2273 * Skip original name data...
2274 */
2275
2276 while (ptr < end && *ptr)
2277 ptr ++;
2278
2279 if (ptr < end)
2280 ptr ++;
2281 else
2282 {
2283 /*
2284 * Can't read from file!
2285 */
2286
2287 return (-1);
2288 }
2289 }
2290
2291 if (fp->buf[3] & 0x10)
2292 {
2293 /*
2294 * Skip comment data...
2295 */
2296
2297 while (ptr < end && *ptr)
2298 ptr ++;
2299
2300 if (ptr < end)
2301 ptr ++;
2302 else
2303 {
2304 /*
2305 * Can't read from file!
2306 */
2307
2308 return (-1);
2309 }
2310 }
2311
2312 if (fp->buf[3] & 0x02)
2313 {
2314 /*
2315 * Skip header CRC data...
2316 */
2317
2318 ptr += 2;
2319
2320 if (ptr > end)
2321 {
2322 /*
2323 * Can't read from file!
2324 */
2325
2326 return (-1);
2327 }
2328 }
2329
2330 /*
2331 * Copy the flate-compressed data to the compression buffer...
2332 */
2333
2334 if ((bytes = end - ptr) > 0)
2335 memcpy(fp->cbuf, ptr, bytes);
2336
2337 /*
2338 * Setup the decompressor data...
2339 */
2340
2341 fp->stream.zalloc = (alloc_func)0;
2342 fp->stream.zfree = (free_func)0;
2343 fp->stream.opaque = (voidpf)0;
2344 fp->stream.next_in = (Bytef *)fp->cbuf;
2345 fp->stream.next_out = NULL;
2346 fp->stream.avail_in = bytes;
2347 fp->stream.avail_out = 0;
2348 fp->crc = crc32(0L, Z_NULL, 0);
2349
2350 if (inflateInit2(&(fp->stream), -15) != Z_OK)
2351 return (-1);
2352
2353 fp->compressed = 1;
2354 }
2355
2356 if (fp->compressed)
2357 {
2358 /*
2359 * If we have reached end-of-file, return immediately...
2360 */
2361
2362 if (fp->eof)
2363 return (-1);
2364
2365 /*
2366 * Fill the decompression buffer as needed...
2367 */
2368
2369 if (fp->stream.avail_in == 0)
2370 {
2371 if ((bytes = cups_read(fp, (char *)fp->cbuf, sizeof(fp->cbuf))) <= 0)
2372 return (-1);
2373
2374 fp->stream.next_in = fp->cbuf;
2375 fp->stream.avail_in = bytes;
2376 }
2377
2378 /*
2379 * Decompress data from the buffer...
2380 */
2381
2382 fp->stream.next_out = (Bytef *)fp->buf;
2383 fp->stream.avail_out = sizeof(fp->buf);
2384
2385 status = inflate(&(fp->stream), Z_NO_FLUSH);
2386
2387 if (fp->stream.next_out > (Bytef *)fp->buf)
2388 fp->crc = crc32(fp->crc, (Bytef *)fp->buf,
2389 fp->stream.next_out - (Bytef *)fp->buf);
2390
2391 if (status == Z_STREAM_END)
2392 {
2393 /*
2394 * Read the CRC and length...
2395 */
2396
2397 unsigned char trailer[8]; /* Trailer bytes */
2398 uLong tcrc; /* Trailer CRC */
2399
2400
2401 if (read(fp->fd, trailer, sizeof(trailer)) < sizeof(trailer))
2402 {
2403 /*
2404 * Can't get it, so mark end-of-file...
2405 */
2406
2407 fp->eof = 1;
2408 }
2409 else
2410 {
2411 tcrc = (((((trailer[3] << 8) | trailer[2]) << 8) | trailer[1]) << 8) |
2412 trailer[0];
2413
2414 if (tcrc != fp->crc)
2415 {
2416 /*
2417 * Bad CRC, mark end-of-file...
2418 */
2419
2420 DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
2421 (unsigned int)tcrc, (unsigned int)fp->crc));
2422
2423 fp->eof = 1;
2424
2425 return (-1);
2426 }
2427
2428 /*
2429 * Otherwise, reset the compressed flag so that we re-read the
2430 * file header...
2431 */
2432
2433 fp->compressed = 0;
2434 }
2435 }
2436
2437 bytes = sizeof(fp->buf) - fp->stream.avail_out;
2438
2439 /*
2440 * Return the decompressed data...
2441 */
2442
2443 fp->ptr = fp->buf;
2444 fp->end = fp->buf + bytes;
2445
2446 if (bytes)
2447 return (bytes);
2448 }
2449 }
2450 #endif /* HAVE_LIBZ */
2451
2452 /*
2453 * Read a buffer's full of data...
2454 */
2455
2456 if ((bytes = cups_read(fp, fp->buf, sizeof(fp->buf))) <= 0)
2457 {
2458 /*
2459 * Can't read from file!
2460 */
2461
2462 fp->eof = 1;
2463 fp->ptr = fp->buf;
2464 fp->end = fp->buf;
2465
2466 return (-1);
2467 }
2468
2469 /*
2470 * Return the bytes we read...
2471 */
2472
2473 fp->eof = 0;
2474 fp->ptr = fp->buf;
2475 fp->end = fp->buf + bytes;
2476
2477 return (bytes);
2478 }
2479
2480
2481 /*
2482 * 'cups_open()' - Safely open a file for writing.
2483 *
2484 * We don't allow appending to directories or files that are hard-linked or
2485 * symlinked.
2486 */
2487
2488 static int /* O - File descriptor or -1 otherwise */
2489 cups_open(const char *filename, /* I - Filename */
2490 int mode) /* I - Open mode */
2491 {
2492 int fd; /* File descriptor */
2493 struct stat fileinfo; /* File information */
2494 #ifndef WIN32
2495 struct stat linkinfo; /* Link information */
2496 #endif /* !WIN32 */
2497
2498
2499 /*
2500 * Open the file...
2501 */
2502
2503 if ((fd = open(filename, mode, 0666)) < 0)
2504 return (-1);
2505
2506 /*
2507 * Then verify that the file descriptor doesn't point to a directory or hard-
2508 * linked file.
2509 */
2510
2511 if (fstat(fd, &fileinfo))
2512 {
2513 close(fd);
2514 return (-1);
2515 }
2516
2517 if (fileinfo.st_nlink != 1)
2518 {
2519 close(fd);
2520 errno = EPERM;
2521 return (-1);
2522 }
2523
2524 #ifdef WIN32
2525 if (fileinfo.st_mode & _S_IFDIR)
2526 #else
2527 if (S_ISDIR(fileinfo.st_mode))
2528 #endif /* WIN32 */
2529 {
2530 close(fd);
2531 errno = EISDIR;
2532 return (-1);
2533 }
2534
2535 #ifndef WIN32
2536 /*
2537 * Then use lstat to determine whether the filename is a symlink...
2538 */
2539
2540 if (lstat(filename, &linkinfo))
2541 {
2542 close(fd);
2543 return (-1);
2544 }
2545
2546 if (S_ISLNK(linkinfo.st_mode) ||
2547 fileinfo.st_dev != linkinfo.st_dev ||
2548 fileinfo.st_ino != linkinfo.st_ino ||
2549 #ifdef HAVE_ST_GEN
2550 fileinfo.st_gen != linkinfo.st_gen ||
2551 #endif /* HAVE_ST_GEN */
2552 fileinfo.st_nlink != linkinfo.st_nlink ||
2553 fileinfo.st_mode != linkinfo.st_mode)
2554 {
2555 /*
2556 * Yes, don't allow!
2557 */
2558
2559 close(fd);
2560 errno = EPERM;
2561 return (-1);
2562 }
2563 #endif /* !WIN32 */
2564
2565 return (fd);
2566 }
2567
2568
2569 /*
2570 * 'cups_read()' - Read from a file descriptor.
2571 */
2572
2573 static ssize_t /* O - Number of bytes read or -1 */
2574 cups_read(cups_file_t *fp, /* I - CUPS file */
2575 char *buf, /* I - Buffer */
2576 size_t bytes) /* I - Number bytes */
2577 {
2578 ssize_t total; /* Total bytes read */
2579
2580
2581 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
2582 CUPS_LLCAST bytes));
2583
2584 /*
2585 * Loop until we read at least 0 bytes...
2586 */
2587
2588 for (;;)
2589 {
2590 #ifdef WIN32
2591 if (fp->mode == 's')
2592 total = (ssize_t)recv(fp->fd, buf, (unsigned)bytes, 0);
2593 else
2594 total = (ssize_t)read(fp->fd, buf, (unsigned)bytes);
2595 #else
2596 if (fp->mode == 's')
2597 total = recv(fp->fd, buf, bytes, 0);
2598 else
2599 total = read(fp->fd, buf, bytes);
2600 #endif /* WIN32 */
2601
2602 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT, CUPS_LLCAST total));
2603
2604 if (total >= 0)
2605 break;
2606
2607 /*
2608 * Reads can be interrupted by signals and unavailable resources...
2609 */
2610
2611 if (errno == EAGAIN || errno == EINTR)
2612 continue;
2613 else
2614 return (-1);
2615 }
2616
2617 /*
2618 * Return the total number of bytes read...
2619 */
2620
2621 return (total);
2622 }
2623
2624
2625 /*
2626 * 'cups_write()' - Write to a file descriptor.
2627 */
2628
2629 static ssize_t /* O - Number of bytes written or -1 */
2630 cups_write(cups_file_t *fp, /* I - CUPS file */
2631 const char *buf, /* I - Buffer */
2632 size_t bytes) /* I - Number bytes */
2633 {
2634 size_t total; /* Total bytes written */
2635 ssize_t count; /* Count this time */
2636
2637
2638 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
2639 CUPS_LLCAST bytes));
2640
2641 /*
2642 * Loop until all bytes are written...
2643 */
2644
2645 total = 0;
2646 while (bytes > 0)
2647 {
2648 #ifdef WIN32
2649 if (fp->mode == 's')
2650 count = (ssize_t)send(fp->fd, buf, (unsigned)bytes, 0);
2651 else
2652 count = (ssize_t)write(fp->fd, buf, (unsigned)bytes);
2653 #else
2654 if (fp->mode == 's')
2655 count = send(fp->fd, buf, bytes, 0);
2656 else
2657 count = write(fp->fd, buf, bytes);
2658 #endif /* WIN32 */
2659
2660 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT, CUPS_LLCAST count));
2661
2662 if (count < 0)
2663 {
2664 /*
2665 * Writes can be interrupted by signals and unavailable resources...
2666 */
2667
2668 if (errno == EAGAIN || errno == EINTR)
2669 continue;
2670 else
2671 return (-1);
2672 }
2673
2674 /*
2675 * Update the counts for the last write call...
2676 */
2677
2678 bytes -= count;
2679 total += count;
2680 buf += count;
2681 }
2682
2683 /*
2684 * Return the total number of bytes written...
2685 */
2686
2687 return ((ssize_t)total);
2688 }
2689
2690
2691 /*
2692 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".
2693 */