libshare: nfs: share nfs_is_shared()

Reviewed-by: Don Brady <don.brady@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12067
This commit is contained in:
наб 2021-05-17 20:25:29 +02:00 committed by Brian Behlendorf
parent 4e225e7316
commit 605e03e51a
4 changed files with 53 additions and 74 deletions

View File

@ -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);
}

View File

@ -30,6 +30,7 @@
void libshare_nfs_init(void);
boolean_t nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share);
int nfs_toggle_share(const char *lockfile, const char *exports,
const char *expdir, sa_share_impl_t impl_share,
int(*cbk)(sa_share_impl_t impl_share, FILE *tmpfile));

View File

@ -148,35 +148,7 @@ nfs_disable_share(sa_share_impl_t impl_share)
static boolean_t
nfs_is_shared(sa_share_impl_t impl_share)
{
char *s, last, line[MAXLINESIZE];
size_t len;
char *mntpoint = impl_share->sa_mountpoint;
size_t mntlen = strlen(mntpoint);
FILE *fp = fopen(ZFS_EXPORTS_FILE, "re");
if (fp == NULL)
return (B_FALSE);
for (;;) {
s = fgets(line, sizeof (line), fp);
if (s == NULL)
return (B_FALSE);
/* Skip empty lines and comments. */
if (line[0] == '\n' || line[0] == '#')
continue;
len = strlen(line);
if (line[len - 1] == '\n')
line[len - 1] = '\0';
last = line[mntlen];
/* Skip the given mountpoint. */
if (strncmp(mntpoint, line, mntlen) == 0 &&
(last == '\t' || last == ' ' || last == '\0')) {
fclose(fp);
return (B_TRUE);
}
}
fclose(fp);
return (B_FALSE);
return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
}
static int

View File

@ -467,31 +467,7 @@ nfs_disable_share(sa_share_impl_t impl_share)
static boolean_t
nfs_is_shared(sa_share_impl_t impl_share)
{
size_t buflen = 0;
char *buf = NULL;
FILE *fp = fopen(ZFS_EXPORTS_FILE, "re");
if (fp == NULL) {
return (B_FALSE);
}
while ((getline(&buf, &buflen, fp)) != -1) {
char *space = NULL;
if ((space = strchr(buf, ' ')) != NULL) {
int mountpoint_len = strlen(impl_share->sa_mountpoint);
if (space - buf == mountpoint_len &&
strncmp(impl_share->sa_mountpoint, buf,
mountpoint_len) == 0) {
fclose(fp);
free(buf);
return (B_TRUE);
}
}
}
free(buf);
fclose(fp);
return (B_FALSE);
return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
}
/*