aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libshare/nfs.c
diff options
context:
space:
mode:
authorнаб <[email protected]>2021-05-17 20:25:29 +0200
committerBrian Behlendorf <[email protected]>2021-12-17 12:54:04 -0800
commit605e03e51a8baeb90c4194c511e85e3663d4a78d (patch)
tree61f67517887516e4afcb0ace6421e37629b127d4 /lib/libshare/nfs.c
parent4e225e7316d5e65dcfd2694e79930400740b7260 (diff)
libshare: nfs: share nfs_is_shared()
Reviewed-by: Don Brady <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: John Kennedy <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes #12067
Diffstat (limited to 'lib/libshare/nfs.c')
-rw-r--r--lib/libshare/nfs.c70
1 files changed, 50 insertions, 20 deletions
diff --git a/lib/libshare/nfs.c b/lib/libshare/nfs.c
index 4bc7e7779..79b822fbc 100644
--- a/lib/libshare/nfs.c
+++ b/lib/libshare/nfs.c
@@ -148,38 +148,29 @@ nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf)
return (SA_OK);
}
-/*
- * Copy all entries from the exports file to newfp,
- * omitting any entries for the specified mountpoint.
- */
static int
-nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint)
+nfs_process_exports(const char *exports, const char *mountpoint,
+ boolean_t (*cbk)(void *userdata, char *line, boolean_t found_mountpoint),
+ void *userdata)
{
int error = SA_OK;
+ boolean_t cont = B_TRUE;
- fputs(FILE_HEADER, newfp);
-
- /*
- * ZFS_EXPORTS_FILE may not exist yet.
- * If that's the case, then just write out the new file.
- */
FILE *oldfp = fopen(exports, "re");
if (oldfp != NULL) {
char *buf = NULL, *sep;
size_t buflen = 0, mplen = strlen(mountpoint);
- while (getline(&buf, &buflen, oldfp) != -1) {
-
+ while (cont && getline(&buf, &buflen, oldfp) != -1) {
if (buf[0] == '\n' || buf[0] == '#')
continue;
- if ((sep = strpbrk(buf, "\t \n")) != NULL &&
+ cont = cbk(userdata, buf,
+ (sep = strpbrk(buf, "\t \n")) != NULL &&
sep - buf == mplen &&
- strncmp(buf, mountpoint, mplen) == 0)
- continue;
-
- fputs(buf, newfp);
+ strncmp(buf, mountpoint, mplen) == 0);
}
+ free(buf);
if (ferror(oldfp) != 0)
error = ferror(oldfp);
@@ -189,10 +180,32 @@ nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint)
exports, strerror(errno));
error = error != SA_OK ? error : SA_SYSTEM_ERR;
}
-
- free(buf);
}
+ return (error);
+}
+
+static boolean_t
+nfs_copy_entries_cb(void *userdata, char *line, boolean_t found_mountpoint)
+{
+ FILE *newfp = userdata;
+ if (!found_mountpoint)
+ fputs(line, newfp);
+ return (B_TRUE);
+}
+
+/*
+ * Copy all entries from the exports file (if it exists) to newfp,
+ * omitting any entries for the specified mountpoint.
+ */
+static int
+nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint)
+{
+ fputs(FILE_HEADER, newfp);
+
+ int error = nfs_process_exports(
+ exports, mountpoint, nfs_copy_entries_cb, newfp);
+
if (error == SA_OK && ferror(newfp) != 0)
error = ferror(newfp);
@@ -233,3 +246,20 @@ fullerr:
nfs_exports_unlock(lockfile);
return (error);
}
+
+static boolean_t
+nfs_is_shared_cb(void *userdata, char *line, boolean_t found_mountpoint)
+{
+ boolean_t *found = userdata;
+ *found = found_mountpoint;
+ return (!found_mountpoint);
+}
+
+boolean_t
+nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share)
+{
+ boolean_t found = B_FALSE;
+ nfs_process_exports(exports, impl_share->sa_mountpoint,
+ nfs_is_shared_cb, &found);
+ return (found);
+}