Remove duplicated headers.
[portableproplib.git] / src / prop_object.c
blobfab7f5554c7db5a019948ca8b88ca58d09b0020a
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/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.
810 bool
811 _prop_object_externalize_write_file(const char *fname, const char *xml,
812 size_t len, bool do_compress)
814 gzFile *gzf = NULL;
815 char tname[PATH_MAX], *otname;
816 int fd;
817 int save_errno;
818 mode_t myumask;
820 if (len > SSIZE_MAX) {
821 errno = EFBIG;
822 return (false);
826 * Get the directory name where the file is to be written
827 * and create the temporary file.
829 _prop_object_externalize_file_dirname(fname, tname);
831 #ifdef HAVE_STRLCAT
832 if (strlcat(tname, "/.plistXXXXXX", sizeof(tname)) >= sizeof(tname)) {
833 errno = ENAMETOOLONG;
834 return (false);
836 #else
837 otname = strncat(tname, "/.plistXXXXXX", sizeof(tname));
839 if (sizeof(*otname) >= sizeof(tname)) {
840 errno = ENAMETOOLONG;
841 return (false);
843 #endif
844 if ((fd = mkstemp(tname)) == -1)
845 return (false);
847 if (do_compress) {
848 if ((gzf = gzdopen(fd, "a")) == NULL)
849 goto bad;
851 if (gzsetparams(gzf, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY))
852 goto bad;
854 if (gzwrite(gzf, xml, len) != (ssize_t)len)
855 goto bad;
856 } else {
857 if (write(fd, xml, len) != (ssize_t)len)
858 goto bad;
861 if (fsync(fd) == -1)
862 goto bad;
864 myumask = umask(0);
865 (void)umask(myumask);
866 if (fchmod(fd, 0666 & ~myumask) == -1)
867 goto bad;
869 if (do_compress)
870 (void)gzclose(gzf);
871 else
872 (void)close(fd);
873 fd = -1;
875 if (rename(tname, fname) == -1)
876 goto bad;
878 return (true);
880 bad:
881 save_errno = errno;
882 if (do_compress && gzf != NULL)
883 (void)gzclose(gzf);
884 else if (fd != -1)
885 (void)close(fd);
886 (void) unlink(tname);
887 errno = save_errno;
888 return (false);
892 * _prop_object_internalize_map_file --
893 * Map a file for the purpose of internalizing it.
895 struct _prop_object_internalize_mapped_file *
896 _prop_object_internalize_map_file(const char *fname)
898 struct stat sb;
899 struct _prop_object_internalize_mapped_file *mf;
900 size_t pgsize = (size_t)sysconf(_SC_PAGESIZE);
901 size_t pgmask = pgsize - 1;
902 bool need_guard = false;
903 int fd;
905 mf = _PROP_MALLOC(sizeof(*mf), M_TEMP);
906 if (mf == NULL)
907 return (NULL);
909 fd = open(fname, O_RDONLY, 0400);
910 if (fd == -1) {
911 _PROP_FREE(mf, M_TEMP);
912 return (NULL);
915 if (fstat(fd, &sb) == -1) {
916 (void) close(fd);
917 _PROP_FREE(mf, M_TEMP);
918 return (NULL);
920 mf->poimf_mapsize = ((size_t)sb.st_size + pgmask) & ~pgmask;
921 if (mf->poimf_mapsize < (size_t)sb.st_size) {
922 (void) close(fd);
923 _PROP_FREE(mf, M_TEMP);
924 return (NULL);
928 * If the file length is an integral number of pages, then we
929 * need to map a guard page at the end in order to provide the
930 * necessary NUL-termination of the buffer.
932 if ((sb.st_size & pgmask) == 0)
933 need_guard = true;
935 mf->poimf_xml = mmap(NULL, need_guard ? mf->poimf_mapsize + pgsize
936 : mf->poimf_mapsize,
937 PROT_READ, MAP_FILE|MAP_SHARED, fd, (off_t)0);
938 (void) close(fd);
939 if (mf->poimf_xml == MAP_FAILED) {
940 _PROP_FREE(mf, M_TEMP);
941 return (NULL);
943 (void) madvise(mf->poimf_xml, mf->poimf_mapsize, MADV_SEQUENTIAL);
945 if (need_guard) {
946 if (mmap(mf->poimf_xml + mf->poimf_mapsize,
947 pgsize, PROT_READ,
948 MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1,
949 (off_t)0) == MAP_FAILED) {
950 (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
951 _PROP_FREE(mf, M_TEMP);
952 return (NULL);
954 mf->poimf_mapsize += pgsize;
957 return (mf);
961 * _prop_object_internalize_unmap_file --
962 * Unmap a file previously mapped for internalizing.
964 void
965 _prop_object_internalize_unmap_file(
966 struct _prop_object_internalize_mapped_file *mf)
969 (void) madvise(mf->poimf_xml, mf->poimf_mapsize, MADV_DONTNEED);
970 (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
971 _PROP_FREE(mf, M_TEMP);
975 * Retain / release serialization --
977 * Eventually we would like to use atomic operations. But until we have
978 * an MI API for them that is common to userland and the kernel, we will
979 * use a lock instead.
981 * We use a single global mutex for all serialization. In the kernel, because
982 * we are still under a biglock, this will basically never contend (properties
983 * cannot be manipulated at interrupt level). In userland, this will cost
984 * nothing for single-threaded programs. For multi-threaded programs, there
985 * could be contention, but it probably won't cost that much unless the program
986 * makes heavy use of property lists.
988 _PROP_MUTEX_DECL_STATIC(_prop_refcnt_mutex)
989 #define _PROP_REFCNT_LOCK() _PROP_MUTEX_LOCK(_prop_refcnt_mutex)
990 #define _PROP_REFCNT_UNLOCK() _PROP_MUTEX_UNLOCK(_prop_refcnt_mutex)
993 * prop_object_retain --
994 * Increment the reference count on an object.
996 void
997 prop_object_retain(prop_object_t obj)
999 struct _prop_object *po = obj;
1000 uint32_t ocnt;
1002 _PROP_REFCNT_LOCK();
1003 ocnt = po->po_refcnt++;
1004 _PROP_REFCNT_UNLOCK();
1006 _PROP_ASSERT(ocnt != 0xffffffffU);
1010 * prop_object_release_emergency
1011 * A direct free with prop_object_release failed.
1012 * Walk down the tree until a leaf is found and
1013 * free that. Do not recurse to avoid stack overflows.
1015 * This is a slow edge condition, but necessary to
1016 * guarantee that an object can always be freed.
1018 static void
1019 prop_object_release_emergency(prop_object_t obj)
1021 struct _prop_object *po;
1022 void (*unlock)(void);
1023 prop_object_t parent = NULL;
1024 uint32_t ocnt;
1026 for (;;) {
1027 po = obj;
1028 _PROP_ASSERT(obj);
1030 if (po->po_type->pot_lock != NULL)
1031 po->po_type->pot_lock();
1033 /* Save pointerto unlock function */
1034 unlock = po->po_type->pot_unlock;
1036 _PROP_REFCNT_LOCK();
1037 ocnt = po->po_refcnt--;
1038 _PROP_REFCNT_UNLOCK();
1040 _PROP_ASSERT(ocnt != 0);
1041 if (ocnt != 1) {
1042 if (unlock != NULL)
1043 unlock();
1044 break;
1047 _PROP_ASSERT(po->po_type);
1048 if ((po->po_type->pot_free)(NULL, &obj) ==
1049 _PROP_OBJECT_FREE_DONE) {
1050 if (unlock != NULL)
1051 unlock();
1052 break;
1055 if (unlock != NULL)
1056 unlock();
1058 parent = po;
1059 _PROP_REFCNT_LOCK();
1060 ++po->po_refcnt;
1061 _PROP_REFCNT_UNLOCK();
1063 _PROP_ASSERT(parent);
1064 /* One object was just freed. */
1065 po = parent;
1066 (*po->po_type->pot_emergency_free)(parent);
1070 * prop_object_release --
1071 * Decrement the reference count on an object.
1073 * Free the object if we are releasing the final
1074 * reference.
1076 void
1077 prop_object_release(prop_object_t obj)
1079 struct _prop_object *po;
1080 struct _prop_stack stack;
1081 void (*unlock)(void);
1082 int ret;
1083 uint32_t ocnt;
1085 _prop_stack_init(&stack);
1087 do {
1088 do {
1089 po = obj;
1090 _PROP_ASSERT(obj);
1092 if (po->po_type->pot_lock != NULL)
1093 po->po_type->pot_lock();
1095 /* Save pointer to object unlock function */
1096 unlock = po->po_type->pot_unlock;
1098 _PROP_REFCNT_LOCK();
1099 ocnt = po->po_refcnt--;
1100 _PROP_REFCNT_UNLOCK();
1102 _PROP_ASSERT(ocnt != 0);
1103 if (ocnt != 1) {
1104 ret = 0;
1105 if (unlock != NULL)
1106 unlock();
1107 break;
1110 ret = (po->po_type->pot_free)(&stack, &obj);
1112 if (unlock != NULL)
1113 unlock();
1115 if (ret == _PROP_OBJECT_FREE_DONE)
1116 break;
1118 _PROP_REFCNT_LOCK();
1119 ++po->po_refcnt;
1120 _PROP_REFCNT_UNLOCK();
1121 } while (ret == _PROP_OBJECT_FREE_RECURSE);
1122 if (ret == _PROP_OBJECT_FREE_FAILED)
1123 prop_object_release_emergency(obj);
1124 } while (_prop_stack_pop(&stack, &obj, NULL, NULL, NULL));
1128 * prop_object_type --
1129 * Return the type of an object.
1131 prop_type_t
1132 prop_object_type(prop_object_t obj)
1134 struct _prop_object *po = obj;
1136 if (obj == NULL)
1137 return (PROP_TYPE_UNKNOWN);
1139 return (po->po_type->pot_type);
1143 * prop_object_equals --
1144 * Returns true if thw two objects are equivalent.
1146 bool
1147 prop_object_equals(prop_object_t obj1, prop_object_t obj2)
1149 return (prop_object_equals_with_error(obj1, obj2, NULL));
1152 bool
1153 prop_object_equals_with_error(prop_object_t obj1, prop_object_t obj2,
1154 bool *error_flag)
1156 struct _prop_object *po1;
1157 struct _prop_object *po2;
1158 void *stored_pointer1, *stored_pointer2;
1159 prop_object_t next_obj1, next_obj2;
1160 struct _prop_stack stack;
1161 _prop_object_equals_rv_t ret;
1163 _prop_stack_init(&stack);
1164 if (error_flag)
1165 *error_flag = false;
1167 start_subtree:
1168 stored_pointer1 = NULL;
1169 stored_pointer2 = NULL;
1170 po1 = obj1;
1171 po2 = obj2;
1173 if (po1->po_type != po2->po_type)
1174 return (false);
1176 continue_subtree:
1177 ret = (*po1->po_type->pot_equals)(obj1, obj2,
1178 &stored_pointer1, &stored_pointer2,
1179 &next_obj1, &next_obj2);
1180 if (ret == _PROP_OBJECT_EQUALS_FALSE)
1181 goto finish;
1182 if (ret == _PROP_OBJECT_EQUALS_TRUE) {
1183 if (!_prop_stack_pop(&stack, &obj1, &obj2,
1184 &stored_pointer1, &stored_pointer2))
1185 return true;
1186 goto continue_subtree;
1188 _PROP_ASSERT(ret == _PROP_OBJECT_EQUALS_RECURSE);
1190 if (!_prop_stack_push(&stack, obj1, obj2,
1191 stored_pointer1, stored_pointer2)) {
1192 if (error_flag)
1193 *error_flag = true;
1194 goto finish;
1196 obj1 = next_obj1;
1197 obj2 = next_obj2;
1198 goto start_subtree;
1200 finish:
1201 while (_prop_stack_pop(&stack, &obj1, &obj2, NULL, NULL)) {
1202 po1 = obj1;
1203 (*po1->po_type->pot_equals_finish)(obj1, obj2);
1205 return (false);
1209 * prop_object_iterator_next --
1210 * Return the next item during an iteration.
1212 prop_object_t
1213 prop_object_iterator_next(prop_object_iterator_t pi)
1216 return ((*pi->pi_next_object)(pi));
1220 * prop_object_iterator_reset --
1221 * Reset the iterator to the first object so as to restart
1222 * iteration.
1224 void
1225 prop_object_iterator_reset(prop_object_iterator_t pi)
1228 (*pi->pi_reset)(pi);
1232 * prop_object_iterator_release --
1233 * Release the object iterator.
1235 void
1236 prop_object_iterator_release(prop_object_iterator_t pi)
1239 prop_object_release(pi->pi_obj);
1240 _PROP_FREE(pi, M_TEMP);