00001
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
00019 int main (int argc, char *argv[]) {
00020
00021 int fd;
00022 unsigned char *buffer = (unsigned char *) calloc(1, BUFLEN+1);
00023 int write_byte=0;
00024 int nread=0;
00025 FILE *fdin;
00026 if (argc != 3) {
00027 fprintf(stderr,"Usage: %s <sourcefile> <lfn>\n",argv[0]);
00028 free(buffer);
00029 exit(1);
00030 }
00031
00032 if ( (fdin = fopen(argv[1], "rb")) == NULL ) {
00033 fprintf(stderr, "Fatal error: file \"%s\" does not exist or you don't have read permissions.\n",argv[1]);
00034 free(buffer);
00035 exit(1);
00036 }
00037
00038
00039 if ( (fd=securestorage_open( argv[2], O_WRONLY|O_CREAT, 0644)) < 0 ) {
00040 int errno_save = errno;
00041 fprintf(stderr, "securestorage_open: %s\n", securestorage_error(errno_save));
00042 fclose(fdin); free(buffer);
00043 exit(1);
00044 }
00045
00046 do {
00047
00048 memset(buffer, 0, BUFLEN+1);
00049 nread = fread(buffer, 1, BUFLEN, fdin);
00050 if (nread < 0) {
00051 perror ("fread");
00052 fclose(fdin); securestorage_close (fd);
00053 free(buffer);
00054 exit(1);
00055 }
00056 else if (nread > 0) {
00057 if ( (write_byte=securestorage_write ( fd, buffer, nread )) < 0 ) {
00058 int errno_save = errno;
00059 fprintf(stderr, "securestorage_write: %s\n", securestorage_error(errno_save));
00060 fclose(fdin); securestorage_close (fd);
00061 free(buffer);
00062 exit(1);
00063 }
00064 }
00065 } while (nread>0);
00066
00067 if ( securestorage_close (fd) < 0 ) {
00068 int errno_save = errno;
00069 fprintf(stderr, "securestorage_close: %s\n", securestorage_error(errno_save));
00070 fclose(fdin);
00071 free(buffer);
00072 exit(1);
00073 }
00074 fclose(fdin);
00075 free(buffer);
00076
00077 exit (0);
00078 }