#include "Include/common.h"Vai al codice sorgente di questo file.
Definizioni | |
| #define | LEN 128 |
Funzioni | |
| static void | byebye () |
| int | main (int argc, char **argv) |
Variabili | |
| message_t | msg |
| channel_t | sk_client |
| char * | path |
| char * | dati_letti = NULL |
This program is free software; you can redistribuite it and/or modify it under the terms of the GNU/General Pubblic License as published the Free software Foundation; either version 2 of the License, or (at your opinion) any later version.
Definizione nel file sfat_cp.c.
| #define LEN 128 |
| void byebye | ( | ) | [static] |
exit function
Definizione alla linea 155 del file sfat_cp.c.
00155 { 00156 extern message_t msg; 00157 extern char * path; 00158 extern char *dati_letti; 00159 extern channel_t sk_client; 00160 00161 /* Chiudo la socket di comunicazione del client */ 00162 if (sk_client != -1) closeConnection(sk_client); 00163 00164 if (msg.buffer!=NULL) free(msg.buffer); 00165 if (dati_letti) free(dati_letti); 00166 if (path!=NULL) free(path); 00167 }
| int main | ( | int | argc, | |
| char ** | argv | |||
| ) |
Comando: sfat_cp file1 file2
Crea il file file2 e vi ricopia il contenuto de file file2.
All'invocazione il client controlla i parametri e contatta il server inviando una richiesta di connessione sul socket.
Se la connessione ha successo il client invia le opportune richieste di creazione, lettura e scrittura per portare a termine la copia.
Tutti gli errori sono riportati sullo standar error.
Definizione alla linea 36 del file sfat_cp.c.
00036 { 00037 extern message_t msg; 00038 extern channel_t sk_client; 00039 extern char *path; 00040 extern char *dati_letti; 00041 int dest_len=0, source_len=0, offset=0, len=LEN, letti=0; 00042 char *buffer=NULL; 00043 00044 sk_client = 0; 00045 path = NULL; 00046 msg.buffer = NULL; 00047 00048 if ((atexit(byebye))!=0){ 00049 errore(__FILE__,__LINE__,"sfat_cp: cannot set exit function",errno); 00050 exit(EXIT_FAILURE); 00051 } 00052 00053 /* Argomenti da riga di comando */ 00054 if (argc<3){ 00055 fprintf(stderr,"sfat_cp: errore argomenti insufficenti\n"); 00056 fprintf(stderr,"./sfat_cp file_src file_dest\n"); 00057 exit(EXIT_FAILURE); 00058 } 00059 00060 if (!(path = calloc((strlen(TMP) + strlen(SKTNAME) + 1), sizeof(char)))){ 00061 errore(__FILE__,__LINE__, 00062 "sfat_cp: error on allocate memory for 'path'",errno); 00063 exit(EXIT_FAILURE); 00064 } 00065 strncpy(path, TMP, strlen(TMP) + 1); 00066 strncat(path, SKTNAME, strlen(SKTNAME)); 00067 00068 /* Apro la socket di comunicazione col server */ 00069 if ((sk_client = openConnection(path)) == -1) { 00070 fprintf(stderr,"sfat_cp: Errore nella apertura del socket di comunicazione\n"); 00071 exit(EXIT_FAILURE); 00072 } 00073 else if (sk_client == SFATENAMETOOLONG){ 00074 fprintf(stderr,"sfat_cp: Error Path Too Long (exceeding UNIX_PATH_MAX)\n"); 00075 exit(EXIT_FAILURE); 00076 } 00077 00078 dest_len = strlen(argv[2]) + 1; 00079 source_len = strlen(argv[1]) + 1; 00080 00081 /* Creo il file */ 00082 msg.type = MSG_MKFILE; 00083 msg.length = dest_len; 00084 if (!(msg.buffer = strdup(argv[2]))){ 00085 errore(__FILE__,__LINE__, 00086 "sfat_cp: error on allocate memory for 'buffer'",errno); 00087 exit(EXIT_FAILURE); 00088 } 00089 if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00090 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00091 if (msg.type==MSG_ERROR) exit(EXIT_FAILURE); 00092 00093 while(1){ 00094 /* Leggo il file da copiare */ 00095 msg.type = MSG_FREAD; 00096 msg.length = 2*sizeof(int) + source_len*sizeof(char) + 2; 00097 if (!(msg.buffer = malloc(msg.length))){ 00098 errore(__FILE__,__LINE__, 00099 "sfat_cp: error on allocate memory for 'buffer'",errno); 00100 exit(EXIT_FAILURE); 00101 } 00102 memset(msg.buffer, 0x0, msg.length); 00103 /* Struttura del messaggio: Offset\0Path\0Len\0 */ 00104 memcpy(msg.buffer, &offset, sizeof(int)); 00105 buffer = msg.buffer + sizeof(int) + 1; 00106 strncpy(buffer, argv[1], source_len); 00107 buffer += source_len; 00108 memcpy(buffer, &len, sizeof(int)); 00109 00110 /* Mando la richiesta di lettura al server */ 00111 if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00112 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00113 if (msg.type==MSG_ERROR) exit(EXIT_FAILURE); 00114 00115 /* Mi faccio una copia dei dati letti */ 00116 if (!(dati_letti = strdup(msg.buffer))){ 00117 errore(__FILE__,__LINE__, 00118 "sfat_cp: error on allocate memory for 'buffer'",errno); 00119 exit(EXIT_FAILURE); 00120 } 00121 00122 /* Preparo il messaggio da inviare al server */ 00123 msg.type = MSG_FWRITE; 00124 msg.length = msg.length + strlen(argv[2]) + 2; 00125 if (msg.buffer) free(msg.buffer); 00126 if (!(msg.buffer = calloc(msg.length, sizeof(char)))){ 00127 errore(__FILE__,__LINE__, 00128 "sfat_cp: error on allocate memory for 'buffer'",errno); 00129 exit(EXIT_FAILURE); 00130 } 00131 /* Struttura del messaggio: Path\0Buf\0 */ 00132 strncpy(msg.buffer, argv[2], strlen(argv[2])); 00133 buffer = msg.buffer + dest_len; 00134 strncpy(buffer, dati_letti, strlen(dati_letti)); 00135 letti = strlen(dati_letti); 00136 if (dati_letti){ 00137 free(dati_letti); 00138 dati_letti = NULL; 00139 } 00140 /* Mando la richiesta di scrittura al server */ 00141 if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00142 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00143 if (msg.type==MSG_ERROR) exit(EXIT_FAILURE); 00144 00145 if (letti<len) break; 00146 else offset += len; 00147 } 00148 if (msg.type==MSG_OK) printf("sfat_cp: %s written \n", argv[2]); 00149 else exit(EXIT_FAILURE); 00150 00151 exit(EXIT_SUCCESS); 00152 return 0; 00153 }
| char* dati_letti = NULL |
1.6.3