/* Read the content of the directory, make an array of absolute paths for * all recognized files. Put directories, playlists and sound files * in proper structures. Return 0 on error.*/ int read_directory (const char *directory, struct file_list *dirs, struct file_list *playlists, struct plist *plist) { DIR *dir; struct dirent *entry; int show_hidden = options_get_int ("ShowHiddenFiles"); int dir_is_root; assert (directory != NULL); assert (*directory == '/'); assert (dirs != NULL); assert (playlists != NULL); assert (plist != NULL); if (!(dir = opendir(directory))) { error ("Can't read directory: %s", strerror(errno)); return 0; } if (!strcmp(directory, "/")) dir_is_root = 1; else dir_is_root = 0; while ((entry = readdir(dir))) { char file[PATH_MAX]; enum file_type type; if (user_wants_interrupt()) { error ("Interrupted! Not all files read!"); break; } if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue; if (!show_hidden && entry->d_name[0] == '.') continue; if (snprintf(file, sizeof(file), "%s/%s", dir_is_root ? "" : directory, entry->d_name) >= (int)sizeof(file)) { error ("Path too long!"); return 0; } type = file_type (file); if (type == F_SOUND) plist_add (plist, file); else if (type == F_DIR) file_list_add (dirs, file); else if (type == F_PLAYLIST) file_list_add (playlists, file); } closedir (dir); return 1; }
File_t *files_from_state_file(const char *state_file_path) { FILE *f = fopen(state_file_path, "r"); if (f == NULL) { Error("ListFromStateFile: %s", state_file_path); } char path[PATH_MAX] = { 0 }; File_t *list = calloc(1, sizeof(File_t)); if (list == NULL) { Error("ListFromStateFile: calloc"); } char line[1024] = { 0 }; while ((fgets(line, sizeof(line), f)) != NULL) { chomp(line); int mtime, size, mode; bool status = get_list_from_state_file(line, path, &size, &mode, &mtime); if (status) { file_list_add(list, path, size, mode, mtime); } } fclose(f); return list; }
bool file_list_prepend(file_list_t *list, const char *path, const char *label, unsigned type, size_t directory_ptr, size_t entry_idx) { unsigned i; if (!file_list_expand_if_needed(list)) return false; for (i = (unsigned)list->size; i > 0; i--) { struct item_file *copy = (struct item_file*) calloc(1, sizeof(struct item_file)); memcpy(copy, &list->list[i-1], sizeof(struct item_file)); memcpy(&list->list[i-1], &list->list[i], sizeof(struct item_file)); memcpy(&list->list[i], copy, sizeof(struct item_file)); free(copy); } file_list_add(list, 0, path, label, type, directory_ptr, entry_idx); return true; }
File_t *files_in_directory(const char *path) { DIR *d = NULL; struct dirent *dirent = NULL; d = opendir(path); if (d == NULL) { Error("opendir()"); } File_t *list = calloc(1, sizeof(File_t)); if (list == NULL) { Error("calloc()"); } while ((dirent = readdir(d)) != NULL) { if (!strncmp(dirent->d_name, ".", 1)) { continue; } unsigned int unix_time_now = time(NULL); char path_full[PATH_MAX] = { 0 }; snprintf(path_full, PATH_MAX, "%s%c%s", path, SLASH, dirent->d_name); struct stat fs; stat(path_full, &fs); if (S_ISDIR(fs.st_mode)) { if (fs.st_mtime >= unix_time_now) { continue; // Let directories bloom! } //sleep(zip_sleep_interval); create_tar_file(dirent->d_name); } else { file_list_add(list, path_full, fs.st_size, fs.st_mode, fs.st_mtime); } } closedir(d); return list; }
bool file_list_append(file_list_t *list, const char *path, const char *label, unsigned type, size_t directory_ptr, size_t entry_idx) { if (!file_list_expand_if_needed(list)) return false; file_list_add(list, (unsigned)list->size, path, label, type, directory_ptr, entry_idx); return true; }
/* int main() { return 0; } */ int communicate(struct donnees* donnees) { printf("communicate\n"); struct client* client=donnees->client; ulong ip = client->sockaddr->sin_addr.s_addr; struct peer* peer = find_peer(donnees->peer_list, ip); struct file_list* file_list = donnees->file_list; struct peer_list* peer_list = donnees->peer_list; int refresh_time=get_refresh_time(); printf("refresh time:%d\n",refresh_time); char* recv_buffer= malloc(sizeof(char)*RECV_BUF_SIZE); char* send_buffer= malloc(sizeof(char)*SEND_BUF_SIZE); char* s1 = malloc(RECV_BUF_SIZE*sizeof(char)); char* s2 = malloc(RECV_BUF_SIZE*sizeof(char)); char* s3 = malloc(RECV_BUF_SIZE*sizeof(char)); char* s4 = malloc(RECV_BUF_SIZE*sizeof(char)); char* s5 = malloc(RECV_BUF_SIZE*sizeof(char)); char* s6 = malloc(RECV_BUF_SIZE*sizeof(char)); char* s0 = malloc(RECV_BUF_SIZE*sizeof(char)); char* saux = malloc(RECV_BUF_SIZE*sizeof(char)); char** tab = malloc(7*sizeof(char *)); tab[0]=s0; tab[1]=s1; tab[2]=s2; tab[3]=s3; tab[4]=s4; tab[5]=s5; tab[6]=s6; int port; int read; int decalage; int length; int piece_size; struct timeval *t = malloc(sizeof(struct timeval)); struct timeval *t2 = malloc(sizeof(struct timeval)); struct timezone *tz = malloc(sizeof(struct timezone)); struct timezone *tz2 = malloc(sizeof(struct timezone)); gettimeofday(t,tz); while(1) { recv_buffer[0]='\0'; send_buffer[0]='\0'; gettimeofday(t2,tz2); if(( (int)(t2->tv_sec - t->tv_sec))>3*refresh_time) { if(peer!=NULL) { peer->time--; if(!peer->time) { peer_list_peer_delete(peer_list,file_list,peer->ip_address); } } //free free(s0); free(s1); free(s2); free(s3); free(s4); free(s5); free(s6); free(saux); free(recv_buffer); free(send_buffer); free(tab); free(t); free(t2); free(tz); free(tz2); end(client,donnees->ct); return 0; } read=read_client(client->sock, recv_buffer); if(read > 0) { gettimeofday(t,tz); if(peer!=NULL) peer->time ++; //print_data(file_list, peer_list); printf("received:\n"); switch(recv_buffer[0]) { pthread_mutex_lock(& mutex); case'a': decalage=0; while(compte_crochet_fermant(recv_buffer)<2) { decalage+=read; read=read_client(client->sock, recv_buffer + decalage); } recv_buffer[decalage+read]='\0'; printf("%s\n", recv_buffer); parse(recv_buffer, tab); port = atoi(s2); if(strcmp(s0,"announce")==0 && strcmp(s1,"listen")==0 && strcmp(s3,"seed")==0 && strcmp(s5,"leech")==0) { if(peer == NULL) { peer = peer_init(ip, port); peer_list_add(peer_list, peer); peer->time=1; } int i=0; while(strlen(s4+i) > 0) { if(compte_espace(s4+i)>3) { sscanf(s4+i, "%s %d %d %s", s1, &length, &piece_size, s2); sprintf(saux, "%s %d %d %s", s1, length, piece_size, s2); struct file* file = find_file(file_list, s1); if(NULL == file) { file = remplit_file(s1, length, piece_size, s2); file_list_add(file_list, file); } add_link(file, peer); i+=strlen(saux)+1; } else { sscanf(s4+i, "%s %d %d %s", s1, &length, &piece_size, s2); struct file* file = find_file(file_list, s1); if(NULL == file) { file = remplit_file(s1, length, piece_size, s2); file_list_add(file_list, file); } add_link(file, peer); s4[i]='\0'; } } struct file_list* f_add = keys_string_to_file_list(s6); update_add(file_list, peer, f_add); file_list_delete(f_add); write_client(client->sock, "ok"); printf("replied:ok\n"); } break; case'u': decalage=0; while(compte_crochet_fermant(recv_buffer)<2) { decalage+=read; read=read_client(client->sock, recv_buffer + decalage); } recv_buffer[decalage+read]='\0'; printf("%s\n", recv_buffer); parse(recv_buffer, tab); if(strcmp(s0,"update")==0 && strcmp(s1,"seed")==0 && strcmp(s3,"leech")==0) { if(peer==NULL){ //free free(s0); free(s1); free(s2); free(s3); free(s4); free(s5); free(s6); free(saux); free(recv_buffer); free(send_buffer); free(tab); free(t); free(t2); free(tz); free(tz2); end(client, donnees->ct); return 0;}//ferme la socket else { char* res=fusion_keys_string(s2, s4); struct file_list* f = keys_string_to_file_list(res); update_diff(f, peer->file_list, file_list, peer); file_list_delete(f); free(res); write_client(client->sock, "ok"); printf("replied:ok\n"); } } break; case'l': decalage=0; while(compte_crochet_fermant(recv_buffer)<1) { decalage+=read; read=read_client(client->sock, recv_buffer + decalage); } recv_buffer[decalage+read]='\0'; printf("%s\n", recv_buffer); parse(recv_buffer, tab); if(strcmp(s0,"look")==0) { char* filename="filename"; int egal=1; int i; s2[0]='\n'; for(i=0;i<8;i++) { if(s1[i]!=filename[i]) egal --; } if(egal==1) { for(i=10;i<((int) strlen(s1))-1;i++) { s2[i-10]=s1[i]; } s2[strlen(s1)-11]='\0'; printf("recherche:\nfilename=%s\n",s2); struct file* file=find_file_name(file_list, s2); if(file==NULL) write_client(client->sock, "list []"); else { sprintf(send_buffer, "list [%s %d %d %s]", s2, file->length, file->p_size, file->key); write_client(client->sock, send_buffer); printf("replied:%s\n", send_buffer); } } } break; case'g': recv_buffer[read]='\0'; printf("%s\n", recv_buffer); parse(recv_buffer, tab); if(strcmp(s0,"getfile")==0) { struct file* f=find_file(file_list, s1); if(f==NULL||f->peer_list->first==NULL) { sprintf(send_buffer, "peers %s []", s1); write_client(client->sock, send_buffer); } else { struct elt_peer* aux=f->peer_list->first; struct in_addr* in_addr1=malloc(sizeof(struct in_addr)); in_addr1->s_addr=aux->peer->ip_address; char* d= inet_ntoa(*in_addr1); sprintf(send_buffer, "peers %s [%s:%d", s1, d, aux->peer->port); aux=aux->next; while(aux!=NULL) { struct in_addr* in_addr=malloc(sizeof(struct in_addr)); in_addr->s_addr=aux->peer->ip_address; char* c= inet_ntoa(*in_addr); sprintf(send_buffer+strlen(send_buffer), " %s:%d", c, aux->peer->port); aux=aux->next; } sprintf(send_buffer+strlen(send_buffer), "]"); write_client(client->sock, send_buffer); printf("replied:%s\n", send_buffer); } } break; default: printf("entree non valide"); //free free(s0); free(s1); free(s2); free(s3); free(s4); free(s5); free(s6); free(saux); free(recv_buffer); free(send_buffer); free(tab); free(t); free(t2); free(tz); free(tz2); end(client,donnees->ct); return 0; } pthread_mutex_unlock( &mutex); } } return EXIT_SUCCESS; }