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