summaryrefslogtreecommitdiffstats
path: root/support/junction/locations.c
blob: c577981b865db28c025c173c7ed31f3ef0eca022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
 * @file support/junction/locations.c
 * @brief Utility functions to manage NFS locations data
 */

/*
 * Copyright 2011, 2018 Oracle.  All rights reserved.
 *
 * This file is part of nfs-utils.
 *
 * nfs-utils is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2.0 as
 * published by the Free Software Foundation.
 *
 * nfs-utils is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License version 2.0 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2.0 along with nfs-utils.  If not, see:
 *
 *	http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "junction.h"

/**
 * Free an array of NUL-terminated C strings
 *
 * @param array array of pointers to C strings
 */
void
nfs_free_string_array(char **array)
{
	unsigned int i;

	if (array == NULL)
		return;
	for (i = 0; array[i] != NULL; i++)
		free(array[i]);
	free(array);
}

/**
 * Duplicate an array of NUL-terminated C strings
 *
 * @param array array of pointers to C strings
 * @return freshly allocated array of points to C strings, or NULL
 *
 * Caller must free the returned array with nfs_free_string_array()
 */
__attribute_malloc__ char **
nfs_dup_string_array(char **array)
{
	unsigned int size, i;
	char **result;

	if (array == NULL)
		return NULL;

	for (size = 0; array[size] != NULL; size++);

	result = calloc(size + 1, sizeof(char *));
	if (result == NULL)
		return NULL;
	for (i = 0; i < size; i++) {
		result[i] = strdup(array[i]);
		if (result[i] == NULL) {
			nfs_free_string_array(result);
			return NULL;
		}
	}
	return result;
}

/**
 * Free a single NFS location
 *
 * @param location pointer to nfs_fsloc data
 */
void
nfs_free_location(struct nfs_fsloc *location)
{
	nfs_free_string_array(location->nfl_rootpath);
	free(location->nfl_hostname);
	free(location);
}

/**
 * Free a list of NFS locations
 *
 * @param locations pointer to list of one or more locations
 */
void
nfs_free_locations(struct nfs_fsloc *locations)
{
	struct nfs_fsloc *fsloc;

	while (locations != NULL) {
		fsloc = locations;
		locations = fsloc->nfl_next;
		nfs_free_location(fsloc);
	}
}

/**
 * Allocate a fresh nfs_fsloc structure
 *
 * @return pointer to new empty nfs_fsloc data structure
 *
 * Caller must free returned locations with nfs_free_location().
 */
struct nfs_fsloc *
nfs_new_location(void)
{
	return calloc(1, sizeof(struct nfs_fsloc));
}