static void Close(vlc_object_t *p_obj) { access_t *p_access = (access_t *)p_obj; access_sys_t *p_sys = p_access->p_sys; if (p_sys->p_nfsfh != NULL) nfs_close(p_sys->p_nfs, p_sys->p_nfsfh); if (p_sys->p_nfsdir != NULL) nfs_closedir(p_sys->p_nfs, p_sys->p_nfsdir); if (p_sys->p_nfs != NULL) nfs_destroy_context(p_sys->p_nfs); if (p_sys->p_mount != NULL) { for (int i = 0; i < p_sys->res.exports.i_count; ++i) free(p_sys->res.exports.ppsz_names[i]); free(p_sys->res.exports.ppsz_names); rpc_destroy_context(p_sys->p_mount); } if (p_sys->p_nfs_url != NULL) nfs_destroy_url(p_sys->p_nfs_url); vlc_UrlClean(&p_sys->encoded_url); free(p_sys->psz_url_decoded); free(p_sys->psz_url_decoded_slash); free(p_sys); }
int main(int argc, char *argv[]) { struct nfs_context *nfs; struct nfs_url *url; uint64_t length; struct nfs_stat_64 st; if (argc != 5) { usage(); } length = strtol(argv[4], NULL, 10); nfs = nfs_init_context(); if (nfs == NULL) { printf("failed to init context\n"); exit(1); } url = nfs_parse_url_full(nfs, argv[1]); if (url == NULL) { fprintf(stderr, "%s\n", nfs_get_error(nfs)); exit(1); } if (nfs_mount(nfs, url->server, url->path) != 0) { fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs)); exit(1); } if (nfs_chdir(nfs, argv[2]) != 0) { fprintf(stderr, "Failed to chdir to \"%s\" : %s\n", argv[2], nfs_get_error(nfs)); exit(1); } if (nfs_truncate(nfs, argv[3], length)) { fprintf(stderr, "Failed to truncate file : %s\n", nfs_get_error(nfs)); exit(1); } if (nfs_stat64(nfs, argv[3], &st)) { fprintf(stderr, "Failed to stat file : %s\n", nfs_get_error(nfs)); exit(1); } if (st.nfs_size != length) { fprintf(stderr, "Unexpected file size. Expected %d got %d\n", (int)length, (int)st.nfs_size); exit(1); } nfs_destroy_url(url); nfs_destroy_context(nfs); return 0; }
int main(int argc, char *argv[]) { struct nfs_context *nfs = NULL; struct nfs_url *url = NULL; int ret = 0; int mode; if (argc != 5) { usage(); } mode = strtol(argv[4], NULL, 8); nfs = nfs_init_context(); if (nfs == NULL) { printf("failed to init context\n"); exit(1); } url = nfs_parse_url_full(nfs, argv[1]); if (url == NULL) { fprintf(stderr, "%s\n", nfs_get_error(nfs)); exit(1); } if (nfs_mount(nfs, url->server, url->path) != 0) { fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs)); ret = 1; goto finished; } if (nfs_chdir(nfs, argv[2]) != 0) { fprintf(stderr, "Failed to chdir to \"%s\" : %s\n", argv[2], nfs_get_error(nfs)); ret = 1; goto finished; } if (nfs_lchmod(nfs, argv[3], mode)) { fprintf(stderr, "Failed to lchmod(): %s\n", nfs_get_error(nfs)); ret = 1; goto finished; } finished: nfs_destroy_url(url); nfs_destroy_context(nfs); return ret; }
static void free_file_context(struct file_context *file_context) { if (file_context->fd != -1) { close(file_context->fd); } if (file_context->nfsfh != NULL) { nfs_close(file_context->nfs, file_context->nfsfh); } if (file_context->nfs != NULL) { nfs_destroy_context(file_context->nfs); } nfs_destroy_url(file_context->url); free(file_context); }
static int Open(vlc_object_t *p_obj) { access_t *p_access = (access_t *)p_obj; access_sys_t *p_sys = calloc(1, sizeof (*p_sys)); if (unlikely(p_sys == NULL)) goto error; p_access->p_sys = p_sys; p_sys->b_auto_guid = var_InheritBool(p_obj, "nfs-auto-guid"); /* nfs_* functions need a decoded url */ p_sys->psz_url_decoded = vlc_uri_decode_duplicate(p_access->psz_url); if (p_sys->psz_url_decoded == NULL) goto error; /* Parse the encoded URL */ vlc_UrlParse(&p_sys->encoded_url, p_access->psz_url); if (p_sys->encoded_url.psz_option) { if (strstr(p_sys->encoded_url.psz_option, "uid") || strstr(p_sys->encoded_url.psz_option, "gid")) p_sys->b_auto_guid = false; } if (NfsInit(p_access, p_sys->psz_url_decoded) == -1) goto error; if (p_sys->p_nfs_url->path != NULL && p_sys->p_nfs_url->file != NULL) { /* The url has a valid path and file, mount the path and open/opendir * the file */ msg_Dbg(p_access, "nfs_mount: server: '%s', path: '%s'", p_sys->p_nfs_url->server, p_sys->p_nfs_url->path); if (nfs_mount_async(p_sys->p_nfs, p_sys->p_nfs_url->server, p_sys->p_nfs_url->path, nfs_mount_cb, p_access) < 0) { msg_Err(p_access, "nfs_mount_async failed"); goto error; } if (vlc_nfs_mainloop(p_access, nfs_mount_open_finished_cb) < 0) goto error; if (p_sys->psz_url_decoded_slash != NULL) { /* Retry to mount by adding a '/' to the path, see comment in * nfs_mount_cb */ nfs_destroy_url(p_sys->p_nfs_url); nfs_destroy_context(p_sys->p_nfs); p_sys->p_nfs_url = NULL; p_sys->p_nfs = NULL; if (NfsInit(p_access, p_sys->psz_url_decoded_slash) == -1 || p_sys->p_nfs_url->path == NULL || p_sys->p_nfs_url->file == NULL) goto error; if (nfs_mount_async(p_sys->p_nfs, p_sys->p_nfs_url->server, p_sys->p_nfs_url->path, nfs_mount_cb, p_access) < 0) { msg_Err(p_access, "nfs_mount_async failed"); goto error; } if (vlc_nfs_mainloop(p_access, nfs_mount_open_slash_finished_cb) < 0) goto error; } if (p_sys->p_nfsfh != NULL) { p_access->pf_read = FileRead; p_access->pf_seek = FileSeek; p_access->pf_control = FileControl; } else if (p_sys->p_nfsdir != NULL) { p_access->pf_readdir = DirRead; p_access->pf_seek = NULL; p_access->pf_control = DirControl; } else vlc_assert_unreachable(); } else { /* url is just a server: fetch exports point */ nfs_destroy_context(p_sys->p_nfs); p_sys->p_nfs = NULL; p_sys->p_mount = rpc_init_context(); if (p_sys->p_mount == NULL) { msg_Err(p_access, "rpc_init_context failed"); goto error; } p_sys->res.exports.ppsz_names = NULL; p_sys->res.exports.i_count = -1; if (mount_getexports_async(p_sys->p_mount, p_sys->p_nfs_url->server, mount_export_cb, p_access) < 0) { msg_Err(p_access, "mount_getexports_async failed"); goto error; } if (vlc_mount_mainloop(p_access, mount_getexports_finished_cb) < 0) goto error; p_access->pf_readdir = MountRead; p_access->pf_seek = NULL; p_access->pf_control = DirControl; } return VLC_SUCCESS; error: Close(p_obj); return VLC_EGENERIC; }
int main(int argc, char *argv[]) { int ret = 1; struct nfs_context *nfs = NULL; struct nfsfh *nfsfh = NULL; struct nfs_url *url = NULL; #ifdef WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("Failed to start Winsock2\n"); exit(10); } #endif #ifdef AROS aros_init_socket(); #endif if (argc < 3) { fprintf(stderr, "No URL specified.\n"); goto finished; } nfs = nfs_init_context(); if (nfs == NULL) { printf("failed to init context\n"); goto finished; } url = nfs_parse_url_full(nfs, argv[argc - 1]); if (url == NULL) { fprintf(stderr, "%s\n", nfs_get_error(nfs)); goto finished; } if (nfs_mount(nfs, url->server, url->path) != 0) { fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs)); goto finished; } if (!strncmp(argv[1], "creat", 5)) { ret = nfs_creat(nfs, url->file, 0600, &nfsfh); } else if (!strncmp(argv[1], "unlink", 6)) { ret = nfs_unlink(nfs, url->file); } else if (!strncmp(argv[1], "mkdir", 5)) { ret = nfs_mkdir(nfs, url->file); } else if (!strncmp(argv[1], "rmdir", 5)) { ret = nfs_rmdir(nfs, url->file); } else if (!strncmp(argv[1], "stat", 4)) { struct nfs_stat_64 st; ret = nfs_stat64(nfs, url->file, &st); if (!ret) { switch (st.nfs_mode & S_IFMT) { #ifndef WIN32 case S_IFLNK: printf("l"); break; #endif case S_IFREG: printf("-"); break; case S_IFDIR: printf("d"); break; case S_IFCHR: printf("c"); break; case S_IFBLK: printf("b"); break; } printf("%c%c%c", "-r"[!!(st.nfs_mode & S_IRUSR)], "-w"[!!(st.nfs_mode & S_IWUSR)], "-x"[!!(st.nfs_mode & S_IXUSR)] ); printf("%c%c%c", "-r"[!!(st.nfs_mode & S_IRGRP)], "-w"[!!(st.nfs_mode & S_IWGRP)], "-x"[!!(st.nfs_mode & S_IXGRP)] ); printf("%c%c%c", "-r"[!!(st.nfs_mode & S_IROTH)], "-w"[!!(st.nfs_mode & S_IWOTH)], "-x"[!!(st.nfs_mode & S_IXOTH)] ); printf(" %2d", (int)st.nfs_nlink); printf(" %5d", (int)st.nfs_uid); printf(" %5d", (int)st.nfs_gid); printf(" %12" PRId64, st.nfs_size); printf("\n"); } } else { goto finished; } if (ret) { fprintf(stderr, "ERROR: %s\n", nfs_get_error(nfs)); } finished: if (ret > 0) { print_usage(); } nfs_destroy_url(url); if (nfs != NULL) { if (nfsfh) { nfs_close(nfs, nfsfh); } nfs_destroy_context(nfs); } return !!ret; }
int open(const char *path, int flags, mode_t mode) { if (!strncmp(path, "nfs:", 4)) { struct nfs_context *nfs; struct nfs_url *url; struct nfsfh *fh = NULL; int ret, fd; LD_NFS_DPRINTF(9, "open(%s, %x, %o)", path, flags, mode); nfs = nfs_init_context(); if (nfs == NULL) { LD_NFS_DPRINTF(1, "Failed to create context"); errno = ENOMEM; return -1; } url = nfs_parse_url_full(nfs, path); if (url == NULL) { LD_NFS_DPRINTF(1, "Failed to parse URL: %s\n", nfs_get_error(nfs)); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if (nfs_mount(nfs, url->server, url->path) != 0) { LD_NFS_DPRINTF(1, "Failed to mount nfs share : %s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if (flags & O_CREAT) { if ((ret = nfs_creat(nfs, url->file, mode, &fh)) != 0) { LD_NFS_DPRINTF(1, "Failed to creat nfs file : " "%s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = -ret; return -1; } } else { if ((ret = nfs_open(nfs, url->file, flags, &fh)) != 0) { LD_NFS_DPRINTF(1, "Failed to open nfs file : " "%s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = -ret; return -1; } } fd = nfs_get_fd(nfs); if (fd >= NFS_MAX_FD) { LD_NFS_DPRINTF(1, "Too many files open"); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = ENFILE; return -1; } nfs_fd_list[fd].is_nfs = 1; nfs_fd_list[fd].nfs = nfs; nfs_fd_list[fd].fh = fh; nfs_fd_list[fd].path = strdup(path); nfs_fd_list[fd].flags = flags; nfs_fd_list[fd].mode = mode; nfs_destroy_url(url); LD_NFS_DPRINTF(9, "open(%s) == %d", path, fd); return fd; } return real_open(path, flags, mode); }
int dup2(int oldfd, int newfd) { close(newfd); if (nfs_fd_list[oldfd].is_nfs == 1) { struct nfs_context *nfs; struct nfs_url *url; struct nfsfh *fh = NULL; int ret, fd; LD_NFS_DPRINTF(9, "dup2(%s:%d, %d)", nfs_fd_list[oldfd].path, oldfd, newfd); nfs = nfs_init_context(); if (nfs == NULL) { LD_NFS_DPRINTF(1, "Failed to create context"); errno = ENOMEM; return -1; } url = nfs_parse_url_full(nfs, nfs_fd_list[oldfd].path); if (url == NULL) { LD_NFS_DPRINTF(1, "Failed to parse URL: %s\n", nfs_get_error(nfs)); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if (nfs_mount(nfs, url->server, url->path) != 0) { LD_NFS_DPRINTF(1, "Failed to mount nfs share : %s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = EINVAL; return -1; } if ((ret = nfs_open(nfs, url->file, nfs_fd_list[oldfd].mode, &fh)) != 0) { LD_NFS_DPRINTF(1, "Failed to open nfs file : %s\n", nfs_get_error(nfs)); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = -ret; return -1; } /* We could actually end on the right descriptor by chance */ if (nfs_get_fd(nfs) != newfd) { if (real_dup2(nfs_get_fd(nfs), newfd) < 0) { LD_NFS_DPRINTF(1, "Failed to dup2 file : %d", errno); return -1; } close(rpc_get_fd(nfs_get_rpc_context(nfs))); rpc_set_fd(nfs_get_rpc_context(nfs), newfd); } fd = nfs_get_fd(nfs); if (fd >= NFS_MAX_FD) { LD_NFS_DPRINTF(1, "Too many files open"); nfs_destroy_url(url); nfs_destroy_context(nfs); errno = ENFILE; return -1; } nfs_fd_list[fd].is_nfs = 1; nfs_fd_list[fd].nfs = nfs; nfs_fd_list[fd].fh = fh; nfs_fd_list[fd].path = strdup(nfs_fd_list[oldfd].path); nfs_fd_list[fd].flags = nfs_fd_list[oldfd].flags; nfs_fd_list[fd].mode = nfs_fd_list[oldfd].mode; nfs_destroy_url(url); LD_NFS_DPRINTF(9, "dup2(%s) successful", nfs_fd_list[oldfd].path); return fd; } return real_dup2(oldfd, newfd); }