1 /*****************************************************************************
2 * nfs.c: NFS VLC access plug-in
3 *****************************************************************************
4 * Copyright © 2016 VLC authors, VideoLAN and VideoLabs
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
29 #include <sys/types.h>
36 #include <vlc_common.h>
37 #include <vlc_access.h>
38 #include <vlc_dialog.h>
39 #include <vlc_input_item.h>
40 #include <vlc_plugin.h>
42 #include <vlc_interrupt.h>
44 #include <nfsc/libnfs.h>
45 #include <nfsc/libnfs-raw.h>
46 #include <nfsc/libnfs-raw-nfs.h>
47 #include <nfsc/libnfs-raw-mount.h>
49 #define AUTO_GUID_TEXT N_("Set NFS uid/guid automatically")
50 #define AUTO_GUID_LONGTEXT N_("If uid/gid are not specified in " \
51 "the url, VLC will automatically set a uid/gid.")
53 static int Open(vlc_object_t
*);
54 static void Close(vlc_object_t
*);
57 set_shortname(N_("NFS"))
58 set_description(N_("NFS input"))
59 set_category(CAT_INPUT
)
60 set_subcategory(SUBCAT_INPUT_ACCESS
)
61 add_bool("nfs-auto-guid", true, AUTO_GUID_TEXT
, AUTO_GUID_LONGTEXT
, true)
62 set_capability("access", 0)
64 set_callbacks(Open
, Close
)
69 struct rpc_context
* p_mount
; /* used to to get exports mount point */
70 struct nfs_context
* p_nfs
;
71 struct nfs_url
* p_nfs_url
;
72 struct nfs_stat_64 stat
;
73 struct nfsfh
* p_nfsfh
;
74 struct nfsdir
* p_nfsdir
;
75 vlc_url_t encoded_url
;
76 char * psz_url_decoded
;
77 char * psz_url_decoded_slash
;
101 nfs_check_status(stream_t
*p_access
, int i_status
, const char *psz_error
,
102 const char *psz_func
)
104 access_sys_t
*sys
= p_access
->p_sys
;
108 if (i_status
!= -EINTR
)
110 msg_Err(p_access
, "%s failed: %d, '%s'", psz_func
, i_status
,
113 vlc_dialog_display_error(p_access
,
114 _("NFS operation failed"), "%s",
118 msg_Warn(p_access
, "%s interrupted", psz_func
);
125 #define NFS_CHECK_STATUS(p_access, i_status, p_data) \
126 nfs_check_status(p_access, i_status, (const char *)p_data, __func__)
129 vlc_rpc_mainloop(stream_t
*p_access
, struct rpc_context
*p_rpc_ctx
,
130 bool (*pf_until_cb
)(stream_t
*))
132 access_sys_t
*p_sys
= p_access
->p_sys
;
134 while (!p_sys
->b_error
&& !pf_until_cb(p_access
))
136 struct pollfd p_fds
[1];
138 p_fds
[0].fd
= rpc_get_fd(p_rpc_ctx
);
139 p_fds
[0].events
= rpc_which_events(p_rpc_ctx
);
141 if ((i_ret
= vlc_poll_i11e(p_fds
, 1, -1)) < 0)
144 msg_Warn(p_access
, "vlc_poll_i11e interrupted");
146 msg_Err(p_access
, "vlc_poll_i11e failed");
147 p_sys
->b_error
= true;
149 else if (i_ret
> 0 && p_fds
[0].revents
150 && rpc_service(p_rpc_ctx
, p_fds
[0].revents
) < 0)
152 msg_Err(p_access
, "nfs_service failed");
153 p_sys
->b_error
= true;
156 return p_sys
->b_error
? -1 : 0;
160 vlc_nfs_mainloop(stream_t
*p_access
, bool (*pf_until_cb
)(stream_t
*))
162 access_sys_t
*p_sys
= p_access
->p_sys
;
163 assert(p_sys
->p_nfs
!= NULL
);
164 return vlc_rpc_mainloop(p_access
, nfs_get_rpc_context(p_sys
->p_nfs
),
169 vlc_mount_mainloop(stream_t
*p_access
, bool (*pf_until_cb
)(stream_t
*))
171 access_sys_t
*p_sys
= p_access
->p_sys
;
172 assert(p_sys
->p_mount
!= NULL
);
173 return vlc_rpc_mainloop(p_access
, p_sys
->p_mount
, pf_until_cb
);
177 nfs_read_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
178 void *p_private_data
)
181 stream_t
*p_access
= p_private_data
;
182 access_sys_t
*p_sys
= p_access
->p_sys
;
183 assert(p_sys
->p_nfs
== p_nfs
);
184 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
191 p_sys
->res
.read
.i_len
= i_status
;
192 memcpy(p_sys
->res
.read
.p_buf
, p_data
, i_status
);
197 nfs_read_finished_cb(stream_t
*p_access
)
199 access_sys_t
*p_sys
= p_access
->p_sys
;
200 return p_sys
->res
.read
.i_len
> 0 || p_sys
->b_eof
;
204 FileRead(stream_t
*p_access
, void *p_buf
, size_t i_len
)
206 access_sys_t
*p_sys
= p_access
->p_sys
;
211 p_sys
->res
.read
.i_len
= 0;
212 p_sys
->res
.read
.p_buf
= p_buf
;
213 if (nfs_read_async(p_sys
->p_nfs
, p_sys
->p_nfsfh
, i_len
, nfs_read_cb
,
216 msg_Err(p_access
, "nfs_read_async failed");
220 if (vlc_nfs_mainloop(p_access
, nfs_read_finished_cb
) < 0)
223 return p_sys
->res
.read
.i_len
;
227 nfs_seek_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
228 void *p_private_data
)
231 stream_t
*p_access
= p_private_data
;
232 access_sys_t
*p_sys
= p_access
->p_sys
;
233 assert(p_sys
->p_nfs
== p_nfs
);
235 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
238 p_sys
->res
.seek
.b_done
= true;
242 nfs_seek_finished_cb(stream_t
*p_access
)
244 access_sys_t
*p_sys
= p_access
->p_sys
;
245 return p_sys
->res
.seek
.b_done
;
249 FileSeek(stream_t
*p_access
, uint64_t i_pos
)
251 access_sys_t
*p_sys
= p_access
->p_sys
;
253 p_sys
->res
.seek
.b_done
= false;
254 if (nfs_lseek_async(p_sys
->p_nfs
, p_sys
->p_nfsfh
, i_pos
, SEEK_SET
,
255 nfs_seek_cb
, p_access
) < 0)
257 msg_Err(p_access
, "nfs_seek_async failed");
261 if (vlc_nfs_mainloop(p_access
, nfs_seek_finished_cb
) < 0)
264 p_sys
->b_eof
= false;
270 FileControl(stream_t
*p_access
, int i_query
, va_list args
)
272 access_sys_t
*p_sys
= p_access
->p_sys
;
276 case STREAM_CAN_SEEK
:
277 *va_arg(args
, bool *) = true;
280 case STREAM_CAN_FASTSEEK
:
281 *va_arg(args
, bool *) = false;
284 case STREAM_CAN_PAUSE
:
285 case STREAM_CAN_CONTROL_PACE
:
286 *va_arg(args
, bool *) = true;
289 case STREAM_GET_SIZE
:
291 *va_arg(args
, uint64_t *) = p_sys
->stat
.nfs_size
;
295 case STREAM_GET_PTS_DELAY
:
296 *va_arg(args
, vlc_tick_t
*) = VLC_TICK_FROM_MS(
297 var_InheritInteger(p_access
, "network-caching") );
300 case STREAM_SET_PAUSE_STATE
:
310 NfsGetUrl(vlc_url_t
*p_url
, const char *psz_file
)
312 /* nfs://<psz_host><psz_path><psz_file>?<psz_option> */
314 if (asprintf(&psz_url
, "nfs://%s%s%s%s%s%s", p_url
->psz_host
,
315 p_url
->psz_path
!= NULL
? p_url
->psz_path
: "",
316 p_url
->psz_path
!= NULL
&& p_url
->psz_path
[0] != '\0' &&
317 p_url
->psz_path
[strlen(p_url
->psz_path
) - 1] != '/' ? "/" : "",
319 p_url
->psz_option
!= NULL
? "?" : "",
320 p_url
->psz_option
!= NULL
? p_url
->psz_option
: "") == -1)
327 DirRead(stream_t
*p_access
, input_item_node_t
*p_node
)
329 access_sys_t
*p_sys
= p_access
->p_sys
;
330 struct nfsdirent
*p_nfsdirent
;
331 int i_ret
= VLC_SUCCESS
;
332 assert(p_sys
->p_nfsdir
);
334 struct vlc_readdir_helper rdh
;
335 vlc_readdir_helper_init(&rdh
, p_access
, p_node
);
337 while (i_ret
== VLC_SUCCESS
338 && (p_nfsdirent
= nfs_readdir(p_sys
->p_nfs
, p_sys
->p_nfsdir
)) != NULL
)
340 char *psz_name_encoded
= vlc_uri_encode(p_nfsdirent
->name
);
341 if (psz_name_encoded
== NULL
)
346 char *psz_url
= NfsGetUrl(&p_sys
->encoded_url
, psz_name_encoded
);
347 free(psz_name_encoded
);
355 switch (p_nfsdirent
->type
)
358 i_type
= ITEM_TYPE_FILE
;
361 i_type
= ITEM_TYPE_DIRECTORY
;
364 i_type
= ITEM_TYPE_UNKNOWN
;
366 i_ret
= vlc_readdir_helper_additem(&rdh
, psz_url
, NULL
, p_nfsdirent
->name
,
371 vlc_readdir_helper_finish(&rdh
, i_ret
== VLC_SUCCESS
);
377 MountRead(stream_t
*p_access
, input_item_node_t
*p_node
)
379 access_sys_t
*p_sys
= p_access
->p_sys
;
380 assert(p_sys
->p_mount
!= NULL
&& p_sys
->res
.exports
.i_count
>= 0);
381 int i_ret
= VLC_SUCCESS
;
383 struct vlc_readdir_helper rdh
;
384 vlc_readdir_helper_init(&rdh
, p_access
, p_node
);
386 for (int i
= 0; i
< p_sys
->res
.exports
.i_count
&& i_ret
== VLC_SUCCESS
; ++i
)
388 char *psz_name
= p_sys
->res
.exports
.ppsz_names
[i
];
390 char *psz_url
= NfsGetUrl(&p_sys
->encoded_url
, psz_name
);
396 i_ret
= vlc_readdir_helper_additem(&rdh
, psz_url
, NULL
, psz_name
,
397 ITEM_TYPE_DIRECTORY
, ITEM_NET
);
401 vlc_readdir_helper_finish(&rdh
, i_ret
== VLC_SUCCESS
);
407 nfs_opendir_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
408 void *p_private_data
)
411 stream_t
*p_access
= p_private_data
;
412 access_sys_t
*p_sys
= p_access
->p_sys
;
413 assert(p_sys
->p_nfs
== p_nfs
);
414 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
417 p_sys
->p_nfsdir
= p_data
;
421 nfs_open_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
422 void *p_private_data
)
425 stream_t
*p_access
= p_private_data
;
426 access_sys_t
*p_sys
= p_access
->p_sys
;
427 assert(p_sys
->p_nfs
== p_nfs
);
428 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
431 p_sys
->p_nfsfh
= p_data
;
435 nfs_stat64_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
436 void *p_private_data
)
439 stream_t
*p_access
= p_private_data
;
440 access_sys_t
*p_sys
= p_access
->p_sys
;
441 assert(p_sys
->p_nfs
== p_nfs
);
442 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
445 struct nfs_stat_64
*p_stat
= p_data
;
446 p_sys
->stat
= *p_stat
;
448 if (p_sys
->b_auto_guid
)
450 nfs_set_uid(p_sys
->p_nfs
, p_sys
->stat
.nfs_uid
);
451 nfs_set_gid(p_sys
->p_nfs
, p_sys
->stat
.nfs_gid
);
454 if (S_ISDIR(p_sys
->stat
.nfs_mode
))
456 msg_Dbg(p_access
, "nfs_opendir: '%s'", p_sys
->p_nfs_url
->file
);
457 if (nfs_opendir_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->file
,
458 nfs_opendir_cb
, p_access
) != 0)
460 msg_Err(p_access
, "nfs_opendir_async failed");
461 p_sys
->b_error
= true;
464 else if (S_ISREG(p_sys
->stat
.nfs_mode
))
466 msg_Dbg(p_access
, "nfs_open: '%s'", p_sys
->p_nfs_url
->file
);
467 if (nfs_open_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->file
, O_RDONLY
,
468 nfs_open_cb
, p_access
) < 0)
470 msg_Err(p_access
, "nfs_open_async failed");
471 p_sys
->b_error
= true;
476 msg_Err(p_access
, "nfs_stat64_cb: file type not handled");
477 p_sys
->b_error
= true;
482 nfs_mount_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
483 void *p_private_data
)
486 stream_t
*p_access
= p_private_data
;
487 access_sys_t
*p_sys
= p_access
->p_sys
;
488 assert(p_sys
->p_nfs
== p_nfs
);
491 /* If a directory url doesn't end with '/', there is no way to know which
492 * part of the url is the export point and which part is the path. An
493 * example with "nfs://myhost/mnt/data": we can't know if /mnt or /mnt/data
494 * is the export point. Therefore, in case of EACCES error, retry to mount
495 * the url by adding a '/' to the decoded path. */
496 if (i_status
== -EACCES
&& p_sys
->psz_url_decoded_slash
== NULL
)
499 vlc_UrlParseFixup(&url
, p_access
->psz_url
);
500 if (url
.psz_path
== NULL
|| url
.psz_path
[0] == '\0'
501 || url
.psz_path
[strlen(url
.psz_path
) - 1] == '/'
502 || (p_sys
->psz_url_decoded_slash
= NfsGetUrl(&url
, "/")) == NULL
)
505 NFS_CHECK_STATUS(p_access
, i_status
, p_data
);
511 msg_Warn(p_access
, "trying to mount '%s' again by adding a '/'",
517 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
520 if (nfs_stat64_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->file
, nfs_stat64_cb
,
523 msg_Err(p_access
, "nfs_stat64_async failed");
524 p_sys
->b_error
= true;
529 nfs_mount_open_finished_cb(stream_t
*p_access
)
531 access_sys_t
*p_sys
= p_access
->p_sys
;
532 return p_sys
->p_nfsfh
!= NULL
|| p_sys
->p_nfsdir
!= NULL
533 || p_sys
->psz_url_decoded_slash
!= NULL
;
537 nfs_mount_open_slash_finished_cb(stream_t
*p_access
)
539 access_sys_t
*p_sys
= p_access
->p_sys
;
540 return p_sys
->p_nfsfh
!= NULL
|| p_sys
->p_nfsdir
!= NULL
;
544 mount_export_cb(struct rpc_context
*p_ctx
, int i_status
, void *p_data
,
545 void *p_private_data
)
548 stream_t
*p_access
= p_private_data
;
549 access_sys_t
*p_sys
= p_access
->p_sys
;
550 assert(p_sys
->p_mount
== p_ctx
);
551 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
554 exports p_export
= *(exports
*)p_data
;
555 p_sys
->res
.exports
.i_count
= 0;
557 /* Dup the export linked list into an array of const char * */
558 while (p_export
!= NULL
)
560 p_sys
->res
.exports
.i_count
++;
561 p_export
= p_export
->ex_next
;
563 if (p_sys
->res
.exports
.i_count
== 0)
566 p_sys
->res
.exports
.ppsz_names
= calloc(p_sys
->res
.exports
.i_count
,
568 if (p_sys
->res
.exports
.ppsz_names
== NULL
)
570 p_sys
->b_error
= true;
574 p_export
= *(exports
*)p_data
;
575 unsigned int i_idx
= 0;
576 while (p_export
!= NULL
)
578 p_sys
->res
.exports
.ppsz_names
[i_idx
] = strdup(p_export
->ex_dir
);
579 if (p_sys
->res
.exports
.ppsz_names
[i_idx
] == NULL
)
581 for (unsigned int i
= 0; i
< i_idx
; ++i
)
582 free(p_sys
->res
.exports
.ppsz_names
[i
]);
583 free(p_sys
->res
.exports
.ppsz_names
);
584 p_sys
->res
.exports
.ppsz_names
= NULL
;
585 p_sys
->res
.exports
.i_count
= 0;
586 p_sys
->b_error
= true;
590 p_export
= p_export
->ex_next
;
595 mount_getexports_finished_cb(stream_t
*p_access
)
597 access_sys_t
*p_sys
= p_access
->p_sys
;
598 return p_sys
->res
.exports
.i_count
!= -1;
602 NfsInit(stream_t
*p_access
, const char *psz_url_decoded
)
604 access_sys_t
*p_sys
= p_access
->p_sys
;
605 p_sys
->p_nfs
= nfs_init_context();
606 if (p_sys
->p_nfs
== NULL
)
608 msg_Err(p_access
, "nfs_init_context failed");
612 p_sys
->p_nfs_url
= nfs_parse_url_incomplete(p_sys
->p_nfs
, psz_url_decoded
);
613 if (p_sys
->p_nfs_url
== NULL
|| p_sys
->p_nfs_url
->server
== NULL
)
615 msg_Err(p_access
, "nfs_parse_url_incomplete failed: '%s'",
616 nfs_get_error(p_sys
->p_nfs
));
623 Open(vlc_object_t
*p_obj
)
625 stream_t
*p_access
= (stream_t
*)p_obj
;
626 access_sys_t
*p_sys
= vlc_obj_calloc(p_obj
, 1, sizeof (*p_sys
));
628 if (unlikely(p_sys
== NULL
))
630 p_access
->p_sys
= p_sys
;
632 p_sys
->b_auto_guid
= var_InheritBool(p_obj
, "nfs-auto-guid");
634 /* nfs_* functions need a decoded url */
635 p_sys
->psz_url_decoded
= vlc_uri_decode_duplicate(p_access
->psz_url
);
636 if (p_sys
->psz_url_decoded
== NULL
)
639 /* Parse the encoded URL */
640 if (vlc_UrlParseFixup(&p_sys
->encoded_url
, p_access
->psz_url
) != 0)
642 if (p_sys
->encoded_url
.psz_option
)
644 if (strstr(p_sys
->encoded_url
.psz_option
, "uid")
645 || strstr(p_sys
->encoded_url
.psz_option
, "gid"))
646 p_sys
->b_auto_guid
= false;
649 if (NfsInit(p_access
, p_sys
->psz_url_decoded
) == -1)
652 if (p_sys
->p_nfs_url
->path
!= NULL
&& p_sys
->p_nfs_url
->file
!= NULL
)
654 /* The url has a valid path and file, mount the path and open/opendir
656 msg_Dbg(p_access
, "nfs_mount: server: '%s', path: '%s'",
657 p_sys
->p_nfs_url
->server
, p_sys
->p_nfs_url
->path
);
659 if (nfs_mount_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->server
,
660 p_sys
->p_nfs_url
->path
, nfs_mount_cb
, p_access
) < 0)
662 msg_Err(p_access
, "nfs_mount_async failed");
666 if (vlc_nfs_mainloop(p_access
, nfs_mount_open_finished_cb
) < 0)
669 if (p_sys
->psz_url_decoded_slash
!= NULL
)
671 /* Retry to mount by adding a '/' to the path, see comment in
673 nfs_destroy_url(p_sys
->p_nfs_url
);
674 nfs_destroy_context(p_sys
->p_nfs
);
675 p_sys
->p_nfs_url
= NULL
;
678 if (NfsInit(p_access
, p_sys
->psz_url_decoded_slash
) == -1
679 || p_sys
->p_nfs_url
->path
== NULL
|| p_sys
->p_nfs_url
->file
== NULL
)
682 if (nfs_mount_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->server
,
683 p_sys
->p_nfs_url
->path
, nfs_mount_cb
, p_access
) < 0)
685 msg_Err(p_access
, "nfs_mount_async failed");
689 if (vlc_nfs_mainloop(p_access
, nfs_mount_open_slash_finished_cb
) < 0)
693 if (p_sys
->p_nfsfh
!= NULL
)
695 p_access
->pf_read
= FileRead
;
696 p_access
->pf_seek
= FileSeek
;
697 p_access
->pf_control
= FileControl
;
699 else if (p_sys
->p_nfsdir
!= NULL
)
701 p_access
->pf_readdir
= DirRead
;
702 p_access
->pf_seek
= NULL
;
703 p_access
->pf_control
= access_vaDirectoryControlHelper
;
706 vlc_assert_unreachable();
710 /* url is just a server: fetch exports point */
711 nfs_destroy_context(p_sys
->p_nfs
);
714 p_sys
->p_mount
= rpc_init_context();
715 if (p_sys
->p_mount
== NULL
)
717 msg_Err(p_access
, "rpc_init_context failed");
721 p_sys
->res
.exports
.ppsz_names
= NULL
;
722 p_sys
->res
.exports
.i_count
= -1;
724 if (mount_getexports_async(p_sys
->p_mount
, p_sys
->p_nfs_url
->server
,
725 mount_export_cb
, p_access
) < 0)
727 msg_Err(p_access
, "mount_getexports_async failed");
731 if (vlc_mount_mainloop(p_access
, mount_getexports_finished_cb
) < 0)
734 p_access
->pf_readdir
= MountRead
;
735 p_access
->pf_seek
= NULL
;
736 p_access
->pf_control
= access_vaDirectoryControlHelper
;
747 Close(vlc_object_t
*p_obj
)
749 stream_t
*p_access
= (stream_t
*)p_obj
;
750 access_sys_t
*p_sys
= p_access
->p_sys
;
752 if (p_sys
->p_nfsfh
!= NULL
)
753 nfs_close(p_sys
->p_nfs
, p_sys
->p_nfsfh
);
755 if (p_sys
->p_nfsdir
!= NULL
)
756 nfs_closedir(p_sys
->p_nfs
, p_sys
->p_nfsdir
);
758 if (p_sys
->p_nfs
!= NULL
)
759 nfs_destroy_context(p_sys
->p_nfs
);
761 if (p_sys
->p_mount
!= NULL
)
763 for (int i
= 0; i
< p_sys
->res
.exports
.i_count
; ++i
)
764 free(p_sys
->res
.exports
.ppsz_names
[i
]);
765 free(p_sys
->res
.exports
.ppsz_names
);
766 rpc_destroy_context(p_sys
->p_mount
);
769 if (p_sys
->p_nfs_url
!= NULL
)
770 nfs_destroy_url(p_sys
->p_nfs_url
);
772 vlc_UrlClean(&p_sys
->encoded_url
);
774 free(p_sys
->psz_url_decoded
);
775 free(p_sys
->psz_url_decoded_slash
);