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

ss_testread.c

Go to the documentation of this file.
00001 /* Test for securestorage_read 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 extern int errno;
00017 
00018 /* MAIN */
00019 int main (int argc, char *argv[]) {
00020         
00021         int fd;
00022         unsigned char *buffer = (unsigned char *) calloc(1, BUFLEN+1);
00023         int read_byte=0;
00024         
00025         if (argc != 3) {
00026                 fprintf(stderr,"Usage: %s <lfn> <destfile>\n",argv[0]);
00027                 free(buffer);
00028                 exit(1);
00029         }
00030         
00031         FILE *fdout;
00032         if ((fdout = fopen(argv[2], "rb"))) {
00033                 fprintf(stderr, "Fatal error: file \"%s\" exist or you don't have write permissions.\n", argv[2]);
00034                 free(buffer);
00035                 exit(1);
00036         }
00037         if ( (fdout = fopen(argv[2], "wb")) == NULL ) {
00038                 perror("fopen");
00039                 free(buffer);
00040                 exit(1);
00041         }
00042         
00043         if ( (fd=securestorage_open(argv[1], O_RDONLY, 0644)) < 0 ) {
00044                 int errno_save = errno;
00045                 fprintf(stderr, "securestorage_open: %s\n", securestorage_error(errno_save));
00046                 fclose(fdout);
00047                 // Delete dest file
00048                 if (remove(argv[2]) < 0 ) {
00049                         perror ("remove");
00050         }
00051         free(buffer);
00052                 exit(1);
00053         }
00054         
00055         do {
00056                 if ( (read_byte=securestorage_read ( fd, buffer, BUFLEN )) < 0 ) {
00057                         int errno_save = errno;
00058                         fprintf(stderr, "securestorage_read: %s\n", securestorage_error(errno_save));
00059                         fclose(fdout);
00060                         // Delete dest file
00061                         if (remove(argv[2]) < 0 ) {
00062                                 perror ("remove");
00063                 }
00064                 free(buffer);
00065                         exit(1);
00066                 }
00067                 if (read_byte>0) {
00068                         if (fwrite(buffer, 1, read_byte, fdout) < 0) {
00069                                 perror ("fwrite");
00070                     fclose(fdout);
00071                     // Delete dest file
00072                     if (remove(argv[2]) < 0 ) {
00073                         perror ("remove");
00074                     }
00075                     free(buffer);
00076                                 exit(1);
00077                         }
00078                 }
00079         } while (read_byte>0);
00080         
00081         
00082         if ( securestorage_close (fd) < 0 ) {
00083                 int errno_save = errno;
00084                 fprintf(stderr, "securestorage_close: %s\n", securestorage_error(errno_save));
00085                 fclose(fdout);
00086                 free(buffer);
00087                 exit(1);
00088         }
00089         fclose(fdout);
00090         free(buffer);
00091         exit(0);
00092 }

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