6811333 Remove prom_printf() message in emlxs driver
[opensolaris.git] / usr / src / lib / libnisdb / db_pickle.cc
blob64fda0d68f08de4abbccf427512ece9669e53c88
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * db_pickle.cc
25 * Copyright (c) 1988-2000 by Sun Microsystems, Inc.
26 * All Rights Reserved.
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 /* #include <sys/types.h> */
32 #include <stdio.h>
33 /* #include <syslog.h> */
34 #include <string.h>
35 #include <unistd.h>
36 #include "db_headers.h"
37 #include "db_pickle.h"
38 #include "nisdb_mt.h"
40 /* Constructor. Creates pickle_file with given name and mode. */
41 pickle_file::pickle_file(char* f, pickle_mode m)
43 if ((filename = strdup(f)) == NULL) {
44 FATAL("pickle_file::pickle_file: cannot allocate space",
45 DB_MEMORY_LIMIT);
48 INITRW(pickle);
50 mode = m;
54 * Opens pickle_file with mode specified with constructor.
55 * Returns TRUE if open was successful; FALSE otherwise.
57 bool_t
58 pickle_file::open()
60 WRITELOCK(this, FALSE, "w pickle_file::open");
61 if (mode == PICKLE_READ) {
62 file = fopen(filename, "r");
63 if (file)
64 xdrstdio_create(&(xdr), file, XDR_DECODE);
65 } else if (mode == PICKLE_WRITE) {
66 file = fopen(filename, "w");
67 if (file) {
68 setvbuf(file, NULL, _IOFBF, 81920);
69 xdrstdio_create(&(xdr), file, XDR_ENCODE);
71 } else if (mode == PICKLE_APPEND) {
72 file = fopen(filename, "a");
73 if (file)
74 xdrstdio_create(&(xdr), file, XDR_ENCODE);
76 if (file == NULL) {
77 WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
78 return (FALSE);
80 WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
81 return (TRUE);
85 /* Closes pickle_file. Returns 0 if successful; -1 otherwise. */
86 int
87 pickle_file::close()
89 int ret;
91 WRITELOCK(this, EOF, "w pickle_file::close");
92 xdr_destroy(&(xdr));
93 ret = fclose(file);
94 WRITEUNLOCK(this, EOF, "wu pickle_file::close");
95 return (ret);
100 * dump or load data structure to/from 'filename' using function 'f'.
101 * dump or load is determined by 'mode' with which pickle_file was created.
102 * Returns 0 if successful; 1 if file cannot be opened in mode
103 * specified; -1 if transfer failed do to encoding/decoding errors.
106 pickle_file::transfer(pptr p, bool_t (*f) (XDR*, pptr))
108 WRITELOCK(this, -1, "w pickle_file::transfer");
109 if (open()) {
110 if ((f)(&xdr, p) == FALSE) {
111 close();
112 WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
113 return (-1);
114 } else {
115 fsync(fileno(file));
116 WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
117 return (close());
120 WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
121 return (1);