sfat_cp.c

Vai alla documentazione di questo file.
00001 
00013 #include "Include/common.h"
00014 
00016 static void byebye();
00017 
00018 message_t       msg;                            
00019 channel_t       sk_client;                      
00020 char            *path;                          
00021 char            *dati_letti=NULL;       
00023 #define LEN     128     
00035 int main(int argc, char **argv){
00036         extern message_t msg;
00037         extern channel_t sk_client;
00038         extern char *path;
00039         extern char *dati_letti;
00040         int dest_len=0, source_len=0, offset=0, len=LEN, letti=0;
00041         char *buffer=NULL;
00042         
00043         sk_client = 0;
00044         path = NULL;
00045         msg.buffer = NULL;
00046         
00047         if ((atexit(byebye))!=0){
00048         errore(__FILE__,__LINE__,"sfat_cp: cannot set exit function",errno);
00049         exit(EXIT_FAILURE);
00050         }
00051         
00052         /* Argomenti da riga di comando */
00053         if (argc<3){
00054                 fprintf(stderr,"sfat_cp: errore argomenti insufficenti\n");
00055                 fprintf(stderr,"./sfat_cp file_src file_dest\n");
00056                 exit(EXIT_FAILURE);
00057         }       
00058         
00059         if (!(path = calloc((strlen(TMP) + strlen(SKTNAME) + 1), sizeof(char)))){
00060                 errore(__FILE__,__LINE__,
00061                 "sfat_cp: error on allocate memory for 'path'",errno);
00062                 exit(EXIT_FAILURE);
00063         }
00064         strncpy(path, TMP, strlen(TMP) + 1);
00065         strncat(path, SKTNAME, strlen(SKTNAME));
00066 
00067         /* Apro la socket di comunicazione col server */
00068         if ((sk_client = openConnection(path)) == -1) {
00069                 fprintf(stderr,"sfat_cp: Errore nella apertura del socket di comunicazione\n");
00070                 exit(EXIT_FAILURE);
00071         }
00072         else if (sk_client == SFATENAMETOOLONG){
00073                 fprintf(stderr,"sfat_cp: Error Path Too Long (exceeding UNIX_PATH_MAX)\n");     
00074                 exit(EXIT_FAILURE);
00075         }
00076 
00077         dest_len = strlen(argv[2]) + 1;
00078         source_len = strlen(argv[1]) + 1;
00079 
00080         /* Creo il file */
00081         msg.type = MSG_MKFILE;
00082         msg.length = dest_len;
00083         if (!(msg.buffer = strdup(argv[2]))){
00084                 errore(__FILE__,__LINE__,
00085                 "sfat_cp: error on allocate memory for 'buffer'",errno);
00086                 exit(EXIT_FAILURE);
00087         }
00088         if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE);
00089         if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE);
00090         if (msg.type==MSG_ERROR) exit(EXIT_FAILURE);
00091         
00092         while(1){
00093                 /* Leggo il file da copiare */
00094                 msg.type = MSG_FREAD;   
00095                 msg.length = 2*sizeof(int) + source_len*sizeof(char) + 2;
00096                 if (!(msg.buffer = malloc(msg.length))){
00097                         errore(__FILE__,__LINE__,
00098                         "sfat_cp: error on allocate memory for 'buffer'",errno);
00099                         exit(EXIT_FAILURE);
00100                 }
00101                 memset(msg.buffer, 0x0, msg.length);
00102                 /* Struttura del messaggio: Offset\0Path\0Len\0 */
00103                 memcpy(msg.buffer, &offset, sizeof(int));
00104                 buffer = msg.buffer + sizeof(int) + 1;
00105                 strncpy(buffer, argv[1], source_len);
00106                 buffer += source_len;
00107                 memcpy(buffer, &len, sizeof(int));
00108                 
00109                 /* Mando la richiesta di lettura al server */
00110                 if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE);
00111                 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE);
00112                 if (msg.type==MSG_ERROR) exit(EXIT_FAILURE);
00113                 
00114                 /* Mi faccio una copia dei dati letti */
00115                 if (!(dati_letti = strdup(msg.buffer))){
00116                         errore(__FILE__,__LINE__,
00117                         "sfat_cp: error on allocate memory for 'buffer'",errno);
00118                         exit(EXIT_FAILURE);             
00119                 }
00120                 
00121                 /* Preparo il messaggio da inviare al server */
00122                 msg.type = MSG_FWRITE;
00123                 msg.length = msg.length + strlen(argv[2]) + 2;
00124                 if (msg.buffer) free(msg.buffer);
00125                 if (!(msg.buffer = calloc(msg.length, sizeof(char)))){
00126                         errore(__FILE__,__LINE__,
00127                         "sfat_cp: error on allocate memory for 'buffer'",errno);
00128                         exit(EXIT_FAILURE);
00129                 }
00130                 /* Struttura del messaggio: Path\0Buf\0 */
00131                 strncpy(msg.buffer, argv[2], strlen(argv[2]));
00132                 buffer = msg.buffer + dest_len;
00133                 strncpy(buffer, dati_letti, strlen(dati_letti));
00134                 letti = strlen(dati_letti);
00135                 if (dati_letti){
00136                         free(dati_letti);
00137                         dati_letti = NULL;
00138                 }
00139                 /* Mando la richiesta di scrittura al server */
00140                 if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE);
00141                 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE);
00142                 if (msg.type==MSG_ERROR) exit(EXIT_FAILURE);
00143                 
00144                 if (letti<len) break;
00145                 else offset += len;             
00146         }
00147         if (msg.type==MSG_OK) printf("sfat_cp: %s written \n", argv[2]);
00148         else exit(EXIT_FAILURE);
00149         
00150         exit(EXIT_SUCCESS);
00151         return 0;     
00152 }
00153 
00154 void byebye(){
00155         extern message_t msg;
00156         extern char * path;
00157         extern char *dati_letti;
00158         extern channel_t sk_client;
00159         
00160         /* Chiudo la socket di comunicazione del client */
00161         if (sk_client != -1) closeConnection(sk_client);
00162                         
00163         if (msg.buffer!=NULL) free(msg.buffer);
00164         if (dati_letti) free(dati_letti);
00165         if (path!=NULL) free(path);
00166 }
Generato il Fri Jan 28 22:16:29 2011 per SFAT: Simplified File Allocation Table Project da  doxygen 1.6.3