| 1 | /*! \defgroup PSIO libpsio: The PSI I/O Library */ | 
|---|
| 2 |  | 
|---|
| 3 | /*! | 
|---|
| 4 | ** \file close.cc | 
|---|
| 5 | ** \ingroup (PSIO) | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include <unistd.h> | 
|---|
| 9 | #include <string.h> | 
|---|
| 10 | #include <stdlib.h> | 
|---|
| 11 | #include <util/psi3/libpsio/psio.h> | 
|---|
| 12 |  | 
|---|
| 13 | namespace psi3 { | 
|---|
| 14 | namespace libpsio { | 
|---|
| 15 |  | 
|---|
| 16 | /*! | 
|---|
| 17 | ** \ingroup (PSIO) | 
|---|
| 18 | ** PSIO_CLOSE(): Closes a multivolume PSI direct access file. | 
|---|
| 19 | ** | 
|---|
| 20 | **  \param unit = The PSI unit number used to identify the file to all read | 
|---|
| 21 | **                and write functions. | 
|---|
| 22 | **  \param keep = Boolean to indicate if the file should be deleted (0) or | 
|---|
| 23 | **                retained (1). | 
|---|
| 24 | */ | 
|---|
| 25 |  | 
|---|
| 26 | int psio_close(unsigned int unit, int keep) | 
|---|
| 27 | { | 
|---|
| 28 | unsigned int i; | 
|---|
| 29 | psio_ud *this_unit; | 
|---|
| 30 | psio_tocentry *this_entry, *next_entry; | 
|---|
| 31 |  | 
|---|
| 32 | this_unit = &(psio_unit[unit]); | 
|---|
| 33 |  | 
|---|
| 34 | /* First check to see if this unit is already closed */ | 
|---|
| 35 | if(this_unit->vol[0].stream == -1) psio_error(unit,PSIO_ERROR_RECLOSE); | 
|---|
| 36 |  | 
|---|
| 37 | /* Dump the current TOC back out to disk */ | 
|---|
| 38 | psio_tocwrite(unit); | 
|---|
| 39 |  | 
|---|
| 40 | /* Free the TOC */ | 
|---|
| 41 | this_entry = this_unit->toc; | 
|---|
| 42 | for(i=0; i < this_unit->toclen; i++) { | 
|---|
| 43 | next_entry = this_entry->next; | 
|---|
| 44 | free(this_entry); | 
|---|
| 45 | this_entry = next_entry; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /* Close each volume (remove if necessary) and free the path */ | 
|---|
| 49 | for(i=0; i < this_unit->numvols; i++) { | 
|---|
| 50 |  | 
|---|
| 51 | if(close(this_unit->vol[i].stream) == -1) | 
|---|
| 52 | psio_error(unit,PSIO_ERROR_CLOSE); | 
|---|
| 53 |  | 
|---|
| 54 | /* Delete the file completely if requested */ | 
|---|
| 55 | if(!keep) unlink(this_unit->vol[i].path); | 
|---|
| 56 |  | 
|---|
| 57 | free(this_unit->vol[i].path); | 
|---|
| 58 | this_unit->vol[i].path = NULL; | 
|---|
| 59 | this_unit->vol[i].stream = -1; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | /* Reset the global page stats to zero */ | 
|---|
| 63 | this_unit->numvols = 0; | 
|---|
| 64 | this_unit->toclen = 0; | 
|---|
| 65 | this_unit->tocaddress.page = 0; | 
|---|
| 66 | this_unit->tocaddress.offset = 0; | 
|---|
| 67 |  | 
|---|
| 68 | return(0); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|