| 1 | /*!
 | 
|---|
| 2 |    \file tocwrite.c
 | 
|---|
| 3 |    \ingroup (PSIO)
 | 
|---|
| 4 | */
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include <stdlib.h>
 | 
|---|
| 7 | #include <unistd.h>
 | 
|---|
| 8 | #include <util/psi3/libpsio/psio.h>
 | 
|---|
| 9 | #include <util/psi3/libpsio/psifiles.h>
 | 
|---|
| 10 | 
 | 
|---|
| 11 | namespace psi3 {
 | 
|---|
| 12 | namespace libpsio {
 | 
|---|
| 13 | 
 | 
|---|
| 14 | /*!
 | 
|---|
| 15 | ** PSIO_TOCWRITE(): Write the table of contents for file number 'unit'.
 | 
|---|
| 16 | **
 | 
|---|
| 17 | ** \ingroup (PSIO)
 | 
|---|
| 18 | */
 | 
|---|
| 19 | int psio_tocwrite(unsigned int unit)
 | 
|---|
| 20 | {
 | 
|---|
| 21 |   unsigned int i;
 | 
|---|
| 22 |   int errcod, volume, entry_size, stream;
 | 
|---|
| 23 |   psio_ud *this_unit;
 | 
|---|
| 24 |   psio_tocentry *this_entry;
 | 
|---|
| 25 | 
 | 
|---|
| 26 |   this_unit = &(psio_unit[unit]);
 | 
|---|
| 27 |   entry_size = sizeof(psio_tocentry) - 2*sizeof(psio_tocentry *);
 | 
|---|
| 28 | 
 | 
|---|
| 29 |   /* Check that this unit is actually open */
 | 
|---|
| 30 |   if(this_unit->vol[0].stream == -1) return(0);
 | 
|---|
| 31 | 
 | 
|---|
| 32 |   /* Seek vol[0] to its beginning */
 | 
|---|
| 33 |   stream = this_unit->vol[0].stream;
 | 
|---|
| 34 |   errcod = lseek(stream, 0L, SEEK_SET);
 | 
|---|
| 35 |   if(errcod == -1) {
 | 
|---|
| 36 |      fprintf(stderr, "Error in PSIO_TOCWRITE()!\n");
 | 
|---|
| 37 |      exit(PSI_RETURN_FAILURE);
 | 
|---|
| 38 |     }
 | 
|---|
| 39 | 
 | 
|---|
| 40 |   /* Dump the TOC stats to disk */
 | 
|---|
| 41 |   errcod = write(stream, (char *) &(this_unit->tocaddress.page), sizeof(ULI));
 | 
|---|
| 42 |   if(errcod != sizeof(ULI)) {
 | 
|---|
| 43 |      fprintf(stderr, "Error in PSIO_TOCWRITE()!\n");
 | 
|---|
| 44 |      exit(PSI_RETURN_FAILURE);
 | 
|---|
| 45 |     }
 | 
|---|
| 46 |   errcod = write(stream, (char *) &(this_unit->tocaddress.offset), sizeof(ULI));
 | 
|---|
| 47 |   if(errcod != sizeof(ULI)) {
 | 
|---|
| 48 |      fprintf(stderr, "Error in PSIO_TOCWRITE()!\n");
 | 
|---|
| 49 |      exit(PSI_RETURN_FAILURE);
 | 
|---|
| 50 |     }
 | 
|---|
| 51 |   errcod = write(stream, (char *) &(this_unit->toclen), sizeof(ULI));
 | 
|---|
| 52 |   if(errcod != sizeof(ULI)) {
 | 
|---|
| 53 |      fprintf(stderr, "Error in PSIO_TOCWRITE()!\n");
 | 
|---|
| 54 |      exit(PSI_RETURN_FAILURE);
 | 
|---|
| 55 |     }
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   /* Seek the TOC volume to the correct position */
 | 
|---|
| 58 |   volume = (this_unit->tocaddress.page) % (this_unit->numvols);
 | 
|---|
| 59 |   errcod = psio_volseek(&(this_unit->vol[volume]), this_unit->tocaddress.page,
 | 
|---|
| 60 |                         this_unit->tocaddress.offset,this_unit->numvols);
 | 
|---|
| 61 |   if(errcod == -1) {
 | 
|---|
| 62 |       fprintf(stderr, "Error in PSIO_TOCWRITE()!\n");
 | 
|---|
| 63 |       exit(PSI_RETURN_FAILURE);
 | 
|---|
| 64 |     }
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   this_entry = this_unit->toc;
 | 
|---|
| 67 |   for(i=0; i < this_unit->toclen; i++) {
 | 
|---|
| 68 | 
 | 
|---|
| 69 |       /* This write() assumes a fixed ordering on the members of this_toc */
 | 
|---|
| 70 |       errcod = write(this_unit->vol[volume].stream, (char *) this_entry,
 | 
|---|
| 71 |                      entry_size);
 | 
|---|
| 72 |       if(errcod != entry_size) {
 | 
|---|
| 73 |       fprintf(stderr, "Error in PSIO_TOCWRITE()!\n");
 | 
|---|
| 74 |       exit(PSI_RETURN_FAILURE);
 | 
|---|
| 75 |         }
 | 
|---|
| 76 | 
 | 
|---|
| 77 |       this_entry = this_entry->next;
 | 
|---|
| 78 |     }
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   return(0);
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | }
 | 
|---|
| 84 | }
 | 
|---|