Skip to content

Commit

Permalink
Move nfs.c:foreach_nfs_shareopt() to libshare.c:foreach_shareopt()
Browse files Browse the repository at this point in the history
so that it can be (re)used in other parts of libshare.
  • Loading branch information
FransUrbo committed Apr 20, 2015
1 parent de6d197 commit 99aa4d2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 65 deletions.
59 changes: 59 additions & 0 deletions lib/libshare/libshare.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,65 @@ static int update_zfs_shares(sa_handle_impl_t impl_handle, const char *proto);
static int fstypes_count;
static sa_fstype_t *fstypes;

/*
* Invokes the specified callback function for each Solaris share option
* listed in the specified string.
*/
int
foreach_shareopt(const char *shareopts,
nfs_shareopt_callback_t callback, void *cookie)
{
char *shareopts_dup, *opt, *cur, *value;
int was_nul, rc;

if (shareopts == NULL)
return (SA_OK);

shareopts_dup = strdup(shareopts);
if (shareopts_dup == NULL)
return (SA_NO_MEMORY);

opt = shareopts_dup;
was_nul = 0;

while (1) {
cur = opt;

while (*cur != ',' && *cur != '\0')
cur++;

if (*cur == '\0')
was_nul = 1;

*cur = '\0';

if (cur > opt) {
value = strchr(opt, '=');

if (value != NULL) {
*value = '\0';
value++;
}

rc = callback(opt, value, cookie);

if (rc != SA_OK) {
free(shareopts_dup);
return (rc);
}
}

opt = cur + 1;

if (was_nul)
break;
}

free(shareopts_dup);

return (SA_OK);
}

sa_fstype_t *
register_fstype(const char *name, const sa_share_ops_t *ops)
{
Expand Down
3 changes: 3 additions & 0 deletions lib/libshare/libshare_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ typedef struct sa_handle_impl {
sa_share_impl_t shares;
} *sa_handle_impl_t;

typedef int (*nfs_shareopt_callback_t)(const char *, const char *, void *);

sa_fstype_t *register_fstype(const char *name, const sa_share_ops_t *ops);
int foreach_shareopt(const char *, nfs_shareopt_callback_t, void *);
67 changes: 2 additions & 65 deletions lib/libshare/nfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,72 +44,9 @@ static sa_fstype_t *nfs_fstype;
*/
static int nfs_exportfs_temp_fd = -1;

typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
void *cookie);

typedef int (*nfs_host_callback_t)(const char *sharepath, const char *host,
const char *security, const char *access, void *cookie);

/*
* Invokes the specified callback function for each Solaris share option
* listed in the specified string.
*/
static int
foreach_nfs_shareopt(const char *shareopts,
nfs_shareopt_callback_t callback, void *cookie)
{
char *shareopts_dup, *opt, *cur, *value;
int was_nul, rc;

if (shareopts == NULL)
return (SA_OK);

shareopts_dup = strdup(shareopts);

if (shareopts_dup == NULL)
return (SA_NO_MEMORY);

opt = shareopts_dup;
was_nul = 0;

while (1) {
cur = opt;

while (*cur != ',' && *cur != '\0')
cur++;

if (*cur == '\0')
was_nul = 1;

*cur = '\0';

if (cur > opt) {
value = strchr(opt, '=');

if (value != NULL) {
*value = '\0';
value++;
}

rc = callback(opt, value, cookie);

if (rc != SA_OK) {
free(shareopts_dup);
return (rc);
}
}

opt = cur + 1;

if (was_nul)
break;
}

free(shareopts_dup);

return (0);
}

typedef struct nfs_host_cookie_s {
nfs_host_callback_t callback;
const char *sharepath;
Expand All @@ -118,7 +55,7 @@ typedef struct nfs_host_cookie_s {
} nfs_host_cookie_t;

/*
* Helper function for foreach_nfs_host. This function checks whether the
* Helper function for foreach_host. This function checks whether the
* current share option is a host specification and invokes a callback
* function with information about the host.
*/
Expand Down Expand Up @@ -390,7 +327,7 @@ get_linux_shareopts(const char *shareopts, char **plinux_opts)
(void) add_linux_shareopt(plinux_opts, "no_root_squash", NULL);
(void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);

rc = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
rc = foreach_shareopt(shareopts, get_linux_shareopts_cb,
plinux_opts);

if (rc != SA_OK) {
Expand Down

0 comments on commit 99aa4d2

Please sign in to comment.