int cmd_sendFile(t_env *e, char **entry) { struct stat st; char tmp[BUF_SZ]; int ret; if ((ret = check_login_entry(e, entry)) != EXIT_SUCCESS) return (ret); memset(tmp, 0, sizeof(tmp)); getcwd(tmp, BUF_SZ); sprintf(tmp, "/%s", entry[2]); stat(tmp, &st); if ((st.st_mode) == S_IFDIR) return (BAD_FILE); memset(tmp, 0, sizeof(tmp)); sprintf(tmp, "%s %s %s", entry[0], entry[1], entry[2]); write(e->sfd, tmp, strlen(tmp) + 1); usleep(500000); if (e->state == 0) { write(2, SEND_FILE_FAILED_MSG, strlen(SEND_FILE_FAILED_MSG)); return (SEND_FILE_FAILED); } if (e->state > 3) my_sendfile(e, entry, st.st_size); e->state = 0; return (EXIT_SUCCESS); }
/* Execute a client proc */ void runclient( Client *cl ) { if (cl->proc == CP_TFTP) tftp( cl ); if (cl->proc == CP_TFTP) { freeclient( cl ); return; } if (cl->proc == CP_SENDFILE) { my_sendfile( cl ); return; } if (cl->proc == CP_DONE) { freeclient( cl ); return; } // my_syslog( MLOG_ERROR, "tftp bad client proc %d", cl->proc ); }
int main(int argc, char** argv) { if (argc != 3) return 1; int n = atoi(argv[2]); FILE *in = fopen(argv[1], "rb"); FHeader h1; fread(&h1, sizeof(FHeader), 1, in); memcpy(&h1.magic, "CMC\x7f", 4); FSection *sections = malloc(sizeof(FSection) * h1.partnum); FSection *newsect = malloc(sizeof(FSection) * h1.partnum); fseek(in, -h1.partnum * sizeof(FSection), SEEK_END); fread(sections, sizeof(FSection), h1.partnum, in); int cnt = h1.partnum; int sectidx = 0; for (int i = 0; i < min(n, cnt); ++i) { char file[PATH_MAX]; snprintf(file, sizeof(file), "%s-%d", argv[1], i + 1); FILE *out = fopen(file, "wb"); h1.partnum = cnt / n + (i < cnt % n); fwrite(&h1, sizeof(FHeader), 1, out); for (int j = 0; j < cnt / n + (i < cnt % n); ++j) { newsect[j].offset = ftell(out); newsect[j].size = sections[sectidx].size; fseek(in, sections[sectidx].offset, SEEK_SET); my_sendfile(in, out, sections[sectidx].size); ++sectidx; } for (int j = 0; j < cnt / n + (i < cnt % n); ++j) { fwrite(newsect + j, sizeof(FSection), 1, out); } fclose(out); } fclose(in); free(newsect); free(sections); }