| 1 | /*!
 | 
|---|
| 2 |    \file open.cc
 | 
|---|
| 3 |    \ingroup (PSIO)
 | 
|---|
| 4 | */
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include <sys/types.h>
 | 
|---|
| 7 | #include <sys/stat.h>
 | 
|---|
| 8 | #include <fcntl.h>
 | 
|---|
| 9 | #include <string.h>
 | 
|---|
| 10 | #include <stdlib.h>
 | 
|---|
| 11 | #include <unistd.h>
 | 
|---|
| 12 | #include <util/psi3/libpsio/psio.h>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | namespace psi3 {
 | 
|---|
| 15 | namespace libpsio {
 | 
|---|
| 16 | 
 | 
|---|
| 17 | /*!
 | 
|---|
| 18 | ** PSIO_OPEN(): Opens a multivolume PSI direct access file for
 | 
|---|
| 19 | ** reading/writing data.
 | 
|---|
| 20 | **
 | 
|---|
| 21 | **  \param unit   = The PSI unit number used to identify the file to all
 | 
|---|
| 22 | **                  read and write functions.
 | 
|---|
| 23 | **  \param status = Indicates if the file is old (PSIO_OPEN_OLD) or new
 | 
|---|
| 24 | **                  (PSIO_OPEN_NEW). 
 | 
|---|
| 25 | **
 | 
|---|
| 26 | ** \ingroup (PSIO)
 | 
|---|
| 27 | */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | int psio_open(unsigned int unit, int status)
 | 
|---|
| 30 | {
 | 
|---|
| 31 |   unsigned int i, j;
 | 
|---|
| 32 |   int errcod, stream, tocstat;
 | 
|---|
| 33 |   char name[PSIO_MAXSTR],path[PSIO_MAXSTR],fullpath[PSIO_MAXSTR];
 | 
|---|
| 34 |   psio_ud *this_unit;
 | 
|---|
| 35 | 
 | 
|---|
| 36 |   this_unit = &(psio_unit[unit]);
 | 
|---|
| 37 | 
 | 
|---|
| 38 |   /* First check to see if this unit is aleady open */
 | 
|---|
| 39 |   if(this_unit->vol[0].stream != -1) psio_error(unit,PSIO_ERROR_REOPEN);
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   /* Get number of volumes to stripe across */
 | 
|---|
| 42 |   this_unit->numvols = psio_get_numvols(unit);
 | 
|---|
| 43 |   if(this_unit->numvols > PSIO_MAXVOL) psio_error(unit,PSIO_ERROR_MAXVOL);
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   if(!(this_unit->numvols)) this_unit->numvols = 1;
 | 
|---|
| 46 | 
 | 
|---|
| 47 |   /* Get the file name prefix */
 | 
|---|
| 48 |   errcod = psio_get_filename(unit,name);
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   /* Build the name for each volume and open the file */
 | 
|---|
| 51 |   for(i=0; i < this_unit->numvols; i++) {
 | 
|---|
| 52 |     errcod = psio_get_volpath(unit, i, path);
 | 
|---|
| 53 | 
 | 
|---|
| 54 |     if(errcod && this_unit->numvols > 1)
 | 
|---|
| 55 |       psio_error(unit,PSIO_ERROR_NOVOLPATH);
 | 
|---|
| 56 | 
 | 
|---|
| 57 |     sprintf(fullpath, "%s%s.%u", path, name, unit);
 | 
|---|
| 58 |     this_unit->vol[i].path = (char *) malloc(strlen(fullpath)+1);
 | 
|---|
| 59 |     strcpy(this_unit->vol[i].path,fullpath);
 | 
|---|
| 60 | 
 | 
|---|
| 61 |     /* Check if any previously opened volumes have the same path */
 | 
|---|
| 62 |     for(j=0; j < i; j++)
 | 
|---|
| 63 |       if (!strcmp(this_unit->vol[i].path,this_unit->vol[j].path))
 | 
|---|
| 64 |         psio_error(unit,PSIO_ERROR_IDENTVOLPATH);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |     /* Now open the volume */
 | 
|---|
| 67 |     if(status == PSIO_OPEN_OLD) {
 | 
|---|
| 68 |       this_unit->vol[i].stream =
 | 
|---|
| 69 |         open(this_unit->vol[i].path,O_CREAT|O_RDWR,0644);
 | 
|---|
| 70 |       if(this_unit->vol[i].stream == -1)
 | 
|---|
| 71 |         psio_error(unit,PSIO_ERROR_OPEN);
 | 
|---|
| 72 |     }
 | 
|---|
| 73 |     else if(status == PSIO_OPEN_NEW) {
 | 
|---|
| 74 |       this_unit->vol[i].stream =
 | 
|---|
| 75 |         open(this_unit->vol[i].path,O_CREAT|O_RDWR|O_TRUNC,0644);
 | 
|---|
| 76 |       if(this_unit->vol[i].stream == -1)
 | 
|---|
| 77 |         psio_error(unit,PSIO_ERROR_OPEN);
 | 
|---|
| 78 |     }
 | 
|---|
| 79 |     else psio_error(unit,PSIO_ERROR_OSTAT);
 | 
|---|
| 80 |   }
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   if (status == PSIO_OPEN_OLD) tocstat = psio_tocread(unit);
 | 
|---|
| 83 |   else if (status == PSIO_OPEN_NEW) {
 | 
|---|
| 84 |       
 | 
|---|
| 85 |     /* Init the TOC stats and write them to disk */
 | 
|---|
| 86 |     this_unit->tocaddress.page = 0;
 | 
|---|
| 87 |     this_unit->tocaddress.offset = 3*sizeof(ULI);
 | 
|---|
| 88 |     this_unit->toclen = 0;
 | 
|---|
| 89 |     this_unit->toc = NULL;
 | 
|---|
| 90 | 
 | 
|---|
| 91 |     /* Seek vol[0] to its beginning */
 | 
|---|
| 92 |     stream = this_unit->vol[0].stream;
 | 
|---|
| 93 |     errcod = lseek(stream, 0L, SEEK_SET);
 | 
|---|
| 94 |     if(errcod == -1) psio_error(unit,PSIO_ERROR_LSEEK);
 | 
|---|
| 95 |   
 | 
|---|
| 96 |     errcod = write(stream, (char *) &(this_unit->tocaddress.page),
 | 
|---|
| 97 |                    sizeof(ULI));
 | 
|---|
| 98 |     if(errcod != sizeof(ULI)) psio_error(unit,PSIO_ERROR_WRITE);
 | 
|---|
| 99 |     errcod = write(stream, (char *) &(this_unit->tocaddress.offset),
 | 
|---|
| 100 |                    sizeof(ULI));
 | 
|---|
| 101 |     if(errcod != sizeof(ULI)) psio_error(unit,PSIO_ERROR_WRITE);
 | 
|---|
| 102 |     errcod = write(stream, (char *) &(this_unit->toclen), sizeof(ULI));
 | 
|---|
| 103 |     if(errcod != sizeof(ULI)) psio_error(unit,PSIO_ERROR_WRITE);
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   }
 | 
|---|
| 106 |   else psio_error(unit,PSIO_ERROR_OSTAT);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   return(0);
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | }
 | 
|---|
| 112 | }
 | 
|---|