xref: /illumos-gate/usr/src/cmd/sendmail/src/bf.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
2*3ee0e492Sjbeck  * Copyright (c) 1999-2002, 2004, 2006 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Contributed by Exactis.com, Inc.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  */
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate /*
147c478bd9Sstevel@tonic-gate **  This is in transition. Changed from the original bf_torek.c code
157c478bd9Sstevel@tonic-gate **  to use sm_io function calls directly rather than through stdio
167c478bd9Sstevel@tonic-gate **  translation layer. Will be made a built-in file type of libsm
177c478bd9Sstevel@tonic-gate **  next (once safeopen() linkable from libsm).
187c478bd9Sstevel@tonic-gate */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <sm/gen.h>
21*3ee0e492Sjbeck SM_RCSID("@(#)$Id: bf.c,v 8.62 2006/03/31 18:45:56 ca Exp $")
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include <sys/types.h>
247c478bd9Sstevel@tonic-gate #include <sys/stat.h>
257c478bd9Sstevel@tonic-gate #include <sys/uio.h>
267c478bd9Sstevel@tonic-gate #include <fcntl.h>
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include "sendmail.h"
327c478bd9Sstevel@tonic-gate #include "bf.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* bf io functions */
377c478bd9Sstevel@tonic-gate static ssize_t	sm_bfread __P((SM_FILE_T *, char *, size_t));
387c478bd9Sstevel@tonic-gate static ssize_t	sm_bfwrite __P((SM_FILE_T *, const char *, size_t));
397c478bd9Sstevel@tonic-gate static off_t	sm_bfseek __P((SM_FILE_T *, off_t, int));
407c478bd9Sstevel@tonic-gate static int	sm_bfclose __P((SM_FILE_T *));
417c478bd9Sstevel@tonic-gate static int	sm_bfcommit __P((SM_FILE_T *));
427c478bd9Sstevel@tonic-gate static int	sm_bftruncate __P((SM_FILE_T *));
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static int	sm_bfopen __P((SM_FILE_T *, const void *, int, const void *));
457c478bd9Sstevel@tonic-gate static int	sm_bfsetinfo __P((SM_FILE_T *, int , void *));
467c478bd9Sstevel@tonic-gate static int	sm_bfgetinfo __P((SM_FILE_T *, int , void *));
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate **  Data structure for storing information about each buffered file
507c478bd9Sstevel@tonic-gate **  (Originally in sendmail/bf_torek.h for the curious.)
517c478bd9Sstevel@tonic-gate */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate struct bf
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	bool	bf_committed;	/* Has this buffered file been committed? */
567c478bd9Sstevel@tonic-gate 	bool	bf_ondisk;	/* On disk: committed or buffer overflow */
577c478bd9Sstevel@tonic-gate 	long	bf_flags;
587c478bd9Sstevel@tonic-gate 	int	bf_disk_fd;	/* If on disk, associated file descriptor */
597c478bd9Sstevel@tonic-gate 	char	*bf_buf;	/* Memory buffer */
607c478bd9Sstevel@tonic-gate 	int	bf_bufsize;	/* Length of above buffer */
617c478bd9Sstevel@tonic-gate 	int	bf_buffilled;	/* Bytes of buffer actually filled */
627c478bd9Sstevel@tonic-gate 	char	*bf_filename;	/* Name of buffered file, if ever committed */
637c478bd9Sstevel@tonic-gate 	MODE_T	bf_filemode;	/* Mode of buffered file, if ever committed */
647c478bd9Sstevel@tonic-gate 	off_t	bf_offset;	/* Currect file offset */
657c478bd9Sstevel@tonic-gate 	int	bf_size;	/* Total current size of file */
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifdef BF_STANDALONE
697c478bd9Sstevel@tonic-gate # define OPEN(fn, omode, cmode, sff) open(fn, omode, cmode)
707c478bd9Sstevel@tonic-gate #else /* BF_STANDALONE */
717c478bd9Sstevel@tonic-gate # define OPEN(fn, omode, cmode, sff) safeopen(fn, omode, cmode, sff)
727c478bd9Sstevel@tonic-gate #endif /* BF_STANDALONE */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate struct bf_info
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	char	*bi_filename;
777c478bd9Sstevel@tonic-gate 	MODE_T	bi_fmode;
787c478bd9Sstevel@tonic-gate 	size_t	bi_bsize;
797c478bd9Sstevel@tonic-gate 	long	bi_flags;
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate **  SM_BFOPEN -- the "base" open function called by sm_io_open() for the
847c478bd9Sstevel@tonic-gate **		internal, file-type-specific info setup.
857c478bd9Sstevel@tonic-gate **
867c478bd9Sstevel@tonic-gate **	Parameters:
877c478bd9Sstevel@tonic-gate **		fp -- file pointer being filled-in for file being open'd
887c478bd9Sstevel@tonic-gate **		info -- information about file being opened
897c478bd9Sstevel@tonic-gate **		flags -- ignored
907c478bd9Sstevel@tonic-gate **		rpool -- ignored (currently)
917c478bd9Sstevel@tonic-gate **
927c478bd9Sstevel@tonic-gate **	Returns:
937c478bd9Sstevel@tonic-gate **		Failure: -1 and sets errno
947c478bd9Sstevel@tonic-gate **		Success: 0 (zero)
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static int
sm_bfopen(fp,info,flags,rpool)987c478bd9Sstevel@tonic-gate sm_bfopen(fp, info, flags, rpool)
997c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
1007c478bd9Sstevel@tonic-gate 	const void *info;
1017c478bd9Sstevel@tonic-gate 	int flags;
1027c478bd9Sstevel@tonic-gate 	const void *rpool;
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	char *filename;
1057c478bd9Sstevel@tonic-gate 	MODE_T fmode;
1067c478bd9Sstevel@tonic-gate 	size_t bsize;
1077c478bd9Sstevel@tonic-gate 	long sflags;
1087c478bd9Sstevel@tonic-gate 	struct bf *bfp;
1097c478bd9Sstevel@tonic-gate 	int l;
1107c478bd9Sstevel@tonic-gate 	struct stat st;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	filename = ((struct bf_info *) info)->bi_filename;
1137c478bd9Sstevel@tonic-gate 	fmode = ((struct bf_info *) info)->bi_fmode;
1147c478bd9Sstevel@tonic-gate 	bsize = ((struct bf_info *) info)->bi_bsize;
1157c478bd9Sstevel@tonic-gate 	sflags = ((struct bf_info *) info)->bi_flags;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* Sanity checks */
1187c478bd9Sstevel@tonic-gate 	if (*filename == '\0')
1197c478bd9Sstevel@tonic-gate 	{
1207c478bd9Sstevel@tonic-gate 		/* Empty filename string */
1217c478bd9Sstevel@tonic-gate 		errno = ENOENT;
1227c478bd9Sstevel@tonic-gate 		return -1;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	if (stat(filename, &st) == 0)
1257c478bd9Sstevel@tonic-gate 	{
1267c478bd9Sstevel@tonic-gate 		/* File already exists on disk */
1277c478bd9Sstevel@tonic-gate 		errno = EEXIST;
1287c478bd9Sstevel@tonic-gate 		return -1;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* Allocate memory */
1327c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) sm_malloc(sizeof(struct bf));
1337c478bd9Sstevel@tonic-gate 	if (bfp == NULL)
1347c478bd9Sstevel@tonic-gate 	{
1357c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1367c478bd9Sstevel@tonic-gate 		return -1;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* Assign data buffer */
1407c478bd9Sstevel@tonic-gate 	/* A zero bsize is valid, just don't allocate memory */
1417c478bd9Sstevel@tonic-gate 	if (bsize > 0)
1427c478bd9Sstevel@tonic-gate 	{
1437c478bd9Sstevel@tonic-gate 		bfp->bf_buf = (char *) sm_malloc(bsize);
1447c478bd9Sstevel@tonic-gate 		if (bfp->bf_buf == NULL)
1457c478bd9Sstevel@tonic-gate 		{
1467c478bd9Sstevel@tonic-gate 			bfp->bf_bufsize = 0;
1477c478bd9Sstevel@tonic-gate 			sm_free(bfp);
1487c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
1497c478bd9Sstevel@tonic-gate 			return -1;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	else
1537c478bd9Sstevel@tonic-gate 		bfp->bf_buf = NULL;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* Nearly home free, just set all the parameters now */
1567c478bd9Sstevel@tonic-gate 	bfp->bf_committed = false;
1577c478bd9Sstevel@tonic-gate 	bfp->bf_ondisk = false;
1587c478bd9Sstevel@tonic-gate 	bfp->bf_flags = sflags;
1597c478bd9Sstevel@tonic-gate 	bfp->bf_bufsize = bsize;
1607c478bd9Sstevel@tonic-gate 	bfp->bf_buffilled = 0;
1617c478bd9Sstevel@tonic-gate 	l = strlen(filename) + 1;
1627c478bd9Sstevel@tonic-gate 	bfp->bf_filename = (char *) sm_malloc(l);
1637c478bd9Sstevel@tonic-gate 	if (bfp->bf_filename == NULL)
1647c478bd9Sstevel@tonic-gate 	{
1657c478bd9Sstevel@tonic-gate 		if (bfp->bf_buf != NULL)
1667c478bd9Sstevel@tonic-gate 			sm_free(bfp->bf_buf);
1677c478bd9Sstevel@tonic-gate 		sm_free(bfp);
1687c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1697c478bd9Sstevel@tonic-gate 		return -1;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(bfp->bf_filename, filename, l);
1727c478bd9Sstevel@tonic-gate 	bfp->bf_filemode = fmode;
1737c478bd9Sstevel@tonic-gate 	bfp->bf_offset = 0;
1747c478bd9Sstevel@tonic-gate 	bfp->bf_size = 0;
1757c478bd9Sstevel@tonic-gate 	bfp->bf_disk_fd = -1;
1767c478bd9Sstevel@tonic-gate 	fp->f_cookie = bfp;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (tTd(58, 8))
1797c478bd9Sstevel@tonic-gate 		sm_dprintf("sm_bfopen(%s)\n", filename);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	return 0;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate **  BFOPEN -- create a new buffered file
1867c478bd9Sstevel@tonic-gate **
1877c478bd9Sstevel@tonic-gate **	Parameters:
1887c478bd9Sstevel@tonic-gate **		filename -- the file's name
1897c478bd9Sstevel@tonic-gate **		fmode -- what mode the file should be created as
1907c478bd9Sstevel@tonic-gate **		bsize -- amount of buffer space to allocate (may be 0)
1917c478bd9Sstevel@tonic-gate **		flags -- if running under sendmail, passed directly to safeopen
1927c478bd9Sstevel@tonic-gate **
1937c478bd9Sstevel@tonic-gate **	Returns:
1947c478bd9Sstevel@tonic-gate **		a SM_FILE_T * which may then be used with stdio functions,
1957c478bd9Sstevel@tonic-gate **		or NULL	on failure. SM_FILE_T * is opened for writing
1967c478bd9Sstevel@tonic-gate **		"SM_IO_WHAT_VECTORS").
1977c478bd9Sstevel@tonic-gate **
1987c478bd9Sstevel@tonic-gate **	Side Effects:
1997c478bd9Sstevel@tonic-gate **		none.
2007c478bd9Sstevel@tonic-gate **
2017c478bd9Sstevel@tonic-gate **	Sets errno:
2027c478bd9Sstevel@tonic-gate **		any value of errno specified by sm_io_setinfo_type()
2037c478bd9Sstevel@tonic-gate **		any value of errno specified by sm_io_open()
2047c478bd9Sstevel@tonic-gate **		any value of errno specified by sm_io_setinfo()
2057c478bd9Sstevel@tonic-gate */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #ifdef __STDC__
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate **  XXX This is a temporary hack since MODE_T on HP-UX 10.x is short.
2107c478bd9Sstevel@tonic-gate **	If we use K&R here, the compiler will complain about
2117c478bd9Sstevel@tonic-gate **	Inconsistent parameter list declaration
2127c478bd9Sstevel@tonic-gate **	due to the change from short to int.
2137c478bd9Sstevel@tonic-gate */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate SM_FILE_T *
bfopen(char * filename,MODE_T fmode,size_t bsize,long flags)2167c478bd9Sstevel@tonic-gate bfopen(char *filename, MODE_T fmode, size_t bsize, long flags)
2177c478bd9Sstevel@tonic-gate #else /* __STDC__ */
2187c478bd9Sstevel@tonic-gate SM_FILE_T *
2197c478bd9Sstevel@tonic-gate bfopen(filename, fmode, bsize, flags)
2207c478bd9Sstevel@tonic-gate 	char *filename;
2217c478bd9Sstevel@tonic-gate 	MODE_T fmode;
2227c478bd9Sstevel@tonic-gate 	size_t bsize;
2237c478bd9Sstevel@tonic-gate 	long flags;
2247c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	MODE_T omask;
2277c478bd9Sstevel@tonic-gate 	SM_FILE_T SM_IO_SET_TYPE(vector, BF_FILE_TYPE, sm_bfopen, sm_bfclose,
2287c478bd9Sstevel@tonic-gate 		sm_bfread, sm_bfwrite, sm_bfseek, sm_bfgetinfo, sm_bfsetinfo,
2297c478bd9Sstevel@tonic-gate 		SM_TIME_FOREVER);
2307c478bd9Sstevel@tonic-gate 	struct bf_info info;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	**  Apply current umask to fmode as it may change by the time
2347c478bd9Sstevel@tonic-gate 	**  the file is actually created.  fmode becomes the true
2357c478bd9Sstevel@tonic-gate 	**  permissions of the file, which OPEN() must obey.
2367c478bd9Sstevel@tonic-gate 	*/
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	omask = umask(0);
2397c478bd9Sstevel@tonic-gate 	fmode &= ~omask;
2407c478bd9Sstevel@tonic-gate 	(void) umask(omask);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	SM_IO_INIT_TYPE(vector, BF_FILE_TYPE, sm_bfopen, sm_bfclose,
2437c478bd9Sstevel@tonic-gate 		sm_bfread, sm_bfwrite, sm_bfseek, sm_bfgetinfo, sm_bfsetinfo,
2447c478bd9Sstevel@tonic-gate 		SM_TIME_FOREVER);
2457c478bd9Sstevel@tonic-gate 	info.bi_filename = filename;
2467c478bd9Sstevel@tonic-gate 	info.bi_fmode = fmode;
2477c478bd9Sstevel@tonic-gate 	info.bi_bsize = bsize;
2487c478bd9Sstevel@tonic-gate 	info.bi_flags = flags;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	return sm_io_open(&vector, SM_TIME_DEFAULT, &info, SM_IO_RDWR, NULL);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate **  SM_BFGETINFO -- returns info about an open file pointer
2557c478bd9Sstevel@tonic-gate **
2567c478bd9Sstevel@tonic-gate **	Parameters:
2577c478bd9Sstevel@tonic-gate **		fp -- file pointer to get info about
2587c478bd9Sstevel@tonic-gate **		what -- type of info to obtain
2597c478bd9Sstevel@tonic-gate **		valp -- thing to return the info in
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static int
sm_bfgetinfo(fp,what,valp)2637c478bd9Sstevel@tonic-gate sm_bfgetinfo(fp, what, valp)
2647c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
2657c478bd9Sstevel@tonic-gate 	int what;
2667c478bd9Sstevel@tonic-gate 	void *valp;
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	struct bf *bfp;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
2717c478bd9Sstevel@tonic-gate 	switch (what)
2727c478bd9Sstevel@tonic-gate 	{
2737c478bd9Sstevel@tonic-gate 	  case SM_IO_WHAT_FD:
2747c478bd9Sstevel@tonic-gate 		return bfp->bf_disk_fd;
2757c478bd9Sstevel@tonic-gate 	  case SM_IO_WHAT_SIZE:
2767c478bd9Sstevel@tonic-gate 		return bfp->bf_size;
2777c478bd9Sstevel@tonic-gate 	  default:
2787c478bd9Sstevel@tonic-gate 		return -1;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate **  SM_BFCLOSE -- close a buffered file
2847c478bd9Sstevel@tonic-gate **
2857c478bd9Sstevel@tonic-gate **	Parameters:
2867c478bd9Sstevel@tonic-gate **		fp -- cookie of file to close
2877c478bd9Sstevel@tonic-gate **
2887c478bd9Sstevel@tonic-gate **	Returns:
2897c478bd9Sstevel@tonic-gate **		0 to indicate success
2907c478bd9Sstevel@tonic-gate **
2917c478bd9Sstevel@tonic-gate **	Side Effects:
2927c478bd9Sstevel@tonic-gate **		deletes backing file, sm_frees memory.
2937c478bd9Sstevel@tonic-gate **
2947c478bd9Sstevel@tonic-gate **	Sets errno:
2957c478bd9Sstevel@tonic-gate **		never.
2967c478bd9Sstevel@tonic-gate */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate static int
sm_bfclose(fp)2997c478bd9Sstevel@tonic-gate sm_bfclose(fp)
3007c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	struct bf *bfp;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* Cast cookie back to correct type */
3057c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/* Need to clean up the file */
3087c478bd9Sstevel@tonic-gate 	if (bfp->bf_ondisk && !bfp->bf_committed)
3097c478bd9Sstevel@tonic-gate 		unlink(bfp->bf_filename);
3107c478bd9Sstevel@tonic-gate 	sm_free(bfp->bf_filename);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (bfp->bf_disk_fd != -1)
3137c478bd9Sstevel@tonic-gate 		close(bfp->bf_disk_fd);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/* Need to sm_free the buffer */
3167c478bd9Sstevel@tonic-gate 	if (bfp->bf_bufsize > 0)
3177c478bd9Sstevel@tonic-gate 		sm_free(bfp->bf_buf);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* Finally, sm_free the structure */
3207c478bd9Sstevel@tonic-gate 	sm_free(bfp);
3217c478bd9Sstevel@tonic-gate 	return 0;
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate **  SM_BFREAD -- read a buffered file
3267c478bd9Sstevel@tonic-gate **
3277c478bd9Sstevel@tonic-gate **	Parameters:
3287c478bd9Sstevel@tonic-gate **		cookie -- cookie of file to read
3297c478bd9Sstevel@tonic-gate **		buf -- buffer to fill
3307c478bd9Sstevel@tonic-gate **		nbytes -- how many bytes to read
3317c478bd9Sstevel@tonic-gate **
3327c478bd9Sstevel@tonic-gate **	Returns:
3337c478bd9Sstevel@tonic-gate **		number of bytes read or -1 indicate failure
3347c478bd9Sstevel@tonic-gate **
3357c478bd9Sstevel@tonic-gate **	Side Effects:
3367c478bd9Sstevel@tonic-gate **		none.
3377c478bd9Sstevel@tonic-gate **
3387c478bd9Sstevel@tonic-gate */
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate static ssize_t
sm_bfread(fp,buf,nbytes)3417c478bd9Sstevel@tonic-gate sm_bfread(fp, buf, nbytes)
3427c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
3437c478bd9Sstevel@tonic-gate 	char *buf;
3447c478bd9Sstevel@tonic-gate 	size_t nbytes;
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	struct bf *bfp;
3477c478bd9Sstevel@tonic-gate 	ssize_t count = 0;	/* Number of bytes put in buf so far */
3487c478bd9Sstevel@tonic-gate 	int retval;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/* Cast cookie back to correct type */
3517c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (bfp->bf_offset < bfp->bf_buffilled)
3547c478bd9Sstevel@tonic-gate 	{
3557c478bd9Sstevel@tonic-gate 		/* Need to grab some from buffer */
3567c478bd9Sstevel@tonic-gate 		count = nbytes;
3577c478bd9Sstevel@tonic-gate 		if ((bfp->bf_offset + count) > bfp->bf_buffilled)
3587c478bd9Sstevel@tonic-gate 			count = bfp->bf_buffilled - bfp->bf_offset;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		memcpy(buf, bfp->bf_buf + bfp->bf_offset, count);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if ((bfp->bf_offset + nbytes) > bfp->bf_buffilled)
3647c478bd9Sstevel@tonic-gate 	{
3657c478bd9Sstevel@tonic-gate 		/* Need to grab some from file */
3667c478bd9Sstevel@tonic-gate 		if (!bfp->bf_ondisk)
3677c478bd9Sstevel@tonic-gate 		{
3687c478bd9Sstevel@tonic-gate 			/* Oops, the file doesn't exist. EOF. */
3697c478bd9Sstevel@tonic-gate 			if (tTd(58, 8))
3707c478bd9Sstevel@tonic-gate 				sm_dprintf("sm_bfread(%s): to disk\n",
3717c478bd9Sstevel@tonic-gate 					   bfp->bf_filename);
3727c478bd9Sstevel@tonic-gate 			goto finished;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		/* Catch a read() on an earlier failed write to disk */
3767c478bd9Sstevel@tonic-gate 		if (bfp->bf_disk_fd < 0)
3777c478bd9Sstevel@tonic-gate 		{
3787c478bd9Sstevel@tonic-gate 			errno = EIO;
3797c478bd9Sstevel@tonic-gate 			return -1;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		if (lseek(bfp->bf_disk_fd,
3837c478bd9Sstevel@tonic-gate 			  bfp->bf_offset + count, SEEK_SET) < 0)
3847c478bd9Sstevel@tonic-gate 		{
3857c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) || (errno == ESPIPE))
3867c478bd9Sstevel@tonic-gate 			{
3877c478bd9Sstevel@tonic-gate 				/*
3887c478bd9Sstevel@tonic-gate 				**  stdio won't be expecting these
3897c478bd9Sstevel@tonic-gate 				**  errnos from read()! Change them
3907c478bd9Sstevel@tonic-gate 				**  into something it can understand.
3917c478bd9Sstevel@tonic-gate 				*/
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 				errno = EIO;
3947c478bd9Sstevel@tonic-gate 			}
3957c478bd9Sstevel@tonic-gate 			return -1;
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		while (count < nbytes)
3997c478bd9Sstevel@tonic-gate 		{
4007c478bd9Sstevel@tonic-gate 			retval = read(bfp->bf_disk_fd,
4017c478bd9Sstevel@tonic-gate 				      buf + count,
4027c478bd9Sstevel@tonic-gate 				      nbytes - count);
4037c478bd9Sstevel@tonic-gate 			if (retval < 0)
4047c478bd9Sstevel@tonic-gate 			{
4057c478bd9Sstevel@tonic-gate 				/* errno is set implicitly by read() */
4067c478bd9Sstevel@tonic-gate 				return -1;
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 			else if (retval == 0)
4097c478bd9Sstevel@tonic-gate 				goto finished;
4107c478bd9Sstevel@tonic-gate 			else
4117c478bd9Sstevel@tonic-gate 				count += retval;
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate finished:
4167c478bd9Sstevel@tonic-gate 	bfp->bf_offset += count;
4177c478bd9Sstevel@tonic-gate 	return count;
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate **  SM_BFSEEK -- seek to a position in a buffered file
4227c478bd9Sstevel@tonic-gate **
4237c478bd9Sstevel@tonic-gate **	Parameters:
4247c478bd9Sstevel@tonic-gate **		fp     -- fp of file to seek
4257c478bd9Sstevel@tonic-gate **		offset -- position to seek to
4267c478bd9Sstevel@tonic-gate **		whence -- how to seek
4277c478bd9Sstevel@tonic-gate **
4287c478bd9Sstevel@tonic-gate **	Returns:
4297c478bd9Sstevel@tonic-gate **		new file offset or -1 indicate failure
4307c478bd9Sstevel@tonic-gate **
4317c478bd9Sstevel@tonic-gate **	Side Effects:
4327c478bd9Sstevel@tonic-gate **		none.
4337c478bd9Sstevel@tonic-gate **
4347c478bd9Sstevel@tonic-gate */
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate static off_t
sm_bfseek(fp,offset,whence)4377c478bd9Sstevel@tonic-gate sm_bfseek(fp, offset, whence)
4387c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
4397c478bd9Sstevel@tonic-gate 	off_t offset;
4407c478bd9Sstevel@tonic-gate 	int whence;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	struct bf *bfp;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/* Cast cookie back to correct type */
4467c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	switch (whence)
4497c478bd9Sstevel@tonic-gate 	{
4507c478bd9Sstevel@tonic-gate 	  case SEEK_SET:
4517c478bd9Sstevel@tonic-gate 		bfp->bf_offset = offset;
4527c478bd9Sstevel@tonic-gate 		break;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	  case SEEK_CUR:
4557c478bd9Sstevel@tonic-gate 		bfp->bf_offset += offset;
4567c478bd9Sstevel@tonic-gate 		break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	  case SEEK_END:
4597c478bd9Sstevel@tonic-gate 		bfp->bf_offset = bfp->bf_size + offset;
4607c478bd9Sstevel@tonic-gate 		break;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	  default:
4637c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4647c478bd9Sstevel@tonic-gate 		return -1;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	return bfp->bf_offset;
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate **  SM_BFWRITE -- write to a buffered file
4717c478bd9Sstevel@tonic-gate **
4727c478bd9Sstevel@tonic-gate **	Parameters:
4737c478bd9Sstevel@tonic-gate **		fp -- fp of file to write
4747c478bd9Sstevel@tonic-gate **		buf -- data buffer
4757c478bd9Sstevel@tonic-gate **		nbytes -- how many bytes to write
4767c478bd9Sstevel@tonic-gate **
4777c478bd9Sstevel@tonic-gate **	Returns:
4787c478bd9Sstevel@tonic-gate **		number of bytes written or -1 indicate failure
4797c478bd9Sstevel@tonic-gate **
4807c478bd9Sstevel@tonic-gate **	Side Effects:
4817c478bd9Sstevel@tonic-gate **		may create backing file if over memory limit for file.
4827c478bd9Sstevel@tonic-gate **
4837c478bd9Sstevel@tonic-gate */
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static ssize_t
sm_bfwrite(fp,buf,nbytes)4867c478bd9Sstevel@tonic-gate sm_bfwrite(fp, buf, nbytes)
4877c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
4887c478bd9Sstevel@tonic-gate 	const char *buf;
4897c478bd9Sstevel@tonic-gate 	size_t nbytes;
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	struct bf *bfp;
4927c478bd9Sstevel@tonic-gate 	ssize_t count = 0;	/* Number of bytes written so far */
4937c478bd9Sstevel@tonic-gate 	int retval;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/* Cast cookie back to correct type */
4967c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/* If committed, go straight to disk */
4997c478bd9Sstevel@tonic-gate 	if (bfp->bf_committed)
5007c478bd9Sstevel@tonic-gate 	{
5017c478bd9Sstevel@tonic-gate 		if (lseek(bfp->bf_disk_fd, bfp->bf_offset, SEEK_SET) < 0)
5027c478bd9Sstevel@tonic-gate 		{
5037c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) || (errno == ESPIPE))
5047c478bd9Sstevel@tonic-gate 			{
5057c478bd9Sstevel@tonic-gate 				/*
5067c478bd9Sstevel@tonic-gate 				**  stdio won't be expecting these
5077c478bd9Sstevel@tonic-gate 				**  errnos from write()! Change them
5087c478bd9Sstevel@tonic-gate 				**  into something it can understand.
5097c478bd9Sstevel@tonic-gate 				*/
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 				errno = EIO;
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 			return -1;
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		count = write(bfp->bf_disk_fd, buf, nbytes);
5177c478bd9Sstevel@tonic-gate 		if (count < 0)
5187c478bd9Sstevel@tonic-gate 		{
5197c478bd9Sstevel@tonic-gate 			/* errno is set implicitly by write() */
5207c478bd9Sstevel@tonic-gate 			return -1;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		goto finished;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (bfp->bf_offset < bfp->bf_bufsize)
5267c478bd9Sstevel@tonic-gate 	{
5277c478bd9Sstevel@tonic-gate 		/* Need to put some in buffer */
5287c478bd9Sstevel@tonic-gate 		count = nbytes;
5297c478bd9Sstevel@tonic-gate 		if ((bfp->bf_offset + count) > bfp->bf_bufsize)
5307c478bd9Sstevel@tonic-gate 			count = bfp->bf_bufsize - bfp->bf_offset;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		memcpy(bfp->bf_buf + bfp->bf_offset, buf, count);
5337c478bd9Sstevel@tonic-gate 		if ((bfp->bf_offset + count) > bfp->bf_buffilled)
5347c478bd9Sstevel@tonic-gate 			bfp->bf_buffilled = bfp->bf_offset + count;
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if ((bfp->bf_offset + nbytes) > bfp->bf_bufsize)
5387c478bd9Sstevel@tonic-gate 	{
5397c478bd9Sstevel@tonic-gate 		/* Need to put some in file */
5407c478bd9Sstevel@tonic-gate 		if (!bfp->bf_ondisk)
5417c478bd9Sstevel@tonic-gate 		{
5427c478bd9Sstevel@tonic-gate 			MODE_T omask;
543*3ee0e492Sjbeck 			int save_errno;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 			/* Clear umask as bf_filemode are the true perms */
5467c478bd9Sstevel@tonic-gate 			omask = umask(0);
5477c478bd9Sstevel@tonic-gate 			retval = OPEN(bfp->bf_filename,
5487c478bd9Sstevel@tonic-gate 				      O_RDWR | O_CREAT | O_TRUNC | QF_O_EXTRA,
5497c478bd9Sstevel@tonic-gate 				      bfp->bf_filemode, bfp->bf_flags);
550*3ee0e492Sjbeck 			save_errno = errno;
5517c478bd9Sstevel@tonic-gate 			(void) umask(omask);
552*3ee0e492Sjbeck 			errno = save_errno;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			/* Couldn't create file: failure */
5557c478bd9Sstevel@tonic-gate 			if (retval < 0)
5567c478bd9Sstevel@tonic-gate 			{
5577c478bd9Sstevel@tonic-gate 				/*
5587c478bd9Sstevel@tonic-gate 				**  stdio may not be expecting these
5597c478bd9Sstevel@tonic-gate 				**  errnos from write()! Change to
5607c478bd9Sstevel@tonic-gate 				**  something which it can understand.
5617c478bd9Sstevel@tonic-gate 				**  Note that ENOSPC and EDQUOT are saved
5627c478bd9Sstevel@tonic-gate 				**  because they are actually valid for
5637c478bd9Sstevel@tonic-gate 				**  write().
5647c478bd9Sstevel@tonic-gate 				*/
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 				if (!(errno == ENOSPC
5677c478bd9Sstevel@tonic-gate #ifdef EDQUOT
5687c478bd9Sstevel@tonic-gate 				      || errno == EDQUOT
5697c478bd9Sstevel@tonic-gate #endif /* EDQUOT */
5707c478bd9Sstevel@tonic-gate 				     ))
5717c478bd9Sstevel@tonic-gate 					errno = EIO;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 				return -1;
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 			bfp->bf_disk_fd = retval;
5767c478bd9Sstevel@tonic-gate 			bfp->bf_ondisk = true;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		/* Catch a write() on an earlier failed write to disk */
5807c478bd9Sstevel@tonic-gate 		if (bfp->bf_ondisk && bfp->bf_disk_fd < 0)
5817c478bd9Sstevel@tonic-gate 		{
5827c478bd9Sstevel@tonic-gate 			errno = EIO;
5837c478bd9Sstevel@tonic-gate 			return -1;
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		if (lseek(bfp->bf_disk_fd,
5877c478bd9Sstevel@tonic-gate 			  bfp->bf_offset + count, SEEK_SET) < 0)
5887c478bd9Sstevel@tonic-gate 		{
5897c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) || (errno == ESPIPE))
5907c478bd9Sstevel@tonic-gate 			{
5917c478bd9Sstevel@tonic-gate 				/*
5927c478bd9Sstevel@tonic-gate 				**  stdio won't be expecting these
5937c478bd9Sstevel@tonic-gate 				**  errnos from write()! Change them into
5947c478bd9Sstevel@tonic-gate 				**  something which it can understand.
5957c478bd9Sstevel@tonic-gate 				*/
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 				errno = EIO;
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 			return -1;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		while (count < nbytes)
6037c478bd9Sstevel@tonic-gate 		{
6047c478bd9Sstevel@tonic-gate 			retval = write(bfp->bf_disk_fd, buf + count,
6057c478bd9Sstevel@tonic-gate 				       nbytes - count);
6067c478bd9Sstevel@tonic-gate 			if (retval < 0)
6077c478bd9Sstevel@tonic-gate 			{
6087c478bd9Sstevel@tonic-gate 				/* errno is set implicitly by write() */
6097c478bd9Sstevel@tonic-gate 				return -1;
6107c478bd9Sstevel@tonic-gate 			}
6117c478bd9Sstevel@tonic-gate 			else
6127c478bd9Sstevel@tonic-gate 				count += retval;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate finished:
6177c478bd9Sstevel@tonic-gate 	bfp->bf_offset += count;
6187c478bd9Sstevel@tonic-gate 	if (bfp->bf_offset > bfp->bf_size)
6197c478bd9Sstevel@tonic-gate 		bfp->bf_size = bfp->bf_offset;
6207c478bd9Sstevel@tonic-gate 	return count;
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate **  BFREWIND -- rewinds the SM_FILE_T *
6257c478bd9Sstevel@tonic-gate **
6267c478bd9Sstevel@tonic-gate **	Parameters:
6277c478bd9Sstevel@tonic-gate **		fp -- SM_FILE_T * to rewind
6287c478bd9Sstevel@tonic-gate **
6297c478bd9Sstevel@tonic-gate **	Returns:
6307c478bd9Sstevel@tonic-gate **		0 on success, -1 on error
6317c478bd9Sstevel@tonic-gate **
6327c478bd9Sstevel@tonic-gate **	Side Effects:
6337c478bd9Sstevel@tonic-gate **		rewinds the SM_FILE_T * and puts it into read mode. Normally
6347c478bd9Sstevel@tonic-gate **		one would bfopen() a file, write to it, then bfrewind() and
6357c478bd9Sstevel@tonic-gate **		fread(). If fp is not a buffered file, this is equivalent to
6367c478bd9Sstevel@tonic-gate **		rewind().
6377c478bd9Sstevel@tonic-gate **
6387c478bd9Sstevel@tonic-gate **	Sets errno:
6397c478bd9Sstevel@tonic-gate **		any value of errno specified by sm_io_rewind()
6407c478bd9Sstevel@tonic-gate */
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate int
bfrewind(fp)6437c478bd9Sstevel@tonic-gate bfrewind(fp)
6447c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	(void) sm_io_flush(fp, SM_TIME_DEFAULT);
6477c478bd9Sstevel@tonic-gate 	sm_io_clearerr(fp); /* quicker just to do it */
6487c478bd9Sstevel@tonic-gate 	return sm_io_seek(fp, SM_TIME_DEFAULT, 0, SM_IO_SEEK_SET);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate **  SM_BFCOMMIT -- "commits" the buffered file
6537c478bd9Sstevel@tonic-gate **
6547c478bd9Sstevel@tonic-gate **	Parameters:
6557c478bd9Sstevel@tonic-gate **		fp -- SM_FILE_T * to commit to disk
6567c478bd9Sstevel@tonic-gate **
6577c478bd9Sstevel@tonic-gate **	Returns:
6587c478bd9Sstevel@tonic-gate **		0 on success, -1 on error
6597c478bd9Sstevel@tonic-gate **
6607c478bd9Sstevel@tonic-gate **	Side Effects:
6617c478bd9Sstevel@tonic-gate **		Forces the given SM_FILE_T * to be written to disk if it is not
6627c478bd9Sstevel@tonic-gate **		already, and ensures that it will be kept after closing. If
6637c478bd9Sstevel@tonic-gate **		fp is not a buffered file, this is a no-op.
6647c478bd9Sstevel@tonic-gate **
6657c478bd9Sstevel@tonic-gate **	Sets errno:
6667c478bd9Sstevel@tonic-gate **		any value of errno specified by open()
6677c478bd9Sstevel@tonic-gate **		any value of errno specified by write()
6687c478bd9Sstevel@tonic-gate **		any value of errno specified by lseek()
6697c478bd9Sstevel@tonic-gate */
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate static int
sm_bfcommit(fp)6727c478bd9Sstevel@tonic-gate sm_bfcommit(fp)
6737c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	struct bf *bfp;
6767c478bd9Sstevel@tonic-gate 	int retval;
6777c478bd9Sstevel@tonic-gate 	int byteswritten;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/* Get associated bf structure */
6807c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* If already committed, noop */
6837c478bd9Sstevel@tonic-gate 	if (bfp->bf_committed)
6847c478bd9Sstevel@tonic-gate 		return 0;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* Do we need to open a file? */
6877c478bd9Sstevel@tonic-gate 	if (!bfp->bf_ondisk)
6887c478bd9Sstevel@tonic-gate 	{
6897c478bd9Sstevel@tonic-gate 		int save_errno;
6907c478bd9Sstevel@tonic-gate 		MODE_T omask;
6917c478bd9Sstevel@tonic-gate 		struct stat st;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		if (tTd(58, 8))
6947c478bd9Sstevel@tonic-gate 		{
6957c478bd9Sstevel@tonic-gate 			sm_dprintf("bfcommit(%s): to disk\n", bfp->bf_filename);
6967c478bd9Sstevel@tonic-gate 			if (tTd(58, 32))
6977c478bd9Sstevel@tonic-gate 				sm_dprintf("bfcommit(): filemode %o flags %ld\n",
6987c478bd9Sstevel@tonic-gate 					   bfp->bf_filemode, bfp->bf_flags);
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (stat(bfp->bf_filename, &st) == 0)
7027c478bd9Sstevel@tonic-gate 		{
7037c478bd9Sstevel@tonic-gate 			errno = EEXIST;
7047c478bd9Sstevel@tonic-gate 			return -1;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		/* Clear umask as bf_filemode are the true perms */
7087c478bd9Sstevel@tonic-gate 		omask = umask(0);
7097c478bd9Sstevel@tonic-gate 		retval = OPEN(bfp->bf_filename,
7107c478bd9Sstevel@tonic-gate 			      O_RDWR | O_CREAT | O_EXCL | QF_O_EXTRA,
7117c478bd9Sstevel@tonic-gate 			      bfp->bf_filemode, bfp->bf_flags);
7127c478bd9Sstevel@tonic-gate 		save_errno = errno;
7137c478bd9Sstevel@tonic-gate 		(void) umask(omask);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		/* Couldn't create file: failure */
7167c478bd9Sstevel@tonic-gate 		if (retval < 0)
7177c478bd9Sstevel@tonic-gate 		{
7187c478bd9Sstevel@tonic-gate 			/* errno is set implicitly by open() */
7197c478bd9Sstevel@tonic-gate 			errno = save_errno;
7207c478bd9Sstevel@tonic-gate 			return -1;
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		bfp->bf_disk_fd = retval;
7247c478bd9Sstevel@tonic-gate 		bfp->bf_ondisk = true;
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	/* Write out the contents of our buffer, if we have any */
7287c478bd9Sstevel@tonic-gate 	if (bfp->bf_buffilled > 0)
7297c478bd9Sstevel@tonic-gate 	{
7307c478bd9Sstevel@tonic-gate 		byteswritten = 0;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		if (lseek(bfp->bf_disk_fd, 0, SEEK_SET) < 0)
7337c478bd9Sstevel@tonic-gate 		{
7347c478bd9Sstevel@tonic-gate 			/* errno is set implicitly by lseek() */
7357c478bd9Sstevel@tonic-gate 			return -1;
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		while (byteswritten < bfp->bf_buffilled)
7397c478bd9Sstevel@tonic-gate 		{
7407c478bd9Sstevel@tonic-gate 			retval = write(bfp->bf_disk_fd,
7417c478bd9Sstevel@tonic-gate 				       bfp->bf_buf + byteswritten,
7427c478bd9Sstevel@tonic-gate 				       bfp->bf_buffilled - byteswritten);
7437c478bd9Sstevel@tonic-gate 			if (retval < 0)
7447c478bd9Sstevel@tonic-gate 			{
7457c478bd9Sstevel@tonic-gate 				/* errno is set implicitly by write() */
7467c478bd9Sstevel@tonic-gate 				return -1;
7477c478bd9Sstevel@tonic-gate 			}
7487c478bd9Sstevel@tonic-gate 			else
7497c478bd9Sstevel@tonic-gate 				byteswritten += retval;
7507c478bd9Sstevel@tonic-gate 		}
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 	bfp->bf_committed = true;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/* Invalidate buf; all goes to file now */
7557c478bd9Sstevel@tonic-gate 	bfp->bf_buffilled = 0;
7567c478bd9Sstevel@tonic-gate 	if (bfp->bf_bufsize > 0)
7577c478bd9Sstevel@tonic-gate 	{
7587c478bd9Sstevel@tonic-gate 		/* Don't need buffer anymore; free it */
7597c478bd9Sstevel@tonic-gate 		bfp->bf_bufsize = 0;
7607c478bd9Sstevel@tonic-gate 		sm_free(bfp->bf_buf);
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 	return 0;
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate **  SM_BFTRUNCATE -- rewinds and truncates the SM_FILE_T *
7677c478bd9Sstevel@tonic-gate **
7687c478bd9Sstevel@tonic-gate **	Parameters:
7697c478bd9Sstevel@tonic-gate **		fp -- SM_FILE_T * to truncate
7707c478bd9Sstevel@tonic-gate **
7717c478bd9Sstevel@tonic-gate **	Returns:
7727c478bd9Sstevel@tonic-gate **		0 on success, -1 on error
7737c478bd9Sstevel@tonic-gate **
7747c478bd9Sstevel@tonic-gate **	Side Effects:
7757c478bd9Sstevel@tonic-gate **		rewinds the SM_FILE_T *, truncates it to zero length, and puts
7767c478bd9Sstevel@tonic-gate **		it into write mode.
7777c478bd9Sstevel@tonic-gate **
7787c478bd9Sstevel@tonic-gate **	Sets errno:
7797c478bd9Sstevel@tonic-gate **		any value of errno specified by fseek()
7807c478bd9Sstevel@tonic-gate **		any value of errno specified by ftruncate()
7817c478bd9Sstevel@tonic-gate */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate static int
sm_bftruncate(fp)7847c478bd9Sstevel@tonic-gate sm_bftruncate(fp)
7857c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	struct bf *bfp;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (bfrewind(fp) < 0)
7907c478bd9Sstevel@tonic-gate 		return -1;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/* Get bf structure */
7937c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
7947c478bd9Sstevel@tonic-gate 	bfp->bf_buffilled = 0;
7957c478bd9Sstevel@tonic-gate 	bfp->bf_size = 0;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	/* Need to zero the buffer */
7987c478bd9Sstevel@tonic-gate 	if (bfp->bf_bufsize > 0)
7997c478bd9Sstevel@tonic-gate 		memset(bfp->bf_buf, '\0', bfp->bf_bufsize);
8007c478bd9Sstevel@tonic-gate 	if (bfp->bf_ondisk)
8017c478bd9Sstevel@tonic-gate 	{
8027c478bd9Sstevel@tonic-gate #if NOFTRUNCATE
8037c478bd9Sstevel@tonic-gate 		/* XXX: Not much we can do except rewind it */
8047c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8057c478bd9Sstevel@tonic-gate 		return -1;
8067c478bd9Sstevel@tonic-gate #else /* NOFTRUNCATE */
8077c478bd9Sstevel@tonic-gate 		return ftruncate(bfp->bf_disk_fd, 0);
8087c478bd9Sstevel@tonic-gate #endif /* NOFTRUNCATE */
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 	return 0;
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate **  SM_BFSETINFO -- set/change info for an open file pointer
8157c478bd9Sstevel@tonic-gate **
8167c478bd9Sstevel@tonic-gate **	Parameters:
8177c478bd9Sstevel@tonic-gate **		fp -- file pointer to get info about
8187c478bd9Sstevel@tonic-gate **		what -- type of info to set/change
8197c478bd9Sstevel@tonic-gate **		valp -- thing to set/change the info to
8207c478bd9Sstevel@tonic-gate **
8217c478bd9Sstevel@tonic-gate */
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate static int
sm_bfsetinfo(fp,what,valp)8247c478bd9Sstevel@tonic-gate sm_bfsetinfo(fp, what, valp)
8257c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
8267c478bd9Sstevel@tonic-gate 	int what;
8277c478bd9Sstevel@tonic-gate 	void *valp;
8287c478bd9Sstevel@tonic-gate {
8297c478bd9Sstevel@tonic-gate 	struct bf *bfp;
8307c478bd9Sstevel@tonic-gate 	int bsize;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/* Get bf structure */
8337c478bd9Sstevel@tonic-gate 	bfp = (struct bf *) fp->f_cookie;
8347c478bd9Sstevel@tonic-gate 	switch (what)
8357c478bd9Sstevel@tonic-gate 	{
8367c478bd9Sstevel@tonic-gate 	  case SM_BF_SETBUFSIZE:
8377c478bd9Sstevel@tonic-gate 		bsize = *((int *) valp);
8387c478bd9Sstevel@tonic-gate 		bfp->bf_bufsize = bsize;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		/* A zero bsize is valid, just don't allocate memory */
8417c478bd9Sstevel@tonic-gate 		if (bsize > 0)
8427c478bd9Sstevel@tonic-gate 		{
8437c478bd9Sstevel@tonic-gate 			bfp->bf_buf = (char *) sm_malloc(bsize);
8447c478bd9Sstevel@tonic-gate 			if (bfp->bf_buf == NULL)
8457c478bd9Sstevel@tonic-gate 			{
8467c478bd9Sstevel@tonic-gate 				bfp->bf_bufsize = 0;
8477c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
8487c478bd9Sstevel@tonic-gate 				return -1;
8497c478bd9Sstevel@tonic-gate 			}
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 		else
8527c478bd9Sstevel@tonic-gate 			bfp->bf_buf = NULL;
8537c478bd9Sstevel@tonic-gate 		return 0;
8547c478bd9Sstevel@tonic-gate 	  case SM_BF_COMMIT:
8557c478bd9Sstevel@tonic-gate 		return sm_bfcommit(fp);
8567c478bd9Sstevel@tonic-gate 	  case SM_BF_TRUNCATE:
8577c478bd9Sstevel@tonic-gate 		return sm_bftruncate(fp);
8587c478bd9Sstevel@tonic-gate 	  case SM_BF_TEST:
8597c478bd9Sstevel@tonic-gate 		return 1; /* always */
8607c478bd9Sstevel@tonic-gate 	  default:
8617c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8627c478bd9Sstevel@tonic-gate 		return -1;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate }
865