diff --git a/lib/libshare/nfs.c b/lib/libshare/nfs.c index e7037b104..4bc7e7779 100644 --- a/lib/libshare/nfs.c +++ b/lib/libshare/nfs.c @@ -148,6 +148,57 @@ 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) +{ + 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 nfs_toggle_share(const char *lockfile, const char *exports, const char *expdir, sa_share_impl_t impl_share, @@ -165,7 +216,7 @@ nfs_toggle_share(const char *lockfile, const char *exports, 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) goto fullerr; diff --git a/lib/libshare/nfs.h b/lib/libshare/nfs.h index 370c409be..63838032b 100644 --- a/lib/libshare/nfs.h +++ b/lib/libshare/nfs.h @@ -30,7 +30,6 @@ void libshare_nfs_init(void); -int nfs_copy_entries(FILE *tmpfile, const char *mountpoint); 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)); diff --git a/lib/libshare/os/freebsd/nfs.c b/lib/libshare/os/freebsd/nfs.c index 082029838..ba98a6472 100644 --- a/lib/libshare/os/freebsd/nfs.c +++ b/lib/libshare/os/freebsd/nfs.c @@ -54,42 +54,6 @@ __FBSDID("$FreeBSD$"); 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. * @@ -143,42 +107,6 @@ translate_opts(const char *shareopts) 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 nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) { diff --git a/lib/libshare/os/linux/nfs.c b/lib/libshare/os/linux/nfs.c index bf3729141..7f8ebd7f6 100644 --- a/lib/libshare/os/linux/nfs.c +++ b/lib/libshare/os/linux/nfs.c @@ -419,60 +419,6 @@ nfs_add_entry(FILE *tmpfile, const char *sharepath, 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. */