New proplib-0.4 from http://code.google.com/p/portableproplib.
[portableproplib.git] / src / prop_object.c
blob570d9ea3bebf3d78ba7f29541138e64fbf7da121
1 /* $NetBSD: prop_object.c,v 1.23 2008/11/30 00:17:07 haad Exp $ */
3 /*-
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "prop_object.h"
33 #include "prop_object_impl.h"
35 #include <sys/mman.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <unistd.h>
42 #include <zlib.h>
45 * _prop_object_init --
46 * Initialize an object. Called when sub-classes create
47 * an instance.
49 void
50 _prop_object_init(struct _prop_object *po, const struct _prop_object_type *pot)
53 po->po_type = pot;
54 po->po_refcnt = 1;
58 * _prop_object_fini --
59 * Finalize an object. Called when sub-classes destroy
60 * an instance.
62 /*ARGSUSED*/
63 void
64 _prop_object_fini(struct _prop_object *po _PROP_ARG_UNUSED)
66 /* Nothing to do, currently. */
70 * _prop_object_externalize_start_tag --
71 * Append an XML-style start tag to the externalize buffer.
73 bool
74 _prop_object_externalize_start_tag(
75 struct _prop_object_externalize_context *ctx, const char *tag)
77 unsigned int i;
79 for (i = 0; i < ctx->poec_depth; i++) {
80 if (_prop_object_externalize_append_char(ctx, '\t') == false)
81 return (false);
83 if (_prop_object_externalize_append_char(ctx, '<') == false ||
84 _prop_object_externalize_append_cstring(ctx, tag) == false ||
85 _prop_object_externalize_append_char(ctx, '>') == false)
86 return (false);
88 return (true);
92 * _prop_object_externalize_end_tag --
93 * Append an XML-style end tag to the externalize buffer.
95 bool
96 _prop_object_externalize_end_tag(
97 struct _prop_object_externalize_context *ctx, const char *tag)
100 if (_prop_object_externalize_append_char(ctx, '<') == false ||
101 _prop_object_externalize_append_char(ctx, '/') == false ||
102 _prop_object_externalize_append_cstring(ctx, tag) == false ||
103 _prop_object_externalize_append_char(ctx, '>') == false ||
104 _prop_object_externalize_append_char(ctx, '\n') == false)
105 return (false);
107 return (true);
111 * _prop_object_externalize_empty_tag --
112 * Append an XML-style empty tag to the externalize buffer.
114 bool
115 _prop_object_externalize_empty_tag(
116 struct _prop_object_externalize_context *ctx, const char *tag)
118 unsigned int i;
120 for (i = 0; i < ctx->poec_depth; i++) {
121 if (_prop_object_externalize_append_char(ctx, '\t') == false)
122 return (false);
125 if (_prop_object_externalize_append_char(ctx, '<') == false ||
126 _prop_object_externalize_append_cstring(ctx, tag) == false ||
127 _prop_object_externalize_append_char(ctx, '/') == false ||
128 _prop_object_externalize_append_char(ctx, '>') == false ||
129 _prop_object_externalize_append_char(ctx, '\n') == false)
130 return (false);
132 return (true);
136 * _prop_object_externalize_append_cstring --
137 * Append a C string to the externalize buffer.
139 bool
140 _prop_object_externalize_append_cstring(
141 struct _prop_object_externalize_context *ctx, const char *cp)
144 while (*cp != '\0') {
145 if (_prop_object_externalize_append_char(ctx,
146 (unsigned char) *cp) == false)
147 return (false);
148 cp++;
151 return (true);
155 * _prop_object_externalize_append_encoded_cstring --
156 * Append an encoded C string to the externalize buffer.
158 bool
159 _prop_object_externalize_append_encoded_cstring(
160 struct _prop_object_externalize_context *ctx, const char *cp)
163 while (*cp != '\0') {
164 switch (*cp) {
165 case '<':
166 if (_prop_object_externalize_append_cstring(ctx,
167 "&lt;") == false)
168 return (false);
169 break;
170 case '>':
171 if (_prop_object_externalize_append_cstring(ctx,
172 "&gt;") == false)
173 return (false);
174 break;
175 case '&':
176 if (_prop_object_externalize_append_cstring(ctx,
177 "&amp;") == false)
178 return (false);
179 break;
180 default:
181 if (_prop_object_externalize_append_char(ctx,
182 (unsigned char) *cp) == false)
183 return (false);
184 break;
186 cp++;
189 return (true);
192 #define BUF_EXPAND 256
195 * _prop_object_externalize_append_char --
196 * Append a single character to the externalize buffer.
198 bool
199 _prop_object_externalize_append_char(
200 struct _prop_object_externalize_context *ctx, unsigned char c)
203 _PROP_ASSERT(ctx->poec_capacity != 0);
204 _PROP_ASSERT(ctx->poec_buf != NULL);
205 _PROP_ASSERT(ctx->poec_len <= ctx->poec_capacity);
207 if (ctx->poec_len == ctx->poec_capacity) {
208 char *cp = _PROP_REALLOC(ctx->poec_buf,
209 ctx->poec_capacity + BUF_EXPAND,
210 M_TEMP);
211 if (cp == NULL)
212 return (false);
213 ctx->poec_capacity = ctx->poec_capacity + BUF_EXPAND;
214 ctx->poec_buf = cp;
217 ctx->poec_buf[ctx->poec_len++] = c;
219 return (true);
223 * _prop_object_externalize_header --
224 * Append the standard XML header to the externalize buffer.
226 bool
227 _prop_object_externalize_header(struct _prop_object_externalize_context *ctx)
229 static const char _plist_xml_header[] =
230 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
231 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
233 if (_prop_object_externalize_append_cstring(ctx,
234 _plist_xml_header) == false ||
235 _prop_object_externalize_start_tag(ctx,
236 "plist version=\"1.0\"") == false ||
237 _prop_object_externalize_append_char(ctx, '\n') == false)
238 return (false);
240 return (true);
244 * _prop_object_externalize_footer --
245 * Append the standard XML footer to the externalize buffer. This
246 * also NUL-terminates the buffer.
248 bool
249 _prop_object_externalize_footer(struct _prop_object_externalize_context *ctx)
252 if (_prop_object_externalize_end_tag(ctx, "plist") == false ||
253 _prop_object_externalize_append_char(ctx, '\0') == false)
254 return (false);
256 return (true);
260 * _prop_object_externalize_context_alloc --
261 * Allocate an externalize context.
263 struct _prop_object_externalize_context *
264 _prop_object_externalize_context_alloc(void)
266 struct _prop_object_externalize_context *ctx;
268 ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
269 if (ctx != NULL) {
270 ctx->poec_buf = _PROP_MALLOC(BUF_EXPAND, M_TEMP);
271 if (ctx->poec_buf == NULL) {
272 _PROP_FREE(ctx, M_TEMP);
273 return (NULL);
275 ctx->poec_len = 0;
276 ctx->poec_capacity = BUF_EXPAND;
277 ctx->poec_depth = 0;
279 return (ctx);
283 * _prop_object_externalize_context_free --
284 * Free an externalize context.
286 void
287 _prop_object_externalize_context_free(
288 struct _prop_object_externalize_context *ctx)
291 /* Buffer is always freed by the caller. */
292 _PROP_FREE(ctx, M_TEMP);
296 * _prop_object_internalize_skip_comment --
297 * Skip the body and end tag of a comment.
299 static bool
300 _prop_object_internalize_skip_comment(
301 struct _prop_object_internalize_context *ctx)
303 const char *cp = ctx->poic_cp;
305 while (!_PROP_EOF(*cp)) {
306 if (cp[0] == '-' &&
307 cp[1] == '-' &&
308 cp[2] == '>') {
309 ctx->poic_cp = cp + 3;
310 return (true);
312 cp++;
315 return (false); /* ran out of buffer */
319 * _prop_object_internalize_find_tag --
320 * Find the next tag in an XML stream. Optionally compare the found
321 * tag to an expected tag name. State of the context is undefined
322 * if this routine returns false. Upon success, the context points
323 * to the first octet after the tag.
325 bool
326 _prop_object_internalize_find_tag(struct _prop_object_internalize_context *ctx,
327 const char *tag, _prop_tag_type_t type)
329 const char *cp;
330 size_t taglen;
332 if (tag != NULL)
333 taglen = strlen(tag);
334 else
335 taglen = 0;
337 start_over:
338 cp = ctx->poic_cp;
341 * Find the start of the tag.
343 while (_PROP_ISSPACE(*cp))
344 cp++;
345 if (_PROP_EOF(*cp))
346 return (false);
348 if (*cp != '<')
349 return (false);
351 ctx->poic_tag_start = cp++;
352 if (_PROP_EOF(*cp))
353 return (false);
355 if (*cp == '!') {
356 if (cp[1] != '-' || cp[2] != '-')
357 return (false);
359 * Comment block -- only allowed if we are allowed to
360 * return a start tag.
362 if (type == _PROP_TAG_TYPE_END)
363 return (false);
364 ctx->poic_cp = cp + 3;
365 if (_prop_object_internalize_skip_comment(ctx) == false)
366 return (false);
367 goto start_over;
370 if (*cp == '/') {
371 if (type != _PROP_TAG_TYPE_END &&
372 type != _PROP_TAG_TYPE_EITHER)
373 return (false);
374 cp++;
375 if (_PROP_EOF(*cp))
376 return (false);
377 ctx->poic_tag_type = _PROP_TAG_TYPE_END;
378 } else {
379 if (type != _PROP_TAG_TYPE_START &&
380 type != _PROP_TAG_TYPE_EITHER)
381 return (false);
382 ctx->poic_tag_type = _PROP_TAG_TYPE_START;
385 ctx->poic_tagname = cp;
387 while (!_PROP_ISSPACE(*cp) && *cp != '/' && *cp != '>')
388 cp++;
389 if (_PROP_EOF(*cp))
390 return (false);
392 ctx->poic_tagname_len = cp - ctx->poic_tagname;
394 /* Make sure this is the tag we're looking for. */
395 if (tag != NULL &&
396 (taglen != ctx->poic_tagname_len ||
397 memcmp(tag, ctx->poic_tagname, taglen) != 0))
398 return (false);
400 /* Check for empty tag. */
401 if (*cp == '/') {
402 if (ctx->poic_tag_type != _PROP_TAG_TYPE_START)
403 return(false); /* only valid on start tags */
404 ctx->poic_is_empty_element = true;
405 cp++;
406 if (_PROP_EOF(*cp) || *cp != '>')
407 return (false);
408 } else
409 ctx->poic_is_empty_element = false;
411 /* Easy case of no arguments. */
412 if (*cp == '>') {
413 ctx->poic_tagattr = NULL;
414 ctx->poic_tagattr_len = 0;
415 ctx->poic_tagattrval = NULL;
416 ctx->poic_tagattrval_len = 0;
417 ctx->poic_cp = cp + 1;
418 return (true);
421 _PROP_ASSERT(!_PROP_EOF(*cp));
422 cp++;
423 if (_PROP_EOF(*cp))
424 return (false);
426 while (_PROP_ISSPACE(*cp))
427 cp++;
428 if (_PROP_EOF(*cp))
429 return (false);
431 ctx->poic_tagattr = cp;
433 while (!_PROP_ISSPACE(*cp) && *cp != '=')
434 cp++;
435 if (_PROP_EOF(*cp))
436 return (false);
438 ctx->poic_tagattr_len = cp - ctx->poic_tagattr;
440 cp++;
441 if (*cp != '\"')
442 return (false);
443 cp++;
444 if (_PROP_EOF(*cp))
445 return (false);
447 ctx->poic_tagattrval = cp;
448 while (*cp != '\"')
449 cp++;
450 if (_PROP_EOF(*cp))
451 return (false);
452 ctx->poic_tagattrval_len = cp - ctx->poic_tagattrval;
454 cp++;
455 if (*cp != '>')
456 return (false);
458 ctx->poic_cp = cp + 1;
459 return (true);
463 * _prop_object_internalize_decode_string --
464 * Decode an encoded string.
466 bool
467 _prop_object_internalize_decode_string(
468 struct _prop_object_internalize_context *ctx,
469 char *target, size_t targsize, size_t *sizep,
470 const char **cpp)
472 const char *src;
473 size_t tarindex;
474 char c;
476 tarindex = 0;
477 src = ctx->poic_cp;
479 for (;;) {
480 if (_PROP_EOF(*src))
481 return (false);
482 if (*src == '<') {
483 break;
486 if ((c = *src) == '&') {
487 if (src[1] == 'a' &&
488 src[2] == 'm' &&
489 src[3] == 'p' &&
490 src[4] == ';') {
491 c = '&';
492 src += 5;
493 } else if (src[1] == 'l' &&
494 src[2] == 't' &&
495 src[3] == ';') {
496 c = '<';
497 src += 4;
498 } else if (src[1] == 'g' &&
499 src[2] == 't' &&
500 src[3] == ';') {
501 c = '>';
502 src += 4;
503 } else if (src[1] == 'a' &&
504 src[2] == 'p' &&
505 src[3] == 'o' &&
506 src[4] == 's' &&
507 src[5] == ';') {
508 c = '\'';
509 src += 6;
510 } else if (src[1] == 'q' &&
511 src[2] == 'u' &&
512 src[3] == 'o' &&
513 src[4] == 't' &&
514 src[5] == ';') {
515 c = '\"';
516 src += 6;
517 } else
518 return (false);
519 } else
520 src++;
521 if (target) {
522 if (tarindex >= targsize)
523 return (false);
524 target[tarindex] = c;
526 tarindex++;
529 _PROP_ASSERT(*src == '<');
530 if (sizep != NULL)
531 *sizep = tarindex;
532 if (cpp != NULL)
533 *cpp = src;
535 return (true);
539 * _prop_object_internalize_match --
540 * Returns true if the two character streams match.
542 bool
543 _prop_object_internalize_match(const char *str1, size_t len1,
544 const char *str2, size_t len2)
547 return (len1 == len2 && memcmp(str1, str2, len1) == 0);
550 #define INTERNALIZER(t, f) \
551 { t, sizeof(t) - 1, f }
553 static const struct _prop_object_internalizer {
554 const char *poi_tag;
555 size_t poi_taglen;
556 prop_object_internalizer_t poi_intern;
557 } _prop_object_internalizer_table[] = {
558 INTERNALIZER("array", _prop_array_internalize),
560 INTERNALIZER("true", _prop_bool_internalize),
561 INTERNALIZER("false", _prop_bool_internalize),
563 INTERNALIZER("data", _prop_data_internalize),
565 INTERNALIZER("dict", _prop_dictionary_internalize),
567 INTERNALIZER("integer", _prop_number_internalize),
569 INTERNALIZER("string", _prop_string_internalize),
571 { 0, 0, NULL }
574 #undef INTERNALIZER
577 * _prop_object_internalize_by_tag --
578 * Determine the object type from the tag in the context and
579 * internalize it.
581 prop_object_t
582 _prop_object_internalize_by_tag(struct _prop_object_internalize_context *ctx)
584 const struct _prop_object_internalizer *poi;
585 prop_object_t obj, parent_obj;
586 void *data, *iter;
587 prop_object_internalizer_continue_t iter_func;
588 struct _prop_stack stack;
590 _prop_stack_init(&stack);
592 match_start:
593 for (poi = _prop_object_internalizer_table;
594 poi->poi_tag != NULL; poi++) {
595 if (_prop_object_internalize_match(ctx->poic_tagname,
596 ctx->poic_tagname_len,
597 poi->poi_tag,
598 poi->poi_taglen))
599 break;
601 if (poi == NULL) {
602 while (_prop_stack_pop(&stack, &obj, &iter, &data, NULL)) {
603 iter_func = (prop_object_internalizer_continue_t)iter;
604 (*iter_func)(&stack, &obj, ctx, data, NULL);
607 return (NULL);
610 obj = NULL;
611 if (!(*poi->poi_intern)(&stack, &obj, ctx))
612 goto match_start;
614 parent_obj = obj;
615 while (_prop_stack_pop(&stack, &parent_obj, &iter, &data, NULL)) {
616 iter_func = (prop_object_internalizer_continue_t)iter;
617 if (!(*iter_func)(&stack, &parent_obj, ctx, data, obj))
618 goto match_start;
619 obj = parent_obj;
622 return (parent_obj);
625 prop_object_t
626 _prop_generic_internalize(const char *xml, const char *master_tag)
628 prop_object_t obj = NULL;
629 struct _prop_object_internalize_context *ctx;
631 ctx = _prop_object_internalize_context_alloc(xml);
632 if (ctx == NULL)
633 return (NULL);
635 /* We start with a <plist> tag. */
636 if (_prop_object_internalize_find_tag(ctx, "plist",
637 _PROP_TAG_TYPE_START) == false)
638 goto out;
640 /* Plist elements cannot be empty. */
641 if (ctx->poic_is_empty_element)
642 goto out;
645 * We don't understand any plist attributes, but Apple XML
646 * property lists often have a "version" attribute. If we
647 * see that one, we simply ignore it.
649 if (ctx->poic_tagattr != NULL &&
650 !_PROP_TAGATTR_MATCH(ctx, "version"))
651 goto out;
653 /* Next we expect to see opening master_tag. */
654 if (_prop_object_internalize_find_tag(ctx, master_tag,
655 _PROP_TAG_TYPE_START) == false)
656 goto out;
658 obj = _prop_object_internalize_by_tag(ctx);
659 if (obj == NULL)
660 goto out;
663 * We've advanced past the closing master_tag.
664 * Now we want </plist>.
666 if (_prop_object_internalize_find_tag(ctx, "plist",
667 _PROP_TAG_TYPE_END) == false) {
668 prop_object_release(obj);
669 obj = NULL;
672 out:
673 _prop_object_internalize_context_free(ctx);
674 return (obj);
678 * _prop_object_internalize_context_alloc --
679 * Allocate an internalize context.
681 struct _prop_object_internalize_context *
682 _prop_object_internalize_context_alloc(const char *xml)
684 struct _prop_object_internalize_context *ctx;
686 ctx = _PROP_MALLOC(sizeof(struct _prop_object_internalize_context),
687 M_TEMP);
688 if (ctx == NULL)
689 return (NULL);
691 ctx->poic_xml = ctx->poic_cp = xml;
694 * Skip any whitespace and XML preamble stuff that we don't
695 * know about / care about.
697 for (;;) {
698 while (_PROP_ISSPACE(*xml))
699 xml++;
700 if (_PROP_EOF(*xml) || *xml != '<')
701 goto bad;
703 #define MATCH(str) (memcmp(&xml[1], str, sizeof(str) - 1) == 0)
706 * Skip over the XML preamble that Apple XML property
707 * lists usually include at the top of the file.
709 if (MATCH("?xml ") ||
710 MATCH("!DOCTYPE plist")) {
711 while (*xml != '>' && !_PROP_EOF(*xml))
712 xml++;
713 if (_PROP_EOF(*xml))
714 goto bad;
715 xml++; /* advance past the '>' */
716 continue;
719 if (MATCH("<!--")) {
720 ctx->poic_cp = xml + 4;
721 if (_prop_object_internalize_skip_comment(ctx) == false)
722 goto bad;
723 xml = ctx->poic_cp;
724 continue;
727 #undef MATCH
730 * We don't think we should skip it, so let's hope we can
731 * parse it.
733 break;
736 ctx->poic_cp = xml;
737 return (ctx);
738 bad:
739 _PROP_FREE(ctx, M_TEMP);
740 return (NULL);
744 * _prop_object_internalize_context_free --
745 * Free an internalize context.
747 void
748 _prop_object_internalize_context_free(
749 struct _prop_object_internalize_context *ctx)
752 _PROP_FREE(ctx, M_TEMP);
756 * _prop_object_externalize_file_dirname --
757 * dirname(3), basically. We have to roll our own because the
758 * system dirname(3) isn't reentrant.
760 static void
761 _prop_object_externalize_file_dirname(const char *path, char *result)
763 const char *lastp;
764 size_t len;
767 * If `path' is a NULL pointer or points to an empty string,
768 * return ".".
770 if (path == NULL || *path == '\0')
771 goto singledot;
773 /* String trailing slashes, if any. */
774 lastp = path + strlen(path) - 1;
775 while (lastp != path && *lastp == '/')
776 lastp--;
778 /* Terminate path at the last occurrence of '/'. */
779 do {
780 if (*lastp == '/') {
781 /* Strip trailing slashes, if any. */
782 while (lastp != path && *lastp == '/')
783 lastp--;
785 /* ...and copy the result into the result buffer. */
786 len = (lastp - path) + 1 /* last char */;
787 if (len > (PATH_MAX - 1))
788 len = PATH_MAX - 1;
790 memcpy(result, path, len);
791 result[len] = '\0';
792 return;
794 } while (--lastp >= path);
796 /* No /'s found, return ".". */
797 singledot:
798 strcpy(result, ".");
802 * _prop_object_externalize_write_file --
803 * Write an externalized dictionary to the specified file.
804 * The file is written atomically from the caller's perspective,
805 * and the mode set to 0666 modified by the caller's umask.
807 * The 'compress' argument enables gzip (via zlib) compression
808 * for the file to be written. If zlib support is disabled it's
809 * simply ignored.
811 bool
812 _prop_object_externalize_write_file(const char *fname, const char *xml,
813 size_t len, bool compress)
815 gzFile *gzf = NULL;
816 char tname[PATH_MAX], *otname;
817 int fd;
818 int save_errno;
819 mode_t myumask;
821 if (len > SSIZE_MAX) {
822 errno = EFBIG;
823 return (false);
827 * Get the directory name where the file is to be written
828 * and create the temporary file.
830 _prop_object_externalize_file_dirname(fname, tname);
832 #ifdef HAVE_STRLCAT
833 if (strlcat(tname, "/.plistXXXXXX", sizeof(tname)) >= sizeof(tname)) {
834 errno = ENAMETOOLONG;
835 return (false);
837 #else
838 otname = strncat(tname, "/.plistXXXXXX", sizeof(tname));
840 if (sizeof(*otname) >= sizeof(tname)) {
841 errno = ENAMETOOLONG;
842 return (false);
844 #endif
845 if ((fd = mkstemp(tname)) == -1)
846 return (false);
848 if ((gzf = gzdopen(fd, "a")) == NULL)
849 goto bad;
851 if (gzsetparams(gzf, compress ? Z_BEST_COMPRESSION : Z_NO_COMPRESSION,
852 Z_DEFAULT_STRATEGY))
853 goto bad;
855 if (gzwrite(gzf, xml, len) != (ssize_t)len)
856 goto bad;
858 if (fsync(fd) == -1)
859 goto bad;
861 myumask = umask(0);
862 (void)umask(myumask);
863 if (fchmod(fd, 0666 & ~myumask) == -1)
864 goto bad;
866 (void)gzclose(gzf);
867 fd = -1;
869 if (rename(tname, fname) == -1)
870 goto bad;
872 return (true);
874 bad:
875 save_errno = errno;
876 if (gzf != NULL)
877 (void)gzclose(gzf);
878 (void) unlink(tname);
879 errno = save_errno;
880 return (false);
884 * _prop_object_internalize_map_file --
885 * Map a file for the purpose of internalizing it.
887 struct _prop_object_internalize_mapped_file *
888 _prop_object_internalize_map_file(const char *fname)
890 struct stat sb;
891 struct _prop_object_internalize_mapped_file *mf;
892 size_t pgsize = (size_t)sysconf(_SC_PAGESIZE);
893 size_t pgmask = pgsize - 1;
894 bool need_guard = false;
895 int fd;
897 mf = _PROP_MALLOC(sizeof(*mf), M_TEMP);
898 if (mf == NULL)
899 return (NULL);
901 fd = open(fname, O_RDONLY, 0400);
902 if (fd == -1) {
903 _PROP_FREE(mf, M_TEMP);
904 return (NULL);
907 if (fstat(fd, &sb) == -1) {
908 (void) close(fd);
909 _PROP_FREE(mf, M_TEMP);
910 return (NULL);
912 mf->poimf_mapsize = ((size_t)sb.st_size + pgmask) & ~pgmask;
913 if (mf->poimf_mapsize < sb.st_size) {
914 (void) close(fd);
915 _PROP_FREE(mf, M_TEMP);
916 return (NULL);
920 * If the file length is an integral number of pages, then we
921 * need to map a guard page at the end in order to provide the
922 * necessary NUL-termination of the buffer.
924 if ((sb.st_size & pgmask) == 0)
925 need_guard = true;
927 mf->poimf_xml = mmap(NULL, need_guard ? mf->poimf_mapsize + pgsize
928 : mf->poimf_mapsize,
929 PROT_READ, MAP_FILE|MAP_SHARED, fd, (off_t)0);
930 (void) close(fd);
931 if (mf->poimf_xml == MAP_FAILED) {
932 _PROP_FREE(mf, M_TEMP);
933 return (NULL);
935 (void) madvise(mf->poimf_xml, mf->poimf_mapsize, MADV_SEQUENTIAL);
937 if (need_guard) {
938 if (mmap(mf->poimf_xml + mf->poimf_mapsize,
939 pgsize, PROT_READ,
940 MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1,
941 (off_t)0) == MAP_FAILED) {
942 (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
943 _PROP_FREE(mf, M_TEMP);
944 return (NULL);
946 mf->poimf_mapsize += pgsize;
949 return (mf);
953 * _prop_object_internalize_unmap_file --
954 * Unmap a file previously mapped for internalizing.
956 void
957 _prop_object_internalize_unmap_file(
958 struct _prop_object_internalize_mapped_file *mf)
961 (void) madvise(mf->poimf_xml, mf->poimf_mapsize, MADV_DONTNEED);
962 (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
963 _PROP_FREE(mf->poimf_uncomp_xml, M_TEMP);
964 _PROP_FREE(mf, M_TEMP);
968 * Retain / release serialization --
970 * Eventually we would like to use atomic operations. But until we have
971 * an MI API for them that is common to userland and the kernel, we will
972 * use a lock instead.
974 * We use a single global mutex for all serialization. In the kernel, because
975 * we are still under a biglock, this will basically never contend (properties
976 * cannot be manipulated at interrupt level). In userland, this will cost
977 * nothing for single-threaded programs. For multi-threaded programs, there
978 * could be contention, but it probably won't cost that much unless the program
979 * makes heavy use of property lists.
981 _PROP_MUTEX_DECL_STATIC(_prop_refcnt_mutex)
982 #define _PROP_REFCNT_LOCK() _PROP_MUTEX_LOCK(_prop_refcnt_mutex)
983 #define _PROP_REFCNT_UNLOCK() _PROP_MUTEX_UNLOCK(_prop_refcnt_mutex)
986 * prop_object_retain --
987 * Increment the reference count on an object.
989 void
990 prop_object_retain(prop_object_t obj)
992 struct _prop_object *po = obj;
993 uint32_t ocnt;
995 _PROP_REFCNT_LOCK();
996 ocnt = po->po_refcnt++;
997 _PROP_REFCNT_UNLOCK();
999 _PROP_ASSERT(ocnt != 0xffffffffU);
1003 * prop_object_release_emergency
1004 * A direct free with prop_object_release failed.
1005 * Walk down the tree until a leaf is found and
1006 * free that. Do not recurse to avoid stack overflows.
1008 * This is a slow edge condition, but necessary to
1009 * guarantee that an object can always be freed.
1011 static void
1012 prop_object_release_emergency(prop_object_t obj)
1014 struct _prop_object *po;
1015 void (*unlock)(void);
1016 prop_object_t parent = NULL;
1017 uint32_t ocnt;
1019 for (;;) {
1020 po = obj;
1021 _PROP_ASSERT(obj);
1023 if (po->po_type->pot_lock != NULL)
1024 po->po_type->pot_lock();
1026 /* Save pointerto unlock function */
1027 unlock = po->po_type->pot_unlock;
1029 _PROP_REFCNT_LOCK();
1030 ocnt = po->po_refcnt--;
1031 _PROP_REFCNT_UNLOCK();
1033 _PROP_ASSERT(ocnt != 0);
1034 if (ocnt != 1) {
1035 if (unlock != NULL)
1036 unlock();
1037 break;
1040 _PROP_ASSERT(po->po_type);
1041 if ((po->po_type->pot_free)(NULL, &obj) ==
1042 _PROP_OBJECT_FREE_DONE) {
1043 if (unlock != NULL)
1044 unlock();
1045 break;
1048 if (unlock != NULL)
1049 unlock();
1051 parent = po;
1052 _PROP_REFCNT_LOCK();
1053 ++po->po_refcnt;
1054 _PROP_REFCNT_UNLOCK();
1056 _PROP_ASSERT(parent);
1057 /* One object was just freed. */
1058 po = parent;
1059 (*po->po_type->pot_emergency_free)(parent);
1063 * prop_object_release --
1064 * Decrement the reference count on an object.
1066 * Free the object if we are releasing the final
1067 * reference.
1069 void
1070 prop_object_release(prop_object_t obj)
1072 struct _prop_object *po;
1073 struct _prop_stack stack;
1074 void (*unlock)(void);
1075 int ret;
1076 uint32_t ocnt;
1078 _prop_stack_init(&stack);
1080 do {
1081 do {
1082 po = obj;
1083 _PROP_ASSERT(obj);
1085 if (po->po_type->pot_lock != NULL)
1086 po->po_type->pot_lock();
1088 /* Save pointer to object unlock function */
1089 unlock = po->po_type->pot_unlock;
1091 _PROP_REFCNT_LOCK();
1092 ocnt = po->po_refcnt--;
1093 _PROP_REFCNT_UNLOCK();
1095 _PROP_ASSERT(ocnt != 0);
1096 if (ocnt != 1) {
1097 ret = 0;
1098 if (unlock != NULL)
1099 unlock();
1100 break;
1103 ret = (po->po_type->pot_free)(&stack, &obj);
1105 if (unlock != NULL)
1106 unlock();
1108 if (ret == _PROP_OBJECT_FREE_DONE)
1109 break;
1111 _PROP_REFCNT_LOCK();
1112 ++po->po_refcnt;
1113 _PROP_REFCNT_UNLOCK();
1114 } while (ret == _PROP_OBJECT_FREE_RECURSE);
1115 if (ret == _PROP_OBJECT_FREE_FAILED)
1116 prop_object_release_emergency(obj);
1117 } while (_prop_stack_pop(&stack, &obj, NULL, NULL, NULL));
1121 * prop_object_type --
1122 * Return the type of an object.
1124 prop_type_t
1125 prop_object_type(prop_object_t obj)
1127 struct _prop_object *po = obj;
1129 if (obj == NULL)
1130 return (PROP_TYPE_UNKNOWN);
1132 return (po->po_type->pot_type);
1136 * prop_object_equals --
1137 * Returns true if thw two objects are equivalent.
1139 bool
1140 prop_object_equals(prop_object_t obj1, prop_object_t obj2)
1142 return (prop_object_equals_with_error(obj1, obj2, NULL));
1145 bool
1146 prop_object_equals_with_error(prop_object_t obj1, prop_object_t obj2,
1147 bool *error_flag)
1149 struct _prop_object *po1;
1150 struct _prop_object *po2;
1151 void *stored_pointer1, *stored_pointer2;
1152 prop_object_t next_obj1, next_obj2;
1153 struct _prop_stack stack;
1154 _prop_object_equals_rv_t ret;
1156 _prop_stack_init(&stack);
1157 if (error_flag)
1158 *error_flag = false;
1160 start_subtree:
1161 stored_pointer1 = NULL;
1162 stored_pointer2 = NULL;
1163 po1 = obj1;
1164 po2 = obj2;
1166 if (po1->po_type != po2->po_type)
1167 return (false);
1169 continue_subtree:
1170 ret = (*po1->po_type->pot_equals)(obj1, obj2,
1171 &stored_pointer1, &stored_pointer2,
1172 &next_obj1, &next_obj2);
1173 if (ret == _PROP_OBJECT_EQUALS_FALSE)
1174 goto finish;
1175 if (ret == _PROP_OBJECT_EQUALS_TRUE) {
1176 if (!_prop_stack_pop(&stack, &obj1, &obj2,
1177 &stored_pointer1, &stored_pointer2))
1178 return true;
1179 goto continue_subtree;
1181 _PROP_ASSERT(ret == _PROP_OBJECT_EQUALS_RECURSE);
1183 if (!_prop_stack_push(&stack, obj1, obj2,
1184 stored_pointer1, stored_pointer2)) {
1185 if (error_flag)
1186 *error_flag = true;
1187 goto finish;
1189 obj1 = next_obj1;
1190 obj2 = next_obj2;
1191 goto start_subtree;
1193 finish:
1194 while (_prop_stack_pop(&stack, &obj1, &obj2, NULL, NULL)) {
1195 po1 = obj1;
1196 (*po1->po_type->pot_equals_finish)(obj1, obj2);
1198 return (false);
1202 * prop_object_iterator_next --
1203 * Return the next item during an iteration.
1205 prop_object_t
1206 prop_object_iterator_next(prop_object_iterator_t pi)
1209 return ((*pi->pi_next_object)(pi));
1213 * prop_object_iterator_reset --
1214 * Reset the iterator to the first object so as to restart
1215 * iteration.
1217 void
1218 prop_object_iterator_reset(prop_object_iterator_t pi)
1221 (*pi->pi_reset)(pi);
1225 * prop_object_iterator_release --
1226 * Release the object iterator.
1228 void
1229 prop_object_iterator_release(prop_object_iterator_t pi)
1232 prop_object_release(pi->pi_obj);
1233 _PROP_FREE(pi, M_TEMP);