| |
|
|
| |
| |
| |
|
|
| #include <string.h> |
| #include <stdlib.h> |
| #include "fitsio2.h" |
| #include "group.h" |
|
|
| #if defined(unix) || defined(__unix__) || defined(__unix) |
| #include <pwd.h> |
|
|
| #ifdef REPLACE_LINKS |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #endif |
|
|
| #endif |
|
|
| #ifdef HAVE_FTRUNCATE |
| #if defined(unix) || defined(__unix__) || defined(__unix) || defined(HAVE_UNISTD_H) |
| #include <unistd.h> |
| #endif |
| #endif |
|
|
| #define IO_SEEK 0 |
| #define IO_READ 1 |
| #define IO_WRITE 2 |
|
|
| static char file_outfile[FLEN_FILENAME]; |
|
|
| typedef struct /* structure containing disk file structure */ |
| { |
| FILE *fileptr; |
| LONGLONG currentpos; |
| int last_io_op; |
| } diskdriver; |
|
|
| static diskdriver handleTable[NMAXFILES]; |
|
|
| |
| int file_init(void) |
| { |
| int ii; |
|
|
| for (ii = 0; ii < NMAXFILES; ii++) |
| { |
| handleTable[ii].fileptr = 0; |
| } |
| return(0); |
| } |
| |
| int file_setoptions(int options) |
| { |
| |
| options = 0; |
| return(options); |
| } |
| |
| int file_getoptions(int *options) |
| { |
| *options = 0; |
| return(0); |
| } |
| |
| int file_getversion(int *version) |
| { |
| *version = 10; |
| return(0); |
| } |
| |
| int file_shutdown(void) |
| { |
| return(0); |
| } |
| |
| int file_open(char *filename, int rwmode, int *handle) |
| { |
| FILE *diskfile; |
| int copyhandle, ii, status; |
| char recbuf[2880]; |
| size_t nread; |
|
|
| |
| |
| |
| |
| |
| |
|
|
| if (*file_outfile) |
| { |
| |
| status = file_openfile(filename, READONLY, &diskfile); |
| if (status) { |
| file_outfile[0] = '\0'; |
| return(status); |
| } |
| |
| |
| status = file_create(file_outfile,handle); |
| if (status) |
| { |
| ffpmsg("Unable to create output file for copy of input file:"); |
| ffpmsg(file_outfile); |
| file_outfile[0] = '\0'; |
| return(status); |
| } |
|
|
| |
| while(0 != (nread = fread(recbuf,1,2880, diskfile))) |
| { |
| status = file_write(*handle, recbuf, nread); |
| if (status) { |
| file_outfile[0] = '\0'; |
| return(status); |
| } |
| } |
|
|
| |
| fclose(diskfile); |
| copyhandle = *handle; |
| file_close(*handle); |
| *handle = copyhandle; |
|
|
| |
| status = file_openfile(file_outfile, rwmode, &diskfile); |
| file_outfile[0] = '\0'; |
| } |
| else |
| { |
| *handle = -1; |
| for (ii = 0; ii < NMAXFILES; ii++) |
| { |
| if (handleTable[ii].fileptr == 0) |
| { |
| *handle = ii; |
| break; |
| } |
| } |
|
|
| if (*handle == -1) |
| return(TOO_MANY_FILES); |
|
|
| |
| status = file_openfile(filename, rwmode, &diskfile); |
| } |
|
|
| handleTable[*handle].fileptr = diskfile; |
| handleTable[*handle].currentpos = 0; |
| handleTable[*handle].last_io_op = IO_SEEK; |
|
|
| return(status); |
| } |
| |
| int file_openfile(char *filename, int rwmode, FILE **diskfile) |
| |
| |
| |
| { |
| char mode[4]; |
|
|
| #if defined(unix) || defined(__unix__) || defined(__unix) |
| char tempname[1024], *cptr, user[80]; |
| struct passwd *pwd; |
| int ii = 0; |
|
|
| #if defined(REPLACE_LINKS) |
| struct stat stbuf; |
| int success = 0; |
| size_t n; |
| FILE *f1, *f2; |
| char buf[BUFSIZ]; |
| #endif |
|
|
| #endif |
|
|
| if (rwmode == READWRITE) |
| { |
| strcpy(mode, "r+b"); |
| } |
| else |
| { |
| strcpy(mode, "rb"); |
| } |
|
|
| #if MACHINE == ALPHAVMS || MACHINE == VAXVMS |
| |
| |
| *diskfile = fopen(filename, mode, "rfm=fix", "mrs=2880", "ctx=stm"); |
|
|
| #elif defined(unix) || defined(__unix__) || defined(__unix) |
|
|
| |
|
|
| if (*filename == '~') |
| { |
| if (filename[1] == '/') |
| { |
| cptr = getenv("HOME"); |
| if (cptr) |
| { |
| if (strlen(cptr) + strlen(filename+1) > 1023) |
| return(FILE_NOT_OPENED); |
|
|
| strcpy(tempname, cptr); |
| strcat(tempname, filename+1); |
| } |
| else |
| { |
| if (strlen(filename) > 1023) |
| return(FILE_NOT_OPENED); |
|
|
| strcpy(tempname, filename); |
| } |
| } |
| else |
| { |
| |
| cptr = filename+1; |
| while (*cptr && (*cptr != '/')) |
| { |
| user[ii] = *cptr; |
| cptr++; |
| ii++; |
| } |
| user[ii] = '\0'; |
|
|
| |
| pwd = getpwnam(user); |
|
|
| |
| if (strlen(pwd->pw_dir) + strlen(cptr) > 1023) |
| return(FILE_NOT_OPENED); |
|
|
| strcpy(tempname, pwd->pw_dir); |
| strcat(tempname, cptr); |
| } |
|
|
| *diskfile = fopen(tempname, mode); |
| } |
| else |
| { |
| |
| *diskfile = fopen(filename, mode); |
|
|
| #if defined(REPLACE_LINKS) |
|
|
| if (!(*diskfile) && (rwmode == READWRITE)) |
| { |
| |
| |
| |
|
|
| lstat(filename, &stbuf); |
| if ((stbuf.st_mode & S_IFMT) == S_IFLNK) |
| { |
| if ((f1 = fopen(filename, "rb")) != 0) |
| { |
|
|
| if (strlen(filename) + 7 > 1023) |
| return(FILE_NOT_OPENED); |
|
|
| strcpy(tempname, filename); |
| strcat(tempname, ".TmxFil"); |
| if ((f2 = fopen(tempname, "wb")) != 0) |
| { |
| success = 1; |
| while ((n = fread(buf, 1, BUFSIZ, f1)) > 0) |
| { |
| |
| if (fwrite(buf, 1, n, f2) != n) |
| { |
| success = 0; |
| break; |
| } |
| } |
| fclose(f2); |
| } |
| fclose(f1); |
| |
| if (success) |
| { |
| |
| remove(filename); |
| rename(tempname, filename); |
|
|
| |
| *diskfile = fopen(filename, mode); |
| } |
| else |
| remove(tempname); |
| } |
| } |
| } |
| #endif |
|
|
| } |
|
|
| #else |
|
|
| |
| *diskfile = fopen(filename, mode); |
|
|
| #endif |
|
|
| if (!(*diskfile)) |
| { |
| return(FILE_NOT_OPENED); |
| } |
| return(0); |
| } |
| |
| int file_create(char *filename, int *handle) |
| { |
| FILE *diskfile; |
| int ii; |
| char mode[4]; |
| |
| int status = 0, rootlen, rootlen2, slen; |
| char *cptr, *cpos; |
| char cwd[FLEN_FILENAME], absURL[FLEN_FILENAME]; |
| char rootstring[256], rootstring2[256]; |
| char username[FLEN_FILENAME], userroot[FLEN_FILENAME], userroot2[FLEN_FILENAME]; |
|
|
| cptr = getenv("HERA_DATA_DIRECTORY"); |
| if (cptr) { |
| |
| |
| |
| |
|
|
| |
| |
| |
| if (strlen(cptr) > 200) |
| return(FILE_NOT_CREATED); |
|
|
| |
| |
|
|
| strcpy(rootstring, cptr); |
| cpos = strchr(rootstring, ';'); |
| if (cpos) { |
| *cpos = '\0'; |
| cpos++; |
| strcpy(rootstring2, cpos); |
| } else { |
| *rootstring2 = '\0'; |
| } |
| |
| |
| |
| |
| |
| |
| fits_get_cwd(cwd, &status); |
| slen = strlen(cwd); |
| if ((slen < FLEN_FILENAME) && cwd[slen-1] != '/') strcat(cwd,"/"); |
|
|
|
|
| |
| rootlen = strlen(rootstring); |
| if (strncmp(rootstring, cwd, rootlen)) { |
| ffpmsg("invalid CWD: does not match root data directory"); |
| return(FILE_NOT_CREATED); |
| } else { |
|
|
| |
| strncpy(username, cwd+rootlen, 50); |
| username[50]=0; |
| cpos=strchr(username, '/'); |
| if (!cpos) { |
| ffpmsg("invalid CWD: not equal to root data directory + username"); |
| return(FILE_NOT_CREATED); |
| } else { |
| *(cpos+1) = '\0'; |
|
|
| |
| strcpy(userroot, rootstring); |
| strcat(userroot, username); |
| rootlen = strlen(userroot); |
|
|
| |
| strcpy(userroot2, rootstring2); |
| strcat(userroot2, username); |
| rootlen2 = strlen(userroot2); |
|
|
| |
| fits_relurl2url(cwd, filename, absURL, &status); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if ( strncmp(userroot, absURL, rootlen) && |
| strncmp(userroot2, absURL, rootlen2) ) { |
| ffpmsg("invalid filename: path not within user directory"); |
| return(FILE_NOT_CREATED); |
| } |
| } |
| } |
| |
| } |
| |
| *handle = -1; |
| for (ii = 0; ii < NMAXFILES; ii++) |
| { |
| if (handleTable[ii].fileptr == 0) |
| { |
| *handle = ii; |
| break; |
| } |
| } |
| if (*handle == -1) |
| return(TOO_MANY_FILES); |
|
|
| strcpy(mode, "w+b"); |
|
|
| diskfile = fopen(filename, "r"); |
|
|
| if (diskfile) |
| { |
| fclose(diskfile); |
| return(FILE_NOT_CREATED); |
| } |
|
|
| #if MACHINE == ALPHAVMS || MACHINE == VAXVMS |
| |
| |
| diskfile = fopen(filename, mode, "rfm=fix", "mrs=2880", "ctx=stm"); |
| #else |
| diskfile = fopen(filename, mode); |
| #endif |
|
|
| if (!(diskfile)) |
| { |
| return(FILE_NOT_CREATED); |
| } |
|
|
| handleTable[ii].fileptr = diskfile; |
| handleTable[ii].currentpos = 0; |
| handleTable[ii].last_io_op = IO_SEEK; |
|
|
| return(0); |
| } |
| |
| int file_truncate(int handle, LONGLONG filesize) |
| |
| |
| |
| { |
|
|
| #ifdef HAVE_FTRUNCATE |
| int fdesc; |
|
|
| fdesc = fileno(handleTable[handle].fileptr); |
| ftruncate(fdesc, (OFF_T) filesize); |
| file_seek(handle, filesize); |
|
|
| handleTable[handle].currentpos = filesize; |
| handleTable[handle].last_io_op = IO_SEEK; |
|
|
| #endif |
|
|
| return(0); |
| } |
| |
| int file_size(int handle, LONGLONG *filesize) |
| |
| |
| |
| { |
| OFF_T position1,position2; |
| FILE *diskfile; |
|
|
| diskfile = handleTable[handle].fileptr; |
|
|
| #if defined(_MSC_VER) && (_MSC_VER >= 1400) |
| |
| |
| |
|
|
| position1 = _ftelli64(diskfile); |
| if (position1 < 0) |
| return(SEEK_ERROR); |
|
|
| if (_fseeki64(diskfile, 0, 2) != 0) |
| return(SEEK_ERROR); |
|
|
| position2 = _ftelli64(diskfile); |
| if (position2 < 0) |
| return(SEEK_ERROR); |
|
|
| if (_fseeki64(diskfile, position1, 0) != 0) |
| return(SEEK_ERROR); |
|
|
| #elif _FILE_OFFSET_BITS - 0 == 64 |
|
|
| |
| |
|
|
| position1 = ftello(diskfile); |
| if (position1 < 0) |
| return(SEEK_ERROR); |
|
|
| if (fseeko(diskfile, 0, 2) != 0) |
| return(SEEK_ERROR); |
|
|
| position2 = ftello(diskfile); |
| if (position2 < 0) |
| return(SEEK_ERROR); |
|
|
| if (fseeko(diskfile, position1, 0) != 0) |
| return(SEEK_ERROR); |
|
|
| #else |
|
|
| position1 = ftell(diskfile); |
| if (position1 < 0) |
| return(SEEK_ERROR); |
|
|
| if (fseek(diskfile, 0, 2) != 0) |
| return(SEEK_ERROR); |
|
|
| position2 = ftell(diskfile); |
| if (position2 < 0) |
| return(SEEK_ERROR); |
|
|
| if (fseek(diskfile, position1, 0) != 0) |
| return(SEEK_ERROR); |
|
|
| #endif |
|
|
| *filesize = (LONGLONG) position2; |
| |
| return(0); |
| } |
| |
| int file_close(int handle) |
| |
| |
| |
| { |
| |
| if (fclose(handleTable[handle].fileptr) ) |
| return(FILE_NOT_CLOSED); |
|
|
| handleTable[handle].fileptr = 0; |
| return(0); |
| } |
| |
| int file_remove(char *filename) |
| |
| |
| |
| { |
| remove(filename); |
| return(0); |
| } |
| |
| int file_flush(int handle) |
| |
| |
| |
| { |
| if (fflush(handleTable[handle].fileptr) ) |
| return(WRITE_ERROR); |
|
|
| |
| |
| |
| |
|
|
| #if MACHINE == IBMPC |
|
|
| if (file_seek(handle, handleTable[handle].currentpos)) |
| return(SEEK_ERROR); |
|
|
| #endif |
|
|
| return(0); |
| } |
| |
| int file_seek(int handle, LONGLONG offset) |
| |
| |
| |
| { |
|
|
| #if defined(_MSC_VER) && (_MSC_VER >= 1400) |
| |
| |
| |
| |
| if (_fseeki64(handleTable[handle].fileptr, (OFF_T) offset, 0) != 0) |
| return(SEEK_ERROR); |
| |
| #elif _FILE_OFFSET_BITS - 0 == 64 |
|
|
| if (fseeko(handleTable[handle].fileptr, (OFF_T) offset, 0) != 0) |
| return(SEEK_ERROR); |
|
|
| #else |
|
|
| if (fseek(handleTable[handle].fileptr, (OFF_T) offset, 0) != 0) |
| return(SEEK_ERROR); |
|
|
| #endif |
|
|
| handleTable[handle].currentpos = offset; |
| return(0); |
| } |
| |
| int file_read(int hdl, void *buffer, long nbytes) |
| |
| |
| |
| { |
| long nread; |
| char *cptr; |
|
|
| if (handleTable[hdl].last_io_op == IO_WRITE) |
| { |
| if (file_seek(hdl, handleTable[hdl].currentpos)) |
| return(SEEK_ERROR); |
| } |
| |
| nread = (long) fread(buffer, 1, nbytes, handleTable[hdl].fileptr); |
|
|
| if (nread == 1) |
| { |
| cptr = (char *) buffer; |
|
|
| |
| |
| if (*cptr == 0 || *cptr == 10 || *cptr == 32) |
| return(END_OF_FILE); |
| else |
| return(READ_ERROR); |
| } |
| else if (nread != nbytes) |
| { |
| return(READ_ERROR); |
| } |
|
|
| handleTable[hdl].currentpos += nbytes; |
| handleTable[hdl].last_io_op = IO_READ; |
| return(0); |
| } |
| |
| int file_write(int hdl, void *buffer, long nbytes) |
| |
| |
| |
| { |
| if (handleTable[hdl].last_io_op == IO_READ) |
| { |
| if (file_seek(hdl, handleTable[hdl].currentpos)) |
| return(SEEK_ERROR); |
| } |
|
|
| if((long) fwrite(buffer, 1, nbytes, handleTable[hdl].fileptr) != nbytes) |
| return(WRITE_ERROR); |
|
|
| handleTable[hdl].currentpos += nbytes; |
| handleTable[hdl].last_io_op = IO_WRITE; |
| return(0); |
| } |
| |
| int file_compress_open(char *filename, int rwmode, int *hdl) |
| |
| |
| |
| |
| |
| |
| |
| { |
| FILE *indiskfile, *outdiskfile; |
| int status; |
| char *cptr; |
|
|
| |
| status = file_openfile(filename, READONLY, &indiskfile); |
| if (status) |
| { |
| ffpmsg("failed to open compressed disk file (file_compress_open)"); |
| ffpmsg(filename); |
| return(status); |
| } |
|
|
| |
| |
|
|
| cptr = file_outfile; |
| if (*cptr == '!') |
| { |
| |
| cptr++; |
| remove(cptr); |
| } |
| else |
| { |
| outdiskfile = fopen(file_outfile, "r"); |
|
|
| if (outdiskfile) |
| { |
| ffpmsg("uncompressed file already exists: (file_compress_open)"); |
| ffpmsg(file_outfile); |
| fclose(outdiskfile); |
| file_outfile[0] = '\0'; |
| return(FILE_NOT_CREATED); |
| } |
| } |
|
|
| outdiskfile = fopen(cptr, "w+b"); |
| if (!outdiskfile) |
| { |
| ffpmsg("could not create uncompressed file: (file_compress_open)"); |
| ffpmsg(file_outfile); |
| file_outfile[0] = '\0'; |
| return(FILE_NOT_CREATED); |
| } |
|
|
| |
| uncompress2file(filename, indiskfile, outdiskfile, &status); |
| fclose(indiskfile); |
| fclose(outdiskfile); |
|
|
| if (status) |
| { |
| ffpmsg("error in file_compress_open: failed to uncompressed file:"); |
| ffpmsg(filename); |
| ffpmsg(" into new output file:"); |
| ffpmsg(file_outfile); |
| file_outfile[0] = '\0'; |
| return(status); |
| } |
|
|
| strcpy(filename, cptr); |
| file_outfile[0] = '\0'; |
|
|
| status = file_open(filename, rwmode, hdl); |
|
|
| return(status); |
| } |
| |
| int file_is_compressed(char *filename) |
| |
| |
| |
| |
| { |
| FILE *diskfile; |
| unsigned char buffer[2]; |
| char tmpfilename[FLEN_FILENAME]; |
|
|
| |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| if (strlen(filename) > FLEN_FILENAME - 5) |
| return(0); |
|
|
| strcpy(tmpfilename,filename); |
| strcat(filename,".gz"); |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| #if HAVE_BZIP2 |
| strcpy(filename,tmpfilename); |
| strcat(filename,".bz2"); |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| #endif |
| strcpy(filename, tmpfilename); |
| strcat(filename,".Z"); |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| strcpy(filename, tmpfilename); |
| strcat(filename,".z"); |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| strcpy(filename, tmpfilename); |
| strcat(filename,".zip"); |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| strcpy(filename, tmpfilename); |
| strcat(filename,"-z"); |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| strcpy(filename, tmpfilename); |
| strcat(filename,"-gz"); |
| if (file_openfile(filename, 0, &diskfile)) |
| { |
| strcpy(filename,tmpfilename); |
| return(0); |
| } |
| } |
| } |
| } |
| } |
| #if HAVE_BZIP2 |
| } |
| #endif |
| } |
| } |
|
|
| if (fread(buffer, 1, 2, diskfile) != 2) |
| { |
| fclose(diskfile); |
| return(0); |
| } |
|
|
| fclose(diskfile); |
|
|
| |
| if ( (memcmp(buffer, "\037\213", 2) == 0) || |
| (memcmp(buffer, "\120\113", 2) == 0) || |
| (memcmp(buffer, "\037\036", 2) == 0) || |
| (memcmp(buffer, "\037\235", 2) == 0) || |
| #if HAVE_BZIP2 |
| (memcmp(buffer, "BZ", 2) == 0) || |
| #endif |
| (memcmp(buffer, "\037\240", 2) == 0)) |
| { |
| return(1); |
| } |
| else |
| { |
| return(0); |
| } |
| } |
| |
| int file_checkfile (char *urltype, char *infile, char *outfile) |
| { |
| |
| if ( file_is_compressed(infile) ) |
| { |
| |
| |
| if (strlen(outfile)) |
| { |
| if (!strncmp(outfile, "mem:", 4) ) |
| { |
| |
| strcpy(urltype, "compressmem://"); |
| *file_outfile = '\0'; |
| } |
| else |
| { |
| strcpy(urltype, "compressfile://"); |
|
|
| |
| if (!strncmp(outfile, "file://", 7) ) |
| strcpy(file_outfile,outfile+7); |
| else |
| strcpy(file_outfile,outfile); |
| } |
| } |
| else |
| { |
| |
| strcpy(urltype, "compress://"); |
| *file_outfile = '\0'; |
| } |
| } |
| else |
| { |
| |
| |
| |
| |
| |
| if (strlen(outfile)) { |
| file_outfile[0] = '\0'; |
| strncat(file_outfile,outfile,FLEN_FILENAME-1); |
| } |
| } |
|
|
| return 0; |
| } |
| |
| |
| |
|
|
| |
|
|
|
|
| |
| int stream_open(char *filename, int rwmode, int *handle) |
| { |
| |
| |
| |
| if (filename) |
| rwmode = 1; |
|
|
| *handle = 1; |
|
|
| return(0); |
| } |
| |
| int stream_create(char *filename, int *handle) |
| { |
| |
| |
| |
|
|
| if (filename) |
| *handle = 2; |
| else |
| *handle = 2; |
|
|
| return(0); |
| } |
| |
| int stream_size(int handle, LONGLONG *filesize) |
| |
| |
| |
| { |
| handle = 0; |
| |
| |
| *filesize = LONG_MAX; |
| return(0); |
| } |
| |
| int stream_close(int handle) |
| |
| |
| |
| { |
| handle = 0; |
| |
| return(0); |
| } |
| |
| int stream_flush(int handle) |
| |
| |
| |
| { |
| if (handle == 2) |
| fflush(stdout); |
|
|
| return(0); |
| } |
| |
| int stream_seek(int handle, LONGLONG offset) |
| |
| |
| |
| { |
| offset = handle; |
| return(1); |
| } |
| |
| int stream_read(int hdl, void *buffer, long nbytes) |
| |
| |
| |
|
|
| { |
| long nread; |
| |
| if (hdl != 1) |
| return(1); |
|
|
| nread = (long) fread(buffer, 1, nbytes, stdin); |
|
|
| if (nread != nbytes) |
| { |
| |
| return(END_OF_FILE); |
| } |
|
|
| return(0); |
| } |
| |
| int stream_write(int hdl, void *buffer, long nbytes) |
| |
| |
| |
| { |
| if (hdl != 2) |
| return(1); |
|
|
| if((long) fwrite(buffer, 1, nbytes, stdout) != nbytes) |
| return(WRITE_ERROR); |
|
|
| return(0); |
| } |
|
|
|
|
|
|
|
|
|
|