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 off_t remote_file_position;
00027 off_t offset;
00028 int whence;
00029
00030 if (argc != 5) {
00031 fprintf(stderr,"Usage: %s <sourcefile> <lfn> <offset> <whence>\n",argv[0]);
00032 free(buffer);
00033 exit(1);
00034 }
00035
00036 offset = atoi(argv[3]);
00037 if (!strncmp(argv[4], "SEEK_SET", 8)) {
00038 whence = SEEK_SET;
00039 } else if (!strncmp(argv[4], "SEEK_CUR", 8)) {
00040 whence = SEEK_CUR;
00041 } else if (!strncmp(argv[4], "SEEK_END", 8)) {
00042 whence = SEEK_END;
00043 } else {
00044 fprintf(stderr,"Invalid whence specified: only SEEK_SET, SEEK_CUR and SEEK_END are allowed.\n");
00045 free(buffer);
00046 exit(1);
00047 }
00048
00049
00050 if ( (fdin = fopen(argv[1], "rb")) == NULL ) {
00051 fprintf(stderr, "Fatal error: file \"%s\" does not exist or you don't have read permissions.\n",argv[1]);
00052 free(buffer);
00053 exit(1);
00054 }
00055
00056
00057 if ( (fd=securestorage_open( argv[2], O_WRONLY|O_CREAT, 0644)) < 0 ) {
00058 int errno_save = errno;
00059 fprintf(stderr, "securestorage_open: %s\n", securestorage_error(errno_save));
00060 fclose(fdin); free(buffer);
00061 exit(1);
00062 }
00063
00064 do {
00065
00066 memset(buffer, 0, BUFLEN+1);
00067 nread = fread(buffer, 1, BUFLEN, fdin);
00068 if (nread < 0) {
00069 perror ("fread");
00070 fclose(fdin); securestorage_close (fd);
00071 free(buffer);
00072 exit(1);
00073 }
00074 else if (nread > 0) {
00075 if ( (write_byte=securestorage_write ( fd, buffer, nread )) < 0 ) {
00076 int errno_save = errno;
00077 fprintf(stderr, "securestorage_write: %s\n", securestorage_error(errno_save));
00078 fclose(fdin); securestorage_close (fd);
00079 free(buffer);
00080 exit(1);
00081 }
00082 }
00083 } while (nread>0);
00084
00085 if ((remote_file_position = securestorage_lseek (fd, 0, SEEK_SET)) < 0 ) {
00086 int errno_save = errno;
00087 fprintf(stderr, "securestorage_lseek: %s\n", securestorage_error(errno_save));
00088 fclose(fdin); securestorage_close (fd);
00089 free(buffer);
00090 exit(1);
00091 }
00092 if ((remote_file_position = securestorage_lseek (fd, offset, whence)) < 0 ) {
00093 int errno_save = errno;
00094 fprintf(stderr, "securestorage_lseek: %s\n", securestorage_error(errno_save));
00095 fclose(fdin); securestorage_close (fd);
00096 free(buffer);
00097 exit(1);
00098 }
00099 if ( (write_byte=securestorage_write ( fd, "prova", 5 )) < 0 ) {
00100 int errno_save = errno;
00101 fprintf(stderr, "securestorage_write: %s\n", securestorage_error(errno_save));
00102 fclose(fdin); securestorage_close (fd);
00103 free(buffer);
00104 exit(1);
00105 }
00106
00107 if ( securestorage_close (fd) < 0 ) {
00108 int errno_save = errno;
00109 fprintf(stderr, "securestorage_close: %s\n", securestorage_error(errno_save));
00110 fclose(fdin); free(buffer);
00111 exit(1);
00112 }
00113 fclose(fdin);
00114 free(buffer);
00115
00116 exit (0);
00117 }