void add_file(char *name) { FILE *file; char *buffer; unsigned long fileLen; //Open file file = fopen(name, "rb"); if (!file) { fprintf(stderr, "Unable to open file %s\n", name); return 0; } //Get file length fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); //Allocate memory buffer=(char *)malloc(fileLen+1); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file); return 0; } //Read file contents into buffer fread(buffer, fileLen, 1, file); fclose(file); printf("shuffle file size %lu \n", fileLen); int times = 0; unsigned long seed = nSeed; for (times = 0; times < timeslast; times++) { printf("shuffle file %lu \n", times); add_buffer(buffer, fileLen); nSeed = seed; nSeed2 = seed; } write_add_buffer(name, buffer, fileLen); // dump_buffer_hex(buffer, fileLen); free(buffer); }
static int cmd_add(struct cli *cli, int argc, const char *args) { if (argc != 1 || !benc_isdct(args)) return IPC_COMMERR; struct tlib *tl; size_t mi_size = 0, csize = 0; const char *mi, *cp; char content[PATH_MAX]; uint8_t hash[20]; if ((mi = benc_dget_mem(args, "torrent", &mi_size)) == NULL) return IPC_COMMERR; if (!mi_test(mi, mi_size)) return write_code_buffer(cli, IPC_EBADT); if ((cp = benc_dget_mem(args, "content", &csize)) == NULL || csize >= PATH_MAX || csize == 0) return write_code_buffer(cli, IPC_EBADCDIR); if (cp[0] != '/') return write_code_buffer(cli, IPC_EBADCDIR); bcopy(cp, content, csize); content[csize] = '\0'; tl = tlib_by_hash(mi_info_hash(mi, hash)); if (tl != NULL && !torrent_haunting(tl)) return write_code_buffer(cli, IPC_ETENTEXIST); if (tl != NULL) { tl = tlib_readd(tl, hash, mi, mi_size, content, benc_dget_str(args, "name", NULL), benc_dget_str(args, "label", NULL)); } else { tl = tlib_add(hash, mi, mi_size, content, benc_dget_str(args, "name", NULL), benc_dget_str(args, "label", NULL)); } return write_add_buffer(cli, tl->num); }