static struct file_context * open_file(const char *url, int flags) { struct file_context *file_context; file_context = malloc(sizeof(struct file_context)); if (file_context == NULL) { fprintf(stderr, "Failed to malloc file_context\n"); return NULL; } file_context->fd = -1; file_context->nfs = NULL; file_context->nfsfh = NULL; file_context->url = NULL; file_context->nfs = nfs_init_context(); if (file_context->nfs == NULL) { fprintf(stderr, "failed to init context\n"); free_file_context(file_context); return NULL; } file_context->url = nfs_parse_url_full(file_context->nfs, url); if (file_context->url == NULL) { fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } if (nfs_mount(file_context->nfs, file_context->url->server, file_context->url->path) != 0) { fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } if (flags == O_RDONLY) { if (nfs_open(file_context->nfs, file_context->url->file, flags, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to open file %s: %s\n", file_context->url->file, nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } } else { if (nfs_creat(file_context->nfs, file_context->url->file, 0660, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to creat file %s: %s\n", file_context->url->file, nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } } return file_context; }
int main(int argc, char *argv[]) { int ret; struct file_context *nf; struct nfs_stat_64 st; uint64_t off; int64_t count; #ifdef WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("Failed to start Winsock2\n"); return 10; } #endif #ifdef AROS aros_init_socket(); #endif if (argc < 2) { usage(); } nf = open_file(argv[1], O_RDONLY); if (nf == NULL) { fprintf(stderr, "Failed to open %s\n", argv[1]); exit(10); } if (nfs_fstat64(nf->nfs, nf->nfsfh, &st) < 0) { fprintf(stderr, "Failed to stat %s\n", argv[1]); exit(10); } off = 0; while (off < st.nfs_size) { count = st.nfs_size - off; if (count > BUFSIZE) { count = BUFSIZE; } count = nfs_pread(nf->nfs, nf->nfsfh, off, count, buf); if (count < 0) { fprintf(stderr, "Failed to read from file\n"); free_file_context(nf); return 10; } fwrite(buf, count, 1, stdout); off += count; } free_file_context(nf); return 0; }
int main(int argc, char *argv[]) { int ret; struct stat st; struct file_context *src; struct file_context *dst; uint64_t off; int64_t count; #ifdef WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("Failed to start Winsock2\n"); return 10; } #endif #ifdef AROS aros_init_socket(); #endif if (argc != 3) { usage(); } src = open_file(argv[1], O_RDONLY); if (src == NULL) { fprintf(stderr, "Failed to open %s\n", argv[1]); return 10; } dst = open_file(argv[2], O_WRONLY|O_CREAT|O_TRUNC); if (dst == NULL) { fprintf(stderr, "Failed to open %s\n", argv[2]); free_file_context(src); return 10; } if (fstat_file(src, &st) != 0) { fprintf(stderr, "Failed to fstat source file\n"); free_file_context(src); free_file_context(dst); return 10; } off = 0; while (off < st.st_size) { count = st.st_size - off; if (count > BUFSIZE) { count = BUFSIZE; } count = file_pread(src, buf, count, off); if (count < 0) { fprintf(stderr, "Failed to read from source file\n"); free_file_context(src); free_file_context(dst); return 10; } count = file_pwrite(dst, buf, count, off); if (count < 0) { fprintf(stderr, "Failed to write to dest file\n"); free_file_context(src); free_file_context(dst); return 10; } off += count; } printf("copied %d bytes\n", (int)off); free_file_context(src); free_file_context(dst); return 0; }
static struct file_context * open_file(const char *url, int flags) { struct file_context *file_context; char *server = NULL, *path = NULL, *file = NULL, *strp; file_context = malloc(sizeof(struct file_context)); if (file_context == NULL) { fprintf(stderr, "Failed to malloc file_context\n"); return NULL; } file_context->is_nfs = 0; file_context->fd = -1; file_context->nfs = NULL; file_context->nfsfh = NULL; if (strncmp(url, "nfs://", 6)) { file_context->is_nfs = 0; file_context->fd = open(url, flags, 0660); if (file_context->fd == -1) { fprintf(stderr, "Failed to open %s\n", url); free_file_context(file_context); return NULL; } return file_context; } file_context->is_nfs = 1; file_context->nfs = nfs_init_context(); if (file_context->nfs == NULL) { fprintf(stderr, "failed to init context\n"); free_file_context(file_context); return NULL; } server = strdup(url + 6); if (server == NULL) { fprintf(stderr, "Failed to strdup server string\n"); free_file_context(file_context); return NULL; } if (server[0] == '/' || server[0] == '\0') { fprintf(stderr, "Invalid server string.\n"); free(server); free_file_context(file_context); return NULL; } strp = strchr(server, '/'); path = strdup(strp); *strp = 0; if (path == NULL) { fprintf(stderr, "Invalid URL specified.\n"); free(server); free_file_context(file_context); return NULL; } strp = strrchr(path, '/'); if (strp == NULL) { fprintf(stderr, "Invalid URL specified.\n"); free(path); free(server); free_file_context(file_context); return NULL; } file = strdup(strp); *strp = 0; if (nfs_mount(file_context->nfs, server, path) != 0) { fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(file_context->nfs)); free(file); free(path); free(server); free_file_context(file_context); return NULL; } if (flags == O_RDONLY) { if (nfs_open(file_context->nfs, file, flags, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to open file : %s\n", nfs_get_error(file_context->nfs)); free(file); free(path); free(server); free_file_context(file_context); return NULL; } } else { if (nfs_creat(file_context->nfs, file, 0660, &file_context->nfsfh) != 0) { fprintf(stderr, "Failed to creat file %s %s\n", file, nfs_get_error(file_context->nfs)); free(file); free(path); free(server); free_file_context(file_context); return NULL; } } free(file); free(path); free(server); return file_context; }