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", 2)
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)
268 FileControl(stream_t
*p_access
, int i_query
, va_list args
)
270 access_sys_t
*p_sys
= p_access
->p_sys
;
274 case STREAM_CAN_SEEK
:
275 *va_arg(args
, bool *) = true;
278 case STREAM_CAN_FASTSEEK
:
279 *va_arg(args
, bool *) = false;
282 case STREAM_CAN_PAUSE
:
283 case STREAM_CAN_CONTROL_PACE
:
284 *va_arg(args
, bool *) = true;
287 case STREAM_GET_SIZE
:
289 *va_arg(args
, uint64_t *) = p_sys
->stat
.nfs_size
;
293 case STREAM_GET_PTS_DELAY
:
294 *va_arg(args
, int64_t *) = var_InheritInteger(p_access
,
298 case STREAM_SET_PAUSE_STATE
:
308 NfsGetUrl(vlc_url_t
*p_url
, const char *psz_file
)
310 /* nfs://<psz_host><psz_path><psz_file>?<psz_option> */
312 if (asprintf(&psz_url
, "nfs://%s%s%s%s%s%s", p_url
->psz_host
,
313 p_url
->psz_path
!= NULL
? p_url
->psz_path
: "",
314 p_url
->psz_path
!= NULL
&& p_url
->psz_path
[0] != '\0' &&
315 p_url
->psz_path
[strlen(p_url
->psz_path
) - 1] != '/' ? "/" : "",
317 p_url
->psz_option
!= NULL
? "?" : "",
318 p_url
->psz_option
!= NULL
? p_url
->psz_option
: "") == -1)
325 DirRead(stream_t
*p_access
, input_item_node_t
*p_node
)
327 access_sys_t
*p_sys
= p_access
->p_sys
;
328 struct nfsdirent
*p_nfsdirent
;
329 int i_ret
= VLC_SUCCESS
;
330 assert(p_sys
->p_nfsdir
);
332 struct vlc_readdir_helper rdh
;
333 vlc_readdir_helper_init(&rdh
, p_access
, p_node
);
335 while (i_ret
== VLC_SUCCESS
336 && (p_nfsdirent
= nfs_readdir(p_sys
->p_nfs
, p_sys
->p_nfsdir
)) != NULL
)
338 char *psz_name_encoded
= vlc_uri_encode(p_nfsdirent
->name
);
339 if (psz_name_encoded
== NULL
)
344 char *psz_url
= NfsGetUrl(&p_sys
->encoded_url
, psz_name_encoded
);
345 free(psz_name_encoded
);
353 switch (p_nfsdirent
->type
)
356 i_type
= ITEM_TYPE_FILE
;
359 i_type
= ITEM_TYPE_DIRECTORY
;
362 i_type
= ITEM_TYPE_UNKNOWN
;
364 i_ret
= vlc_readdir_helper_additem(&rdh
, psz_url
, NULL
, p_nfsdirent
->name
,
369 vlc_readdir_helper_finish(&rdh
, i_ret
== VLC_SUCCESS
);
375 MountRead(stream_t
*p_access
, input_item_node_t
*p_node
)
377 access_sys_t
*p_sys
= p_access
->p_sys
;
378 assert(p_sys
->p_mount
!= NULL
&& p_sys
->res
.exports
.i_count
>= 0);
379 int i_ret
= VLC_SUCCESS
;
381 struct vlc_readdir_helper rdh
;
382 vlc_readdir_helper_init(&rdh
, p_access
, p_node
);
384 for (int i
= 0; i
< p_sys
->res
.exports
.i_count
&& i_ret
== VLC_SUCCESS
; ++i
)
386 char *psz_name
= p_sys
->res
.exports
.ppsz_names
[i
];
388 char *psz_url
= NfsGetUrl(&p_sys
->encoded_url
, psz_name
);
394 i_ret
= vlc_readdir_helper_additem(&rdh
, psz_url
, NULL
, psz_name
,
395 ITEM_TYPE_DIRECTORY
, ITEM_NET
);
399 vlc_readdir_helper_finish(&rdh
, i_ret
== VLC_SUCCESS
);
405 nfs_opendir_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
406 void *p_private_data
)
409 stream_t
*p_access
= p_private_data
;
410 access_sys_t
*p_sys
= p_access
->p_sys
;
411 assert(p_sys
->p_nfs
== p_nfs
);
412 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
415 p_sys
->p_nfsdir
= p_data
;
419 nfs_open_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
420 void *p_private_data
)
423 stream_t
*p_access
= p_private_data
;
424 access_sys_t
*p_sys
= p_access
->p_sys
;
425 assert(p_sys
->p_nfs
== p_nfs
);
426 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
429 p_sys
->p_nfsfh
= p_data
;
433 nfs_stat64_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
434 void *p_private_data
)
437 stream_t
*p_access
= p_private_data
;
438 access_sys_t
*p_sys
= p_access
->p_sys
;
439 assert(p_sys
->p_nfs
== p_nfs
);
440 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
443 struct nfs_stat_64
*p_stat
= p_data
;
444 p_sys
->stat
= *p_stat
;
446 if (p_sys
->b_auto_guid
)
448 nfs_set_uid(p_sys
->p_nfs
, p_sys
->stat
.nfs_uid
);
449 nfs_set_gid(p_sys
->p_nfs
, p_sys
->stat
.nfs_gid
);
452 if (S_ISDIR(p_sys
->stat
.nfs_mode
))
454 msg_Dbg(p_access
, "nfs_opendir: '%s'", p_sys
->p_nfs_url
->file
);
455 if (nfs_opendir_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->file
,
456 nfs_opendir_cb
, p_access
) != 0)
458 msg_Err(p_access
, "nfs_opendir_async failed");
459 p_sys
->b_error
= true;
462 else if (S_ISREG(p_sys
->stat
.nfs_mode
))
464 msg_Dbg(p_access
, "nfs_open: '%s'", p_sys
->p_nfs_url
->file
);
465 if (nfs_open_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->file
, O_RDONLY
,
466 nfs_open_cb
, p_access
) < 0)
468 msg_Err(p_access
, "nfs_open_async failed");
469 p_sys
->b_error
= true;
474 msg_Err(p_access
, "nfs_stat64_cb: file type not handled");
475 p_sys
->b_error
= true;
480 nfs_mount_cb(int i_status
, struct nfs_context
*p_nfs
, void *p_data
,
481 void *p_private_data
)
484 stream_t
*p_access
= p_private_data
;
485 access_sys_t
*p_sys
= p_access
->p_sys
;
486 assert(p_sys
->p_nfs
== p_nfs
);
489 /* If a directory url doesn't end with '/', there is no way to know which
490 * part of the url is the export point and which part is the path. An
491 * example with "nfs://myhost/mnt/data": we can't know if /mnt or /mnt/data
492 * is the export point. Therefore, in case of EACCES error, retry to mount
493 * the url by adding a '/' to the decoded path. */
494 if (i_status
== -EACCES
&& p_sys
->psz_url_decoded_slash
== NULL
)
497 vlc_UrlParse(&url
, p_sys
->psz_url_decoded
);
498 if (url
.psz_path
== NULL
|| url
.psz_path
[0] == '\0'
499 || url
.psz_path
[strlen(url
.psz_path
) - 1] == '/'
500 || (p_sys
->psz_url_decoded_slash
= NfsGetUrl(&url
, "/")) == NULL
)
503 NFS_CHECK_STATUS(p_access
, i_status
, p_data
);
509 msg_Warn(p_access
, "trying to mount '%s' again by adding a '/'",
515 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
518 if (nfs_stat64_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->file
, nfs_stat64_cb
,
521 msg_Err(p_access
, "nfs_stat64_async failed");
522 p_sys
->b_error
= true;
527 nfs_mount_open_finished_cb(stream_t
*p_access
)
529 access_sys_t
*p_sys
= p_access
->p_sys
;
530 return p_sys
->p_nfsfh
!= NULL
|| p_sys
->p_nfsdir
!= NULL
531 || p_sys
->psz_url_decoded_slash
!= NULL
;
535 nfs_mount_open_slash_finished_cb(stream_t
*p_access
)
537 access_sys_t
*p_sys
= p_access
->p_sys
;
538 return p_sys
->p_nfsfh
!= NULL
|| p_sys
->p_nfsdir
!= NULL
;
542 mount_export_cb(struct rpc_context
*p_ctx
, int i_status
, void *p_data
,
543 void *p_private_data
)
546 stream_t
*p_access
= p_private_data
;
547 access_sys_t
*p_sys
= p_access
->p_sys
;
548 assert(p_sys
->p_mount
== p_ctx
);
549 if (NFS_CHECK_STATUS(p_access
, i_status
, p_data
))
552 exports p_export
= *(exports
*)p_data
;
553 p_sys
->res
.exports
.i_count
= 0;
555 /* Dup the export linked list into an array of const char * */
556 while (p_export
!= NULL
)
558 p_sys
->res
.exports
.i_count
++;
559 p_export
= p_export
->ex_next
;
561 if (p_sys
->res
.exports
.i_count
== 0)
564 p_sys
->res
.exports
.ppsz_names
= calloc(p_sys
->res
.exports
.i_count
,
566 if (p_sys
->res
.exports
.ppsz_names
== NULL
)
568 p_sys
->b_error
= true;
572 p_export
= *(exports
*)p_data
;
573 unsigned int i_idx
= 0;
574 while (p_export
!= NULL
)
576 p_sys
->res
.exports
.ppsz_names
[i_idx
] = strdup(p_export
->ex_dir
);
577 if (p_sys
->res
.exports
.ppsz_names
[i_idx
] == NULL
)
579 for (unsigned int i
= 0; i
< i_idx
; ++i
)
580 free(p_sys
->res
.exports
.ppsz_names
[i
]);
581 free(p_sys
->res
.exports
.ppsz_names
);
582 p_sys
->res
.exports
.ppsz_names
= NULL
;
583 p_sys
->res
.exports
.i_count
= 0;
584 p_sys
->b_error
= true;
588 p_export
= p_export
->ex_next
;
593 mount_getexports_finished_cb(stream_t
*p_access
)
595 access_sys_t
*p_sys
= p_access
->p_sys
;
596 return p_sys
->res
.exports
.i_count
!= -1;
600 NfsInit(stream_t
*p_access
, const char *psz_url_decoded
)
602 access_sys_t
*p_sys
= p_access
->p_sys
;
603 p_sys
->p_nfs
= nfs_init_context();
604 if (p_sys
->p_nfs
== NULL
)
606 msg_Err(p_access
, "nfs_init_context failed");
610 p_sys
->p_nfs_url
= nfs_parse_url_incomplete(p_sys
->p_nfs
, psz_url_decoded
);
611 if (p_sys
->p_nfs_url
== NULL
|| p_sys
->p_nfs_url
->server
== NULL
)
613 msg_Err(p_access
, "nfs_parse_url_incomplete failed: '%s'",
614 nfs_get_error(p_sys
->p_nfs
));
621 Open(vlc_object_t
*p_obj
)
623 stream_t
*p_access
= (stream_t
*)p_obj
;
624 access_sys_t
*p_sys
= vlc_obj_calloc(p_obj
, 1, sizeof (*p_sys
));
626 if (unlikely(p_sys
== NULL
))
628 p_access
->p_sys
= p_sys
;
630 p_sys
->b_auto_guid
= var_InheritBool(p_obj
, "nfs-auto-guid");
632 /* nfs_* functions need a decoded url */
633 p_sys
->psz_url_decoded
= vlc_uri_decode_duplicate(p_access
->psz_url
);
634 if (p_sys
->psz_url_decoded
== NULL
)
637 /* Parse the encoded URL */
638 vlc_UrlParse(&p_sys
->encoded_url
, p_access
->psz_url
);
639 if (p_sys
->encoded_url
.psz_option
)
641 if (strstr(p_sys
->encoded_url
.psz_option
, "uid")
642 || strstr(p_sys
->encoded_url
.psz_option
, "gid"))
643 p_sys
->b_auto_guid
= false;
646 if (NfsInit(p_access
, p_sys
->psz_url_decoded
) == -1)
649 if (p_sys
->p_nfs_url
->path
!= NULL
&& p_sys
->p_nfs_url
->file
!= NULL
)
651 /* The url has a valid path and file, mount the path and open/opendir
653 msg_Dbg(p_access
, "nfs_mount: server: '%s', path: '%s'",
654 p_sys
->p_nfs_url
->server
, p_sys
->p_nfs_url
->path
);
656 if (nfs_mount_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->server
,
657 p_sys
->p_nfs_url
->path
, nfs_mount_cb
, p_access
) < 0)
659 msg_Err(p_access
, "nfs_mount_async failed");
663 if (vlc_nfs_mainloop(p_access
, nfs_mount_open_finished_cb
) < 0)
666 if (p_sys
->psz_url_decoded_slash
!= NULL
)
668 /* Retry to mount by adding a '/' to the path, see comment in
670 nfs_destroy_url(p_sys
->p_nfs_url
);
671 nfs_destroy_context(p_sys
->p_nfs
);
672 p_sys
->p_nfs_url
= NULL
;
675 if (NfsInit(p_access
, p_sys
->psz_url_decoded_slash
) == -1
676 || p_sys
->p_nfs_url
->path
== NULL
|| p_sys
->p_nfs_url
->file
== NULL
)
679 if (nfs_mount_async(p_sys
->p_nfs
, p_sys
->p_nfs_url
->server
,
680 p_sys
->p_nfs_url
->path
, nfs_mount_cb
, p_access
) < 0)
682 msg_Err(p_access
, "nfs_mount_async failed");
686 if (vlc_nfs_mainloop(p_access
, nfs_mount_open_slash_finished_cb
) < 0)
690 if (p_sys
->p_nfsfh
!= NULL
)
692 p_access
->pf_read
= FileRead
;
693 p_access
->pf_seek
= FileSeek
;
694 p_access
->pf_control
= FileControl
;
696 else if (p_sys
->p_nfsdir
!= NULL
)
698 p_access
->pf_readdir
= DirRead
;
699 p_access
->pf_seek
= NULL
;
700 p_access
->pf_control
= access_vaDirectoryControlHelper
;
703 vlc_assert_unreachable();
707 /* url is just a server: fetch exports point */
708 nfs_destroy_context(p_sys
->p_nfs
);
711 p_sys
->p_mount
= rpc_init_context();
712 if (p_sys
->p_mount
== NULL
)
714 msg_Err(p_access
, "rpc_init_context failed");
718 p_sys
->res
.exports
.ppsz_names
= NULL
;
719 p_sys
->res
.exports
.i_count
= -1;
721 if (mount_getexports_async(p_sys
->p_mount
, p_sys
->p_nfs_url
->server
,
722 mount_export_cb
, p_access
) < 0)
724 msg_Err(p_access
, "mount_getexports_async failed");
728 if (vlc_mount_mainloop(p_access
, mount_getexports_finished_cb
) < 0)
731 p_access
->pf_readdir
= MountRead
;
732 p_access
->pf_seek
= NULL
;
733 p_access
->pf_control
= access_vaDirectoryControlHelper
;
744 Close(vlc_object_t
*p_obj
)
746 stream_t
*p_access
= (stream_t
*)p_obj
;
747 access_sys_t
*p_sys
= p_access
->p_sys
;
749 if (p_sys
->p_nfsfh
!= NULL
)
750 nfs_close(p_sys
->p_nfs
, p_sys
->p_nfsfh
);
752 if (p_sys
->p_nfsdir
!= NULL
)
753 nfs_closedir(p_sys
->p_nfs
, p_sys
->p_nfsdir
);
755 if (p_sys
->p_nfs
!= NULL
)
756 nfs_destroy_context(p_sys
->p_nfs
);
758 if (p_sys
->p_mount
!= NULL
)
760 for (int i
= 0; i
< p_sys
->res
.exports
.i_count
; ++i
)
761 free(p_sys
->res
.exports
.ppsz_names
[i
]);
762 free(p_sys
->res
.exports
.ppsz_names
);
763 rpc_destroy_context(p_sys
->p_mount
);
766 if (p_sys
->p_nfs_url
!= NULL
)
767 nfs_destroy_url(p_sys
->p_nfs_url
);
769 vlc_UrlClean(&p_sys
->encoded_url
);
771 free(p_sys
->psz_url_decoded
);
772 free(p_sys
->psz_url_decoded_slash
);