Blame lib/Mrm/MrmIfile.c

Packit b099d7
/* 
Packit b099d7
 * Motif
Packit b099d7
 *
Packit b099d7
 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
Packit b099d7
 *
Packit b099d7
 * These libraries and programs are free software; you can
Packit b099d7
 * redistribute them and/or modify them under the terms of the GNU
Packit b099d7
 * Lesser General Public License as published by the Free Software
Packit b099d7
 * Foundation; either version 2 of the License, or (at your option)
Packit b099d7
 * any later version.
Packit b099d7
 *
Packit b099d7
 * These libraries and programs are distributed in the hope that
Packit b099d7
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
Packit b099d7
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Packit b099d7
 * PURPOSE. See the GNU Lesser General Public License for more
Packit b099d7
 * details.
Packit b099d7
 *
Packit b099d7
 * You should have received a copy of the GNU Lesser General Public
Packit b099d7
 * License along with these librararies and programs; if not, write
Packit b099d7
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Packit b099d7
 * Floor, Boston, MA 02110-1301 USA
Packit b099d7
 */ 
Packit b099d7
/* 
Packit b099d7
 * HISTORY
Packit b099d7
 */ 
Packit b099d7
#ifdef HAVE_CONFIG_H
Packit b099d7
#include <config.h>
Packit b099d7
#endif
Packit b099d7
Packit b099d7
Packit b099d7
#ifdef REV_INFO
Packit b099d7
#ifndef lint
Packit b099d7
static char rcsid[] = "$XConsortium: MrmIfile.c /main/13 1996/11/13 13:56:30 drk $"
Packit b099d7
#endif
Packit b099d7
#endif
Packit b099d7
Packit b099d7
/* (c) Copyright 1989, 1990, DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS. */
Packit b099d7
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 *++
Packit b099d7
 *  FACILITY:
Packit b099d7
 *
Packit b099d7
 *      UIL Resource Manager (URM)
Packit b099d7
 *
Packit b099d7
 *  ABSTRACT:
Packit b099d7
 *
Packit b099d7
 *	This module contains the low-level file utilities
Packit b099d7
 *
Packit b099d7
 *--
Packit b099d7
 */
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 *
Packit b099d7
 *  INCLUDE FILES
Packit b099d7
 *
Packit b099d7
 */
Packit b099d7
Packit b099d7
#include <Mrm/MrmAppl.h>
Packit b099d7
#include <Mrm/Mrm.h>
Packit b099d7
#include <Mrm/IDB.h>
Packit b099d7
Packit b099d7
#include <stdio.h>		/* Standard IO definitions		*/
Packit b099d7
#include <errno.h>
Packit b099d7
#include <fcntl.h>
Packit b099d7
Packit b099d7
#ifndef X_NOT_STDC_ENV
Packit b099d7
#include <unistd.h>
Packit b099d7
#endif
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 *
Packit b099d7
 *  DEFINE and MACRO DEFINITIONS
Packit b099d7
 *
Packit b099d7
 */
Packit b099d7
Packit b099d7
#define	PMODE	0666	/* Default protection mode before umask		*/
Packit b099d7
#define FAILURE	-1	/* creat/stat returns this			*/
Packit b099d7

Packit b099d7
/*
Packit b099d7
 *++
Packit b099d7
 *
Packit b099d7
 *  PROCEDURE DESCRIPTION:
Packit b099d7
 *
Packit b099d7
 *	This routine will take the file name specified and
Packit b099d7
 *	open or create it depending on the access parameter.
Packit b099d7
 *	An attempt is made to save any existing file of the
Packit b099d7
 *	same name.
Packit b099d7
 *
Packit b099d7
 *
Packit b099d7
 *  FORMAL PARAMETERS:
Packit b099d7
 *
Packit b099d7
 * 	name		the system-dependent file spec of the IDB file
Packit b099d7
 *			to be opened.
Packit b099d7
 *	accss		access type desired, read or write access.
Packit b099d7
 *	os_ext		an operating specific structure to take advantage
Packit b099d7
 *			of file system features (if any).
Packit b099d7
 *	file_id		IDB file id used in all calls to low level routines.
Packit b099d7
 *	returned_fname	The resultant file name.
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT INPUTS:
Packit b099d7
 *
Packit b099d7
 *      NONE
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT OUTPUTS:
Packit b099d7
 *
Packit b099d7
 *      NONE
Packit b099d7
 *
Packit b099d7
 *  FUNCTION VALUE:
Packit b099d7
 *
Packit b099d7
 *	Returns an integer:
Packit b099d7
 *
Packit b099d7
 *	MrmSUCCESS	- When access is read and open works
Packit b099d7
 *	MrmCREATE_NEW	- When access is write and open works
Packit b099d7
 *	MrmNOT_FOUND	- When access is read and the file isn't present
Packit b099d7
 *	MrmFAILURE	- When the open fails for any other reason
Packit b099d7
 *
Packit b099d7
 *  SIDE EFFECTS:
Packit b099d7
 *
Packit b099d7
 *      Opens or creates the named file and assigns a channel to it.
Packit b099d7
 *
Packit b099d7
 *--
Packit b099d7
 */
Packit b099d7
Packit b099d7
Cardinal 
Packit b099d7
Idb__FU_OpenFile (char 			*name,
Packit b099d7
		  MrmCode 		access,
Packit b099d7
		  MrmOsOpenParamPtr 	os_ext,
Packit b099d7
		  IDBLowLevelFilePtr 	*file_id,
Packit b099d7
		  char 			*returned_fname)
Packit b099d7
{
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * Local variables
Packit b099d7
   */
Packit b099d7
  int		file_desc;		/* 'unix' file descriptor */
Packit b099d7
  int		length;			/* the length of the above string */
Packit b099d7
  IDBLowLevelFile *a_file;		/* pointer to the file_id */
Packit b099d7
Packit b099d7
  /* Fill in the result name with the name specified so far */
Packit b099d7
  length = strlen (name);
Packit b099d7
  strcpy (returned_fname, name);
Packit b099d7
  returned_fname[length] = 0;
Packit b099d7
Packit b099d7
  /* Check if this file is to be opened for read or write access */
Packit b099d7
  if (access == URMWriteAccess)
Packit b099d7
    {
Packit b099d7
      file_desc = open (name, O_RDWR, PMODE);
Packit b099d7
      if (file_desc != FAILURE)		/* filename already exists. */
Packit b099d7
	{
Packit b099d7
	  if (os_ext == 0)
Packit b099d7
	    return MrmFAILURE;
Packit b099d7
	  else if (!os_ext->nam_flg.clobber_flg)
Packit b099d7
	    return MrmEXISTS;		/* no clobber. return Exists */
Packit b099d7
	  else if (os_ext->version != MrmOsOpenParamVersion)
Packit b099d7
	    return MrmFAILURE;
Packit b099d7
	  (void) close (file_desc);	/* we care not what close returns*/
Packit b099d7
	}
Packit b099d7
Packit b099d7
      file_desc = creat (name,PMODE);
Packit b099d7
      if (file_desc == FAILURE)		/* verify that worked */
Packit b099d7
	return MrmFAILURE;
Packit b099d7
Packit b099d7
      close (file_desc);		/* we care not what close returns */
Packit b099d7
      file_desc = open (name, O_RDWR, PMODE);
Packit b099d7
Packit b099d7
      if (file_desc == FAILURE)		/* verify that worked */
Packit b099d7
	return MrmFAILURE;
Packit b099d7
    }
Packit b099d7
Packit b099d7
Packit b099d7
  /* Else this file is to opened for read access */
Packit b099d7
  else if (access == URMReadAccess)
Packit b099d7
    {
Packit b099d7
      file_desc = open (name, O_RDONLY, PMODE);
Packit b099d7
Packit b099d7
      /* verify that worked */
Packit b099d7
      if (file_desc == FAILURE)
Packit b099d7
	{
Packit b099d7
	  if ( errno == EACCES )
Packit b099d7
	    return MrmFAILURE;
Packit b099d7
	  else
Packit b099d7
	    return MrmNOT_FOUND;
Packit b099d7
	}
Packit b099d7
    }
Packit b099d7
Packit b099d7
  /* Not URMReadAccess or URMWriteAccess, so return invalid access type	    */
Packit b099d7
  else
Packit b099d7
    return MrmFAILURE;
Packit b099d7
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * now all we have to do is set up the IDBFile and return the
Packit b099d7
   * proper success code.
Packit b099d7
   */
Packit b099d7
Packit b099d7
  *file_id = (IDBLowLevelFilePtr)
Packit b099d7
    XtMalloc(sizeof (IDBLowLevelFile));
Packit b099d7
  if (*file_id==0)
Packit b099d7
    return MrmFAILURE;
Packit b099d7
Packit b099d7
  a_file = (IDBLowLevelFile *) *file_id;
Packit b099d7
Packit b099d7
  a_file->name = XtMalloc (length+1);
Packit b099d7
  if (a_file->name==0)
Packit b099d7
    {
Packit b099d7
      XtFree ((char*)*file_id);
Packit b099d7
      return (MrmFAILURE);
Packit b099d7
    }
Packit b099d7
Packit b099d7
  a_file->file_desc = file_desc;
Packit b099d7
  strcpy (a_file->name, name);
Packit b099d7
  a_file->name[length] = 0;
Packit b099d7
Packit b099d7
  if (access == URMWriteAccess)
Packit b099d7
    return (MrmCREATE_NEW);
Packit b099d7
  else
Packit b099d7
    return (MrmSUCCESS);
Packit b099d7
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7

Packit b099d7
/*
Packit b099d7
 *++
Packit b099d7
 *
Packit b099d7
 *  PROCEDURE DESCRIPTION:
Packit b099d7
 *
Packit b099d7
 *	This routine will close the file and free any allocated storage
Packit b099d7
 *
Packit b099d7
 *
Packit b099d7
 *  FORMAL PARAMETERS:
Packit b099d7
 *
Packit b099d7
 *	file_id		IDB file id
Packit b099d7
 *	delete		delete the file if == true
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT INPUTS:
Packit b099d7
 *
Packit b099d7
 *      the file name and channel from the IDBFile record
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT OUTPUTS:
Packit b099d7
 *
Packit b099d7
 *      NONE
Packit b099d7
 *
Packit b099d7
 *  FUNCTION VALUE:
Packit b099d7
 *
Packit b099d7
 *	MrmSUCCESS	- When the file is closed [and deleted] successfully
Packit b099d7
 *	MrmFAILURE	- When the close fails
Packit b099d7
 *
Packit b099d7
 *  SIDE EFFECTS:
Packit b099d7
 *
Packit b099d7
 *      Closes the file, deassigns the channel and possible deletes the file.
Packit b099d7
 *
Packit b099d7
 *--
Packit b099d7
 */
Packit b099d7
Packit b099d7
Cardinal 
Packit b099d7
Idb__FU_CloseFile (IDBLowLevelFile	*file_id ,
Packit b099d7
		   int			delete)
Packit b099d7
{
Packit b099d7
  /*
Packit b099d7
   * Local variables
Packit b099d7
   */
Packit b099d7
Packit b099d7
  int	status;				/* ret status for sys services	*/
Packit b099d7
Packit b099d7
  status = close (file_id->file_desc);
Packit b099d7
  if (status != 0)
Packit b099d7
    return MrmFAILURE;
Packit b099d7
Packit b099d7
  if (delete) {
Packit b099d7
    status = unlink (file_id->name);
Packit b099d7
  }
Packit b099d7
Packit b099d7
  XtFree (file_id->name);
Packit b099d7
  XtFree ((char*)file_id);
Packit b099d7
  return MrmSUCCESS;
Packit b099d7
Packit b099d7
}
Packit b099d7

Packit b099d7
/*
Packit b099d7
 *++
Packit b099d7
 *
Packit b099d7
 *  PROCEDURE DESCRIPTION:
Packit b099d7
 *
Packit b099d7
 *	This function reads in the desired record into the given
Packit b099d7
 *	buffer.
Packit b099d7
 *
Packit b099d7
 *  FORMAL PARAMETERS:
Packit b099d7
 *
Packit b099d7
 *	file_id		the IDB file identifier
Packit b099d7
 *	block_num	the record number to retrieve
Packit b099d7
 *	buffer		pointer to the buffer to fill in
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT INPUTS:
Packit b099d7
 *
Packit b099d7
 *      NONE
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT OUTPUTS:
Packit b099d7
 *
Packit b099d7
 *      NONE
Packit b099d7
 *
Packit b099d7
 *  FUNCTION VALUE:
Packit b099d7
 *
Packit b099d7
 *	MrmSUCCESS	operation succeeded
Packit b099d7
 *	MrmNOT_FOUND	entry not found
Packit b099d7
 *	MrmFAILURE	operation failed, no further reason
Packit b099d7
 *
Packit b099d7
 *  SIDE EFFECTS:
Packit b099d7
 *
Packit b099d7
 *      The buffer is filled in.  Should the $READ fail the buffer's
Packit b099d7
 *	content is not predictable.
Packit b099d7
 *
Packit b099d7
 *--
Packit b099d7
 */
Packit b099d7
Packit b099d7
Cardinal 
Packit b099d7
Idb__FU_GetBlock (IDBLowLevelFile	*file_id,
Packit b099d7
		  IDBRecordNumber	block_num,
Packit b099d7
		  char			*buffer)
Packit b099d7
{
Packit b099d7
  /*
Packit b099d7
   * Local variables
Packit b099d7
   */
Packit b099d7
  int	number_read;		/* the number of bytes actually read	*/
Packit b099d7
  int	fdesc ;			/* file descriptor from lowlevel desc */
Packit b099d7
Packit b099d7
Packit b099d7
  fdesc = file_id->file_desc ;
Packit b099d7
  lseek (fdesc, (block_num-1)*IDBRecordSize, 0);
Packit b099d7
  number_read = read (file_id->file_desc, buffer, IDBRecordSize);
Packit b099d7
Packit b099d7
  if (number_read != IDBRecordSize) 
Packit b099d7
    return MrmFAILURE;
Packit b099d7
  else
Packit b099d7
    return MrmSUCCESS;
Packit b099d7
}
Packit b099d7
Packit b099d7

Packit b099d7
/*
Packit b099d7
 *++
Packit b099d7
 *
Packit b099d7
 *  PROCEDURE DESCRIPTION:
Packit b099d7
 *
Packit b099d7
 *	This function writes the data in the givin buffer into
Packit b099d7
 *	the desired record in the file.
Packit b099d7
 *
Packit b099d7
 *  FORMAL PARAMETERS:
Packit b099d7
 *
Packit b099d7
 *	file_id		the IDB file identifier
Packit b099d7
 *	block_num	the record number to write
Packit b099d7
 *	buffer		pointer to the buffer to read from
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT INPUTS:
Packit b099d7
 *
Packit b099d7
 *      NONE
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT OUTPUTS:
Packit b099d7
 *
Packit b099d7
 *      NONE
Packit b099d7
 *
Packit b099d7
 *  FUNCTION VALUE:
Packit b099d7
 *
Packit b099d7
 *	Returns an integer by value:
Packit b099d7
 *
Packit b099d7
 *	MrmSUCCESS	operation succeeded
Packit b099d7
 *	MrmFAILURE	operation failed, no further reason
Packit b099d7
 *
Packit b099d7
 *  SIDE EFFECTS:
Packit b099d7
 *
Packit b099d7
 *	the file is modified.
Packit b099d7
 *
Packit b099d7
 *--
Packit b099d7
 */
Packit b099d7
Packit b099d7
Cardinal 
Packit b099d7
Idb__FU_PutBlock (IDBLowLevelFile	*file_id,
Packit b099d7
		  IDBRecordNumber	block_num,
Packit b099d7
		  char			*buffer)
Packit b099d7
{
Packit b099d7
  /*
Packit b099d7
   * Local variables
Packit b099d7
   */
Packit b099d7
  int	number_written;		/* the # of bytes acctually written	*/
Packit b099d7
  int	fdesc ;			/* file descriptor from lowlevel desc */
Packit b099d7
Packit b099d7
Packit b099d7
  fdesc = file_id->file_desc ;
Packit b099d7
  lseek (fdesc, (block_num-1)*IDBRecordSize, 0);
Packit b099d7
  number_written = write (file_id->file_desc, buffer, IDBRecordSize);
Packit b099d7
Packit b099d7
  if (number_written != IDBRecordSize)
Packit b099d7
    return MrmFAILURE;
Packit b099d7
  else
Packit b099d7
    return MrmSUCCESS;
Packit b099d7
}
Packit b099d7