~ubuntu-branches/ubuntu/trusty/nilfs-tools/trusty

« back to all changes in this revision

Viewing changes to sbin/mount/mount_attrs.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2011-08-12 20:42:38 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20110812204238-3i5mnvvmg7amtyjm
Tags: upstream-2.1.0~rc2
ImportĀ upstreamĀ versionĀ 2.1.0~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * mount_attrs.c - NILFS mount attribute functions for libmount version
 
3
 *
 
4
 * Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public
 
17
 * License along with this program; if not, write to the
 
18
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
 * Boston, MA 021110-1307, USA.
 
20
 *
 
21
 * Written by Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include "config.h"
 
26
#endif  /* HAVE_CONFIG_H */
 
27
 
 
28
#if HAVE_SYS_TYPES_H
 
29
#include <sys/types.h>
 
30
#endif  /* HAVE_SYS_TYPES_H */
 
31
 
 
32
#include <stdio.h>
 
33
 
 
34
#if HAVE_STDLIB_H
 
35
#include <stdlib.h>
 
36
#endif  /* HAVE_STDLIB_H */
 
37
 
 
38
#if HAVE_STRING_H
 
39
#include <string.h>
 
40
#endif  /* HAVE_STRING_H */
 
41
 
 
42
#if HAVE_LIBMOUNT_LIBMOUNT_H
 
43
#include <libmount/libmount.h>
 
44
#endif  /* HAVE_LIBMOUNT_H */
 
45
 
 
46
#include <stdarg.h>
 
47
#include <errno.h>
 
48
#include <assert.h>
 
49
 
 
50
#include "sundries.h"
 
51
#include "mount.nilfs2.h"
 
52
#include "mount_attrs.h"
 
53
#include "cleaner_exec.h"       /* PIDOPT_NAME */
 
54
#include "nls.h"
 
55
 
 
56
extern char *progname;
 
57
 
 
58
void nilfs_mount_attrs_init(struct nilfs_mount_attrs *mattrs)
 
59
{
 
60
        memset(mattrs, 0, sizeof(*mattrs));
 
61
        mattrs->pp = ULONG_MAX; /* no protection period */
 
62
}
 
63
 
 
64
int nilfs_mount_attrs_parse(struct nilfs_mount_attrs *mattrs,
 
65
                            const char *optstr, char **found, char **rest,
 
66
                            int mtab)
 
67
{
 
68
        char *name, *val, *str, *p, *endptr, *prev = NULL;
 
69
        size_t namesz, valsz;
 
70
        int res;
 
71
 
 
72
        if (rest)
 
73
                *rest = NULL;
 
74
        if (found)
 
75
                *found = NULL;
 
76
 
 
77
        str = strdup(optstr);
 
78
        if (!str)
 
79
                return -1;
 
80
 
 
81
        p = str;
 
82
        while (!mnt_optstr_next_option(&p, &name, &namesz, &val, &valsz)) {
 
83
                if (!strncmp(name, PPOPT_NAME, namesz)) {
 
84
                        if (!val || valsz == 0)
 
85
                                goto out_inval;
 
86
                        
 
87
                        mattrs->pp = strtoul(val, &endptr, 10);
 
88
                        if (endptr != val + valsz)
 
89
                                goto out_inval;
 
90
 
 
91
                } else if (!strncmp(name, NOGCOPT_NAME, namesz)) {
 
92
                        if (val)
 
93
                                goto out_inval;
 
94
 
 
95
                        mattrs->nogc = 1;
 
96
 
 
97
                } else if (!strncmp(name, PIDOPT_NAME, namesz)) {
 
98
                        if (!val || valsz == 0 || !mtab)
 
99
                                goto out_inval;
 
100
 
 
101
                        mattrs->gcpid = strtoul(val, &endptr, 10);
 
102
                        if (endptr != val + valsz)
 
103
                                goto out_inval;
 
104
 
 
105
                } else if (!strncmp(name, NOATTR_NAME, namesz)) {
 
106
                        if (val || !mtab)
 
107
                                goto out_inval;
 
108
                        /* ignore */
 
109
                } else {
 
110
                        if (!prev)
 
111
                                prev = name;
 
112
                        continue;
 
113
                }
 
114
 
 
115
                if (prev && rest) {
 
116
                        *(name - 1) = '\0';
 
117
                        res = mnt_optstr_append_option(rest, prev, NULL);
 
118
                        if (res < 0)
 
119
                                goto failed;
 
120
                        prev = NULL;
 
121
                }
 
122
                if (found) {
 
123
                        name[namesz] = '\0';
 
124
                        if (val)
 
125
                                val[valsz] = '\0';
 
126
                        res = mnt_optstr_append_option(found, name, val);
 
127
                        if (res < 0)
 
128
                                goto failed;
 
129
                }
 
130
        }
 
131
 
 
132
        if (prev && rest) {
 
133
                res = mnt_optstr_append_option(rest, prev, NULL);
 
134
                if (res < 0)
 
135
                        goto failed;
 
136
        }
 
137
 
 
138
        free(str);
 
139
        return 0;
 
140
 
 
141
out_inval:
 
142
        error(_("%s: invalid options (%s)."), progname, optstr);
 
143
        res = -1;
 
144
failed:
 
145
        if (rest) {
 
146
                free(*rest);
 
147
                *rest = NULL;
 
148
        }
 
149
        if (found) {
 
150
                free(*found);
 
151
                *found = NULL;
 
152
        }
 
153
 
 
154
        free(str);
 
155
 
 
156
        return res;
 
157
}
 
158
 
 
159
void nilfs_mount_attrs_update(struct nilfs_mount_attrs *old_attrs,
 
160
                              struct nilfs_mount_attrs *new_attrs,
 
161
                              struct libmnt_context *cxt)
 
162
{
 
163
        struct libmnt_fs *fs;
 
164
 
 
165
        if (!new_attrs && !old_attrs)
 
166
                return;
 
167
 
 
168
        fs = mnt_context_get_fs(cxt);
 
169
        mnt_fs_set_attributes(fs, NULL);
 
170
        if (!new_attrs)
 
171
                return;
 
172
 
 
173
        if (new_attrs->nogc) {
 
174
                mnt_fs_append_attributes(fs, NOGCOPT_NAME);
 
175
        } else if (new_attrs->gcpid) {
 
176
                char abuf[100];
 
177
 
 
178
                snprintf(abuf, sizeof(abuf), PIDOPT_NAME "=%lu",
 
179
                         (unsigned long)new_attrs->gcpid);
 
180
                mnt_fs_append_attributes(fs, abuf);
 
181
                if (new_attrs->pp != ULONG_MAX) {
 
182
                        snprintf(abuf, sizeof(abuf), PPOPT_NAME "=%lu",
 
183
                                 new_attrs->pp);
 
184
                        mnt_fs_append_attributes(fs, abuf);
 
185
                }
 
186
        } else if (old_attrs) {
 
187
                /*
 
188
                 * The following dummy attribute is required to handle
 
189
                 * removal of attributes on remount.
 
190
                 */
 
191
                mnt_fs_set_attributes(fs, NOATTR_NAME);
 
192
        }
 
193
}