]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rmtab.c
See Changelog
[nfs-utils.git] / support / nfs / rmtab.c
1 /*
2  * support/nfs/rmtab.c
3  *
4  * Handling for rmtab.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include "config.h"
10
11 #include <sys/fcntl.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include "nfslib.h"
19
20 static FILE     *rmfp = NULL;
21
22 int
23 setrmtabent(char *type)
24 {
25         if (rmfp)
26                 fclose(rmfp);
27         rmfp = fsetrmtabent(_PATH_RMTAB, type);
28         return (rmfp != NULL);
29 }
30
31 FILE *
32 fsetrmtabent(char *fname, char *type)
33 {
34         int     readonly = !strcmp(type, "r");
35         FILE    *fp;
36
37         if (!fname)
38                 return NULL;
39         if ((fp = fopen(fname, type)) == NULL) {
40                 xlog(L_ERROR, "can't open %s for %sing", fname,
41                                 readonly ? "read" : "writ");
42                 return NULL;
43         }
44         return fp;
45 }
46
47 struct rmtabent *
48 getrmtabent(int log, long *pos)
49 {
50         return fgetrmtabent(rmfp, log, pos);
51 }
52
53 struct rmtabent *
54 fgetrmtabent(FILE *fp, int log, long *pos)
55 {
56         static struct rmtabent  re;
57         char    buf[2048], *count, *host, *path;
58
59         errno = 0;
60         if (!fp)
61                 return NULL;
62         do {
63                 if (pos)
64                         *pos = ftell (fp);
65                 if (fgets(buf, sizeof(buf)-1, fp) == NULL)
66                         return NULL;
67                 host = buf;
68                 if ((path = strchr(host, '\n')) != NULL)
69                         *path = '\0';
70                 if (!(path = strchr(host, ':'))) {
71                         if (log)
72                                 xlog(L_ERROR, "malformed entry in rmtab file");
73                         errno = EINVAL;
74                         return NULL;
75                 }
76                 *path++ = '\0';
77                 count = strchr(path, ':');
78                 if (count) {
79                         *count++ = '\0';
80                         re.r_count = strtol (count, NULL, 0);
81                 }
82                 else
83                         re.r_count = 1;
84         } while (0);
85         strncpy(re.r_client, host, sizeof (re.r_client) - 1);
86         re.r_client[sizeof (re.r_client) - 1] = '\0';
87         strncpy(re.r_path, path, sizeof (re.r_path) - 1);
88         re.r_path[sizeof (re.r_path) - 1] = '\0';
89         return &re;
90 }
91
92 void
93 putrmtabent(struct rmtabent *rep, long *pos)
94 {
95         fputrmtabent(rmfp, rep, pos);
96 }
97
98 void
99 fputrmtabent(FILE *fp, struct rmtabent *rep, long *pos)
100 {
101         if (!fp || (pos && fseek (fp, *pos, SEEK_SET) != 0))
102                 return;
103         fprintf(fp, "%s:%s:0x%.8x\n", rep->r_client, rep->r_path,
104                 rep->r_count);
105 }
106
107 void
108 endrmtabent(void)
109 {
110         fendrmtabent(rmfp);
111         rmfp = NULL;
112 }
113
114 void
115 fendrmtabent(FILE *fp)
116 {
117         if (fp) {
118                 /* If it was written to, we really want
119                  * to flush to disk before returning
120                  */
121                 fflush(fp);
122                 fdatasync(fileno(fp));
123                 fclose(fp);
124         }
125 }
126
127 void
128 rewindrmtabent(void)
129 {
130         if (rmfp)
131                 rewind(rmfp);
132 }
133
134 void
135 frewindrmtabent(FILE *fp)
136 {
137         if (fp)
138                 rewind (fp);
139 }