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
|
/*
* support/nfs/rmtab.c
*
* Handling for rmtab.
*
* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
*/
#include "config.h"
#include <sys/fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "nfslib.h"
static FILE *rmfp = NULL;
int
setrmtabent(char *type)
{
if (rmfp)
fclose(rmfp);
rmfp = fsetrmtabent(_PATH_RMTAB, type);
return (rmfp != NULL);
}
FILE *
fsetrmtabent(char *fname, char *type)
{
int readonly = !strcmp(type, "r");
FILE *fp;
if (!fname)
return NULL;
if ((fp = fopen(fname, type)) == NULL) {
xlog(L_ERROR, "can't open %s for %sing", fname,
readonly ? "read" : "writ");
return NULL;
}
return fp;
}
struct rmtabent *
getrmtabent(int log, long *pos)
{
return fgetrmtabent(rmfp, log, pos);
}
struct rmtabent *
fgetrmtabent(FILE *fp, int log, long *pos)
{
static struct rmtabent re;
char buf[2048], *count, *host, *path;
errno = 0;
if (!fp)
return NULL;
do {
if (pos)
*pos = ftell (fp);
if (fgets(buf, sizeof(buf)-1, fp) == NULL)
return NULL;
host = buf;
if ((path = strchr(host, '\n')) != NULL)
*path = '\0';
if (!(path = strchr(host, ':'))) {
if (log)
xlog(L_ERROR, "malformed entry in rmtab file");
errno = EINVAL;
return NULL;
}
*path++ = '\0';
count = strchr(path, ':');
if (count) {
*count++ = '\0';
re.r_count = strtol (count, NULL, 0);
}
else
re.r_count = 1;
} while (0);
strncpy(re.r_client, host, sizeof (re.r_client) - 1);
re.r_client[sizeof (re.r_client) - 1] = '\0';
strncpy(re.r_path, path, sizeof (re.r_path) - 1);
re.r_path[sizeof (re.r_path) - 1] = '\0';
return &re;
}
void
putrmtabent(struct rmtabent *rep, long *pos)
{
fputrmtabent(rmfp, rep, pos);
}
void
fputrmtabent(FILE *fp, struct rmtabent *rep, long *pos)
{
if (!fp || (pos && fseek (fp, *pos, SEEK_SET) != 0))
return;
fprintf(fp, "%s:%s:0x%.8x\n", rep->r_client, rep->r_path,
rep->r_count);
}
void
endrmtabent(void)
{
fendrmtabent(rmfp);
rmfp = NULL;
}
void
fendrmtabent(FILE *fp)
{
if (fp) {
/* If it was written to, we really want
* to flush to disk before returning
*/
fflush(fp);
fdatasync(fileno(fp));
fclose(fp);
}
}
void
rewindrmtabent(void)
{
if (rmfp)
rewind(rmfp);
}
void
frewindrmtabent(FILE *fp)
{
if (fp)
rewind (fp);
}
|