int mfs_file_copy(char *from_file, char *to_file) { char buf2[512]; int tmp; int fdr = mfs_file_open(from_file, MFS_MODE_READ); int fdw = mfs_file_open(to_file, MFS_MODE_CREATE); if (fdr < 0 || fdw < 0) { /* error opening file */ print("Cannot open file to read/write\n"); mfs_file_close(fdr); mfs_file_close(fdw); return 0; } while((tmp= mfs_file_read(fdr, buf2, 512))== 512){ mfs_file_write(fdw, buf2, 512); } if (tmp > 0) { mfs_file_write(fdw, buf2, tmp); } tmp = mfs_file_close(fdr); tmp = mfs_file_close(fdw); return 1; }
/** * print the file to stdout * @param filename - file to print * @return 1 on success, 0 on failure */ int mfs_cat(char *filename) { char buf2[513]; int tmp; int fdr = mfs_file_open(filename, MFS_MODE_READ); if (fdr < 0) { /* error opening file */ print("Cannot open file\n"); return 0; } while((tmp= mfs_file_read(fdr, buf2, 512))== 512){ buf2[512]='\0'; print(buf2); } if (tmp > 0) { buf2[tmp] = '\0'; print(buf2); } tmp = mfs_file_close(fdr); print("\n"); return 1; }
err_t http_sent_callback(void *arg, struct tcp_pcb *tpcb, u16_t len) { int BUFSIZE = 1400, sndbuf, n; char buf[BUFSIZE]; http_arg *a = (http_arg*)arg; if (g_webserver_debug) xil_printf("%d (%d): S%d..\r\n", a?a->count:0, tpcb->state, len); if (tpcb->state > ESTABLISHED) { if (a) { pfree_arg(a); a = NULL; } tcp_close(tpcb); return ERR_OK; } if (a->fd == -1 || a->fsize <= 0) /* no more data to be sent */ return ERR_OK; /* read more data out of the file and send it */ while (1) { sndbuf = tcp_sndbuf(tpcb); if (sndbuf < BUFSIZE) return ERR_OK; xil_printf("attempting to read %d bytes, left = %d bytes\r\n", BUFSIZE, a->fsize); n = mfs_file_read(a->fd, buf, BUFSIZE); tcp_write(tpcb, buf, n, 1); a->fsize -= n; if (a->fsize <= 0) { mfs_file_close(a->fd); a->fd = 0; break; } } return ERR_OK; }
/** * copy from stdin to named file * @param filename - file to print * @return 1 on success, 0 on failure */ int mfs_copy_stdin_to_file(char *filename) { char buf2[512]; int offset = 0; int c; int fdw = mfs_file_open(filename, MFS_MODE_CREATE); if (fdw < 0) { /* cannot open file */ print ("Cannot open file\n"); return 0; } while ((c = inbyte()) != EOF) { buf2[offset++] = c; if (offset == 512) { mfs_file_write(fdw, buf2, 512); offset = 0; } } if (offset != 512) { /* write the last buffer */ mfs_file_write(fdw, buf2, offset); } mfs_file_close(fdw); return 1; }
int main(int argc, char *argv[]) { int numbytes; char buffer[4]; int fd; int i; numbytes = 1000 *sizeof(struct mfs_file_block); fs = (char *)malloc(numbytes); mfs_init_fs(numbytes, fs, MFSINIT_NEW); mfs_create_dir("dir1"); mfs_change_dir("dir1"); mfs_create_dir("dir1_1"); mfs_change_dir("dir1_1"); mfs_create_dir("dir1_1_1"); fd = mfs_file_open("file1", MFS_MODE_CREATE); mfs_file_write(fd, "abcd", 4); mfs_file_close(fd); mfs_change_dir(".."); fd = mfs_file_open("file2", MFS_MODE_CREATE); mfs_file_write(fd, "efgh", 4); mfs_file_close(fd); fd = mfs_file_open("file2", MFS_MODE_READ); if (mfs_file_read(fd, buffer, 4) != 4) { printf("1"); } else { printf("0"); } mfs_file_close(fd); fd = mfs_file_open("file3", MFS_MODE_CREATE); for(i = 0; i < 512; i++) mfs_file_write(fd, "a", 1); mfs_file_close(fd); fd = mfs_file_open("file3", MFS_MODE_READ); i = mfs_file_lseek(fd, 0, SEEK_END); if (i != -1) { i = mfs_file_lseek(fd, 1, SEEK_CUR); if (i == -1) { i = mfs_file_lseek(fd, 0, SEEK_SET); if (i != -1) { i = mfs_file_lseek(fd, 1, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, 10, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, 20, SEEK_SET); } } } } } mfs_file_close(fd); fd = mfs_file_open("file4", MFS_MODE_CREATE); for (i = 0; i < 513; i++) mfs_file_write(fd, "b", 1); mfs_file_close(fd); fd = mfs_file_open("file4", MFS_MODE_READ); i = mfs_file_lseek(fd, 0, SEEK_END); if (i != -1) { i = mfs_file_lseek(fd, -1, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, 0, SEEK_SET); if (i != -1) { i = mfs_file_lseek(fd, 500, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, 12, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, 513, SEEK_SET); } } } } } mfs_file_close(fd); fd = mfs_file_open("file5", MFS_MODE_CREATE); for (i=0; i < 1024; i++) mfs_file_write(fd,"c",1); mfs_file_close(fd); fd = mfs_file_open("file6", MFS_MODE_CREATE); for (i =0; i < 1025; i++) mfs_file_write(fd, "d", 1); mfs_file_close(fd); fd = mfs_file_open("file6", MFS_MODE_READ); i = mfs_file_lseek(fd, 0, SEEK_END); if (i != -1) { i = mfs_file_lseek(fd, -1, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, 0, SEEK_SET); if (i != -1) { i = mfs_file_lseek(fd, 700, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, -250, SEEK_CUR); if (i != -1) { i = mfs_file_lseek(fd, 1025, SEEK_SET); } } } } } fd = mfs_file_open("file7", MFS_MODE_CREATE); for (i =0; i < 5000; i++) mfs_file_write(fd, "e", 1); mfs_file_close(fd); mfs_change_dir(".."); mfs_change_dir(".."); mfs_ls_r(-1); { int fd; char c; long l; fd = mfs_file_open("file1.tmp", MFS_MODE_CREATE); if (fd == -1) { // printf("error opening the file %d\n", errno); exit(1); } for (c= 0; c < 127; c++) { mfs_file_write(fd, &c, 1); } mfs_file_close (fd); fd = mfs_file_open("file1.tmp", MFS_MODE_READ); l = mfs_file_lseek (fd, 0, SEEK_SET); printf("l (0) %ld\n", l); mfs_file_read(fd, &c, 1); printf("c (0) %d\n", c); l = mfs_file_lseek (fd, 10, SEEK_CUR); printf("l (11) %ld\n", l); mfs_file_read(fd, &c, 1); printf("c (11) %d\n", c); l = mfs_file_lseek (fd, 20, SEEK_CUR); printf("l (32) %ld\n", l); mfs_file_read(fd, &c, 1); printf("c (32) %d\n", c); mfs_file_read(fd, &c, 1); printf("c (32) %d\n", c); l = mfs_file_lseek (fd, 0, SEEK_END); printf("l (127) %ld\n", l); mfs_file_read(fd, &c, 1); printf("c (-1) %d\n", c); } return 0; }
/* respond for a file GET request */ int do_http_get(struct tcp_pcb *pcb, char *req, int rlen) { int BUFSIZE = 1400; char filename[MAX_FILENAME]; char buf[BUFSIZE]; int fsize, hlen, n; int fd; char *fext; err_t err; /* determine file name */ extract_file_name(filename, req, rlen, MAX_FILENAME); /* respond with 404 if not present */ if (mfs_exists_file(filename) != 1) { xil_printf("requested file %s not found, returning 404\r\n", filename); do_404(pcb, req, rlen); return -1; } /* respond with correct file */ /* debug statement on UART */ xil_printf("http GET: %s\r\n", filename); /* get a pointer to file extension */ fext = get_file_extension(filename); fd = mfs_file_open(filename, MFS_MODE_READ); /* obtain file size, * note that lseek with offset 0, MFS_SEEK_END does not move file pointer */ fsize = mfs_file_lseek(fd, 0, MFS_SEEK_END); /* write the http headers */ hlen = generate_http_header(buf, fext, fsize); if ((err = tcp_write(pcb, buf, hlen, 1)) != ERR_OK) { xil_printf("error (%d) writing http header to socket\r\n", err); xil_printf("attempted to write #bytes = %d, tcp_sndbuf = %d\r\n", hlen, tcp_sndbuf(pcb)); xil_printf("http header = %s\r\n", buf); return -1; } /* now write the file */ while (fsize) { int sndbuf; sndbuf = tcp_sndbuf(pcb); if (sndbuf < BUFSIZE) { /* not enough space in sndbuf, so send remaining bytes when there is space */ /* this is done by storing the fd in as part of the tcp_arg, so that the sent callback handler knows to send data */ http_arg *a = (http_arg *)pcb->callback_arg; a->fd = fd; a->fsize = fsize; return 0; } n = mfs_file_read(fd, buf, BUFSIZE); if ((err = tcp_write(pcb, buf, n, 1)) != ERR_OK) { xil_printf("error writing file (%s) to socket, remaining unwritten bytes = %d\r\n", filename, fsize - n); xil_printf("attempted to lwip_write %d bytes, tcp write error = %d\r\n", n, err); break; } fsize -= n; } mfs_file_close(fd); return 0; }
/** * close a directory - same as closing a file * @param fd is the descriptor of the directory to close * @return 1 on success, 0 otherwise */ int mfs_dir_close(int fd) { return mfs_file_close(fd); }
int main(int argc, char *argv[]) { char buf[512]; char buf2[512]; int fdr; int fdw; int tmp; int num_iter; mfs_init_fs(20*sizeof(struct mfs_file_block), (char *)efs, MFSINIT_NEW); fdr = mfs_file_open(".", MFS_MODE_READ); tmp = mfs_file_read(fdr, &(buf[0]), 512); tmp = mfs_file_close(fdr); tmp = mfs_ls(); tmp = mfs_create_dir("testdir1"); tmp = mfs_create_dir("testdir2"); tmp = mfs_create_dir("."); tmp = mfs_ls(); tmp = mfs_change_dir("testdir1"); tmp = mfs_create_dir("testdir3"); tmp = mfs_create_dir("testdir1"); tmp = mfs_ls(); tmp = mfs_change_dir("testdir3"); fdw = mfs_file_open("testfile1", MFS_MODE_CREATE); strcpy(buf,"this is a test string"); for (num_iter = 0; num_iter < 100; num_iter++) tmp = mfs_file_write(fdw, buf, strlen(buf)); fdr = mfs_file_open("testfile1", MFS_MODE_READ); while((tmp= mfs_file_read(fdr, buf2, 512))== 512){ buf2[511]='\0'; strcpy(buf, buf2); } tmp = mfs_file_close(fdr); tmp = mfs_file_close(fdw); tmp = mfs_file_close(fdw); /* should return error */ tmp = mfs_ls(); tmp = mfs_cat("testfile1"); for (num_iter=65; num_iter < 91; num_iter++) { strcpy(buf,"testfileA"); buf[8] = num_iter; tmp = mfs_create_dir(buf); } tmp = mfs_ls(); tmp = mfs_change_dir("testfileX"); tmp = mfs_change_dir(".."); tmp = mfs_change_dir("."); tmp = mfs_ls(); tmp = mfs_delete_dir("testfileX"); tmp = mfs_delete_dir("."); tmp = mfs_delete_file("testfile1"); tmp = mfs_cat("testfile1"); /* should return error */ tmp = mfs_ls(); tmp = mfs_copy_stdin_to_file("stdinfile"); tmp = mfs_ls(); tmp = mfs_cat("stdinfile"); tmp = mfs_rename_file("stdinfile", "tmp1"); tmp = mfs_file_copy("tmp1", "tmp2"); tmp = mfs_ls(); tmp = mfs_cat("tmp2"); tmp = mfs_create_dir("/testpath1"); tmp = mfs_ls(); tmp = mfs_create_dir("/testpath1/testpath2"); tmp = mfs_change_dir("/testpath1/testpath2"); tmp = mfs_ls(); tmp = mfs_change_dir("/testpath1"); tmp = mfs_ls(); tmp = mfs_change_dir("/testpath2"); tmp = mfs_change_dir("testpath2"); tmp = mfs_create_dir("/a/b/c"); tmp = mfs_change_dir("/testpath2/"); tmp = mfs_change_dir("/testpath1/"); tmp = mfs_ls(); tmp = mfs_change_dir("/testpath1/testpath2/"); tmp = mfs_ls(); tmp = mfs_create_dir("testpath3/"); tmp = mfs_ls(); tmp = mfs_delete_dir("/testpath1/testpath2/testpath3"); tmp = mfs_change_dir("/testpath1"); tmp = mfs_delete_dir("/testpath1/testpath2/"); tmp = mfs_ls(); tmp = mfs_change_dir("/"); tmp = mfs_ls(); fdw = mfs_file_open("testappend", MFS_MODE_CREATE); strcpy(buf2,"testing append mode...\n"); tmp = mfs_file_write(fdw, buf2, strlen(buf2)); tmp = mfs_file_close(fdw); tmp = mfs_cat("testappend"); fdw = mfs_file_open("testappend", MFS_MODE_WRITE); tmp = mfs_file_lseek(fdw, 0, MFS_SEEK_END); strcpy(buf2, "testing append mode2\n"); tmp = mfs_file_write(fdw, buf2, strlen(buf2)); tmp = mfs_file_close(fdw); tmp = mfs_cat("testappend"); fdw = mfs_file_open("testappend", MFS_MODE_WRITE); tmp = mfs_file_lseek(fdw, 10, MFS_SEEK_SET); strcpy(buf2, "testing append mode3\n"); tmp = mfs_file_write(fdw, buf2, strlen(buf2)); tmp = mfs_file_lseek(fdw,-10, MFS_SEEK_END); tmp = mfs_file_write(fdw, buf2, strlen(buf2)); tmp = mfs_file_close(fdw); tmp = mfs_cat("testappend"); return 0; }