Main Page | Data Structures | File List | Data Fields | Globals

ss_testseek.c

Go to the documentation of this file.
00001 /* Test for securestorage_seek function */
00002 #include <errno.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <stdlib.h>
00006 #include <fcntl.h>
00007 #if defined(_WIN32)
00008 #include <io.h>
00009 #else
00010 #include <unistd.h>
00011 #endif
00012 #include "../src/securestorage.h"
00013 
00014 #define BUFLEN 65536
00015 
00016 /* MAIN */
00017 int main (int argc, char *argv[]) {
00018         
00019         int fd;
00020         unsigned char *buffer = (unsigned char *) calloc(1, BUFLEN+1);
00021         int read_byte=0;
00022         off_t remote_file_position;
00023         off_t offset;
00024         int whence;
00025         
00026         if (argc != 5) {
00027                 fprintf(stderr,"Usage: %s <lfn> <destfile> <offset> <whence>\n",argv[0]);
00028                 free(buffer);
00029                 exit(1);
00030         }
00031         
00032         offset = atoi(argv[3]);
00033         if (!strncmp(argv[4], "SEEK_SET", 8)) {
00034                         whence = SEEK_SET;
00035         } else if (!strncmp(argv[4], "SEEK_CUR", 8)) {
00036                         whence = SEEK_CUR;
00037         } else if (!strncmp(argv[4], "SEEK_END", 8)) {
00038                         whence = SEEK_END;
00039         } else {
00040                 fprintf(stderr,"Invalid whence specified: only SEEK_SET, SEEK_CUR and SEEK_END are allowed.\n");
00041                 free(buffer);
00042                 exit(1);
00043         }
00044         
00045         FILE *fdout;
00046         if ((fdout = fopen(argv[2], "rb"))) {
00047                 fprintf(stderr, "Fatal error: file \"%s\" exist or you don't have write permissions.\n", argv[2]);
00048                 free(buffer);
00049                 exit(1);
00050         }
00051         if ( (fdout = fopen(argv[2], "wb")) == NULL ) {
00052                 perror("fopen");
00053                 free(buffer);
00054                 exit(1);
00055         }
00056         
00057         if ( (fd=securestorage_open(argv[1], O_RDONLY, 0644)) < 0 ) {
00058                 int errno_save = errno;
00059                 fprintf(stderr, "securestorage_open: %s\n", securestorage_error(errno_save));
00060                 fclose(fdout);
00061                 // Delete dest file
00062                 if (remove(argv[2]) < 0 ) {
00063                         perror ("remove");
00064         }
00065         free(buffer);
00066                 exit(1);
00067         }
00068         
00069         if ((remote_file_position = securestorage_lseek (fd, offset, whence)) < 0 ) {
00070                 int errno_save = errno;
00071                 fprintf(stderr, "securestorage_lseek: %s\n", securestorage_error(errno_save));
00072                 fclose(fdout);
00073                 // Delete dest file
00074                 if (remove(argv[2]) < 0 ) {
00075                         perror ("remove");
00076         }
00077         free(buffer);
00078         securestorage_close (fd);
00079                 exit(1);
00080         }
00081         
00082         do {
00083                 if ( (read_byte=securestorage_read ( fd, buffer, BUFLEN )) < 0 ) {
00084                         int errno_save = errno;
00085                         fprintf(stderr, "securestorage_read: %s\n", securestorage_error(errno_save));
00086                         fclose(fdout);
00087                         // Delete dest file
00088                         if (remove(argv[2]) < 0 ) {
00089                                 perror ("remove");
00090                 }
00091                 free(buffer);
00092                 securestorage_close (fd);
00093                         exit(1);
00094                 }
00095                 if (read_byte>0) {
00096                         if (fwrite(buffer, 1, read_byte, fdout) < 0) {
00097                                 perror ("fwrite");
00098                     fclose(fdout);
00099                     // Delete dest file
00100                     if (remove(argv[2]) < 0 ) {
00101                         perror ("remove");
00102                     }
00103                     free(buffer);
00104                     securestorage_close (fd);
00105                                 exit(1);
00106                         }
00107                 }
00108         } while (read_byte>0);
00109         
00110         
00111         if ( securestorage_close (fd) < 0 ) {
00112                 int errno_save = errno;
00113                 fprintf(stderr, "securestorage_close: %s\n", securestorage_error(errno_save));
00114                 fclose(fdout);
00115                 free(buffer);
00116                 exit(1);
00117         }
00118         fclose(fdout);
00119         free(buffer);
00120         exit(0);
00121 }

Generated on Wed May 30 09:56:31 2007 for Secure Storage Service by doxygen 1.3.5