libshare: nfs: share nfs_copy_entries()

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 19:56:48 +02:00 committed by Brian Behlendorf
parent c53f2e9b50
commit 4e225e7316
4 changed files with 52 additions and 128 deletions

View File

@ -148,6 +148,57 @@ nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf)
return (SA_OK); 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)
{
int error = SA_OK;
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) {
if (buf[0] == '\n' || buf[0] == '#')
continue;
if ((sep = strpbrk(buf, "\t \n")) != NULL &&
sep - buf == mplen &&
strncmp(buf, mountpoint, mplen) == 0)
continue;
fputs(buf, newfp);
}
if (ferror(oldfp) != 0)
error = ferror(oldfp);
if (fclose(oldfp) != 0) {
fprintf(stderr, "Unable to close file %s: %s\n",
exports, strerror(errno));
error = error != SA_OK ? error : SA_SYSTEM_ERR;
}
free(buf);
}
if (error == SA_OK && ferror(newfp) != 0)
error = ferror(newfp);
return (error);
}
int int
nfs_toggle_share(const char *lockfile, const char *exports, nfs_toggle_share(const char *lockfile, const char *exports,
const char *expdir, sa_share_impl_t impl_share, const char *expdir, sa_share_impl_t impl_share,
@ -165,7 +216,7 @@ nfs_toggle_share(const char *lockfile, const char *exports,
return (error); return (error);
} }
error = nfs_copy_entries(tmpf.fp, impl_share->sa_mountpoint); error = nfs_copy_entries(tmpf.fp, exports, impl_share->sa_mountpoint);
if (error != SA_OK) if (error != SA_OK)
goto fullerr; goto fullerr;

View File

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

View File

@ -54,42 +54,6 @@ __FBSDID("$FreeBSD$");
static sa_fstype_t *nfs_fstype; static sa_fstype_t *nfs_fstype;
/*
* Read one line from a file. Skip comments, empty lines and a line with a
* mountpoint specified in the 'skip' argument.
*
* NOTE: This function returns a static buffer and thus is not thread-safe.
*/
static char *
zgetline(FILE *fd, const char *skip)
{
static char line[MAXLINESIZE];
size_t len, skiplen = 0;
char *s, last;
if (skip != NULL)
skiplen = strlen(skip);
for (;;) {
s = fgets(line, sizeof (line), fd);
if (s == NULL)
return (NULL);
/* 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[skiplen];
/* Skip the given mountpoint. */
if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
(last == '\t' || last == ' ' || last == '\0')) {
continue;
}
break;
}
return (line);
}
/* /*
* This function translate options to a format acceptable by exports(5), eg. * This function translate options to a format acceptable by exports(5), eg.
* *
@ -143,42 +107,6 @@ translate_opts(const char *shareopts)
return (newopts); return (newopts);
} }
/*
* This function copies all entries from the exports file to newfp,
* omitting any entries for the specified mountpoint.
*/
int
nfs_copy_entries(FILE *newfp, const char *mountpoint)
{
int error = SA_OK;
char *line;
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re");
fputs(FILE_HEADER, newfp);
/*
* The ZFS_EXPORTS_FILE may not exist yet. If that's the
* case then just write out the new file.
*/
if (oldfp != NULL) {
while ((line = zgetline(oldfp, mountpoint)) != NULL)
fprintf(newfp, "%s\n", line);
if (ferror(oldfp) != 0) {
error = ferror(oldfp);
}
if (fclose(oldfp) != 0) {
fprintf(stderr, "Unable to close file %s: %s\n",
ZFS_EXPORTS_FILE, strerror(errno));
error = error != 0 ? error : SA_SYSTEM_ERR;
}
}
if (error == SA_OK && ferror(newfp) != 0)
error = ferror(newfp);
return (error);
}
static int static int
nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
{ {

View File

@ -419,60 +419,6 @@ nfs_add_entry(FILE *tmpfile, const char *sharepath,
return (SA_OK); return (SA_OK);
} }
/*
* This function copies all entries from the exports file to newfp,
* omitting any entries for the specified mountpoint.
*/
int
nfs_copy_entries(FILE *newfp, const char *mountpoint)
{
char *buf = NULL;
size_t buflen = 0;
int error = SA_OK;
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re");
fputs(FILE_HEADER, newfp);
/*
* The ZFS_EXPORTS_FILE may not exist yet. If that's the
* case then just write out the new file.
*/
if (oldfp != NULL) {
while (getline(&buf, &buflen, oldfp) != -1) {
char *space = NULL;
if (buf[0] == '\n' || buf[0] == '#')
continue;
if ((space = strchr(buf, ' ')) != NULL) {
int mountpoint_len = strlen(mountpoint);
if (space - buf == mountpoint_len &&
strncmp(mountpoint, buf,
mountpoint_len) == 0) {
continue;
}
}
fputs(buf, newfp);
}
if (ferror(oldfp) != 0) {
error = ferror(oldfp);
}
if (fclose(oldfp) != 0) {
fprintf(stderr, "Unable to close file %s: %s\n",
ZFS_EXPORTS_FILE, strerror(errno));
error = error != 0 ? error : SA_SYSTEM_ERR;
}
}
if (error == SA_OK && ferror(newfp) != 0)
error = ferror(newfp);
free(buf);
return (error);
}
/* /*
* Enables NFS sharing for the specified share. * Enables NFS sharing for the specified share.
*/ */