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