2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * db_pickle.cc
2N/A *
2N/A * Copyright (c) 1988-2000 by Sun Microsystems, Inc.
2N/A * All Rights Reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/* #include <sys/types.h> */
2N/A#include <stdio.h>
2N/A/* #include <syslog.h> */
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A#include "db_headers.h"
2N/A#include "db_pickle.h"
2N/A#include "nisdb_mt.h"
2N/A
2N/A/* Constructor. Creates pickle_file with given name and mode. */
2N/Apickle_file::pickle_file(char* f, pickle_mode m)
2N/A{
2N/A if ((filename = strdup(f)) == NULL) {
2N/A FATAL("pickle_file::pickle_file: cannot allocate space",
2N/A DB_MEMORY_LIMIT);
2N/A }
2N/A
2N/A INITRW(pickle);
2N/A
2N/A mode = m;
2N/A}
2N/A
2N/A/*
2N/A * Opens pickle_file with mode specified with constructor.
2N/A * Returns TRUE if open was successful; FALSE otherwise.
2N/A */
2N/Abool_t
2N/Apickle_file::open()
2N/A{
2N/A WRITELOCK(this, FALSE, "w pickle_file::open");
2N/A if (mode == PICKLE_READ) {
2N/A file = fopen(filename, "r");
2N/A if (file)
2N/A xdrstdio_create(&(xdr), file, XDR_DECODE);
2N/A } else if (mode == PICKLE_WRITE) {
2N/A file = fopen(filename, "w");
2N/A if (file) {
2N/A setvbuf(file, NULL, _IOFBF, 81920);
2N/A xdrstdio_create(&(xdr), file, XDR_ENCODE);
2N/A }
2N/A } else if (mode == PICKLE_APPEND) {
2N/A file = fopen(filename, "a");
2N/A if (file)
2N/A xdrstdio_create(&(xdr), file, XDR_ENCODE);
2N/A }
2N/A if (file == NULL) {
2N/A WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
2N/A return (FALSE);
2N/A }
2N/A WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
2N/A return (TRUE);
2N/A}
2N/A
2N/A
2N/A/* Closes pickle_file. Returns 0 if successful; -1 otherwise. */
2N/Aint
2N/Apickle_file::close()
2N/A{
2N/A int ret;
2N/A
2N/A WRITELOCK(this, EOF, "w pickle_file::close");
2N/A xdr_destroy(&(xdr));
2N/A ret = fclose(file);
2N/A WRITEUNLOCK(this, EOF, "wu pickle_file::close");
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * dump or load data structure to/from 'filename' using function 'f'.
2N/A * dump or load is determined by 'mode' with which pickle_file was created.
2N/A * Returns 0 if successful; 1 if file cannot be opened in mode
2N/A * specified; -1 if transfer failed do to encoding/decoding errors.
2N/A*/
2N/Aint
2N/Apickle_file::transfer(pptr p, bool_t (*f) (XDR*, pptr))
2N/A{
2N/A WRITELOCK(this, -1, "w pickle_file::transfer");
2N/A if (open()) {
2N/A if ((f)(&xdr, p) == FALSE) {
2N/A close();
2N/A WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
2N/A return (-1);
2N/A } else {
2N/A fsync(fileno(file));
2N/A WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
2N/A return (close());
2N/A }
2N/A }
2N/A WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
2N/A return (1);
2N/A}