summaryrefslogtreecommitdiffstats
path: root/utils/nfsref/remove.c
blob: 1a4e371cf8144fffe51814c54d1b1309f507a64a (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * @file utils/nfsref/remove.c
 * @brief Remove junction metadata from a local file system object
 */

/*
 * 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 <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

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

#include "junction.h"
#include "xlog.h"
#include "nfsref.h"

/**
 * Display help message for "remove" subcommand
 *
 * @param progname NUL-terminated C string containing name of program
 * @return program exit status
 */
int
nfsref_remove_help(const char *progname)
{
	fprintf(stderr, " \n");

	fprintf(stderr, "Usage: %s [ -t type ] remove <junction path>\n\n",
		progname);

	fprintf(stderr, "Remove the junction at <junction path>.  For FedFS "
			"junctions, FSL and FSN\n");
	fprintf(stderr, "records are removed from the NSDB.\n");

	return EXIT_SUCCESS;
}

/**
 * Remove an NFS locations-style junction
 *
 * @param junct_path NUL-terminated C string containing pathname of junction
 * @return program exit status
 */
static int
nfsref_remove_nfs_basic(const char *junct_path)
{
	int status = EXIT_FAILURE;
	FedFsStatus retval;

	xlog(D_GENERAL, "%s: Removing FedFS junction from %s",
		__func__, junct_path);

	retval = nfs_delete_junction(junct_path);
	switch (retval) {
	case FEDFS_OK:
		printf("Removed nfs-basic junction from %s\n", junct_path);
		status = EXIT_SUCCESS;
		break;
	case FEDFS_ERR_NOTJUNCT:
		xlog(L_ERROR, "%s is not an nfs-basic junction", junct_path);
		break;
	default:
		xlog(L_ERROR, "Failed to delete %s: %s",
			junct_path, nsdb_display_fedfsstatus(retval));
	}

	return status;
}

/**
 * Remove any NFS junction information
 *
 * @param junct_path NUL-terminated C string containing pathname of junction
 * @return program exit status
 */
static int
nfsref_remove_unspecified(const char *junct_path)
{
	FedFsStatus retval;

	xlog(D_GENERAL, "%s: Removing junction from %s",
		__func__, junct_path);

	retval = nfs_delete_junction(junct_path);
	if (retval != FEDFS_OK) {
		if (retval != FEDFS_ERR_NOTJUNCT)
			goto out_err;
	}

	printf("Removed junction from %s\n", junct_path);
	return EXIT_SUCCESS;

out_err:
	switch (retval) {
	case FEDFS_ERR_NOTJUNCT:
		xlog(L_ERROR, "No junction information found in %s", junct_path);
		break;
	default:
		xlog(L_ERROR, "Failed to delete %s: %s",
			junct_path, nsdb_display_fedfsstatus(retval));
	}
	return EXIT_FAILURE;
}

/**
 * Remove an NFS junction
 *
 * @param type type of junction to add
 * @param junct_path NUL-terminated C string containing pathname of junction
 * @return program exit status
 */
int
nfsref_remove(enum nfsref_type type, const char *junct_path)
{
	switch (type) {
	case NFSREF_TYPE_UNSPECIFIED:
		return nfsref_remove_unspecified(junct_path);
	case NFSREF_TYPE_NFS_BASIC:
		return nfsref_remove_nfs_basic(junct_path);
	default:
		xlog(L_ERROR, "Unrecognized junction type");
	}
	return EXIT_FAILURE;
}