Skip to content

Instantly share code, notes, and snippets.

@furandon-pig
Created December 15, 2018 14:18
Show Gist options
  • Save furandon-pig/66ae7b2479693d6a3d85ca021092e8f6 to your computer and use it in GitHub Desktop.
Save furandon-pig/66ae7b2479693d6a3d85ca021092e8f6 to your computer and use it in GitHub Desktop.
NetBSDのpuffsでlsしたときに今日の日付を返すサンプルです。
/* $NetBSD: datefs.c,v 1.18 2008/11/26 14:03:48 pooka Exp $ */
/*
* Copyright (c) 2007 Antti Kantee. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* datefs: puffs nullfs example
*/
#include <err.h>
#include <puffs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
static void usage(void);
PUFFSOP_PROTOS(datefs)
static void
usage()
{
errx(1, "usage: %s [-s] [-o mntopts] nullpath mountpath",
getprogname());
}
int
main(int argc, char *argv[])
{
struct puffs_usermount *pu;
struct puffs_ops *pops;
struct puffs_pathobj *po_root;
struct puffs_node *node;
struct stat sb;
mntoptparse_t mp;
int mntflags, pflags;
int ch;
int detach;
setprogname(argv[0]);
if (argc < 3)
usage();
pflags = mntflags = 0;
detach = 1;
while ((ch = getopt(argc, argv, "o:s")) != -1) {
switch (ch) {
case 'o':
mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
if (mp == NULL)
err(1, "getmntopts");
freemntopts(mp);
break;
case 's':
detach = 0;
break;
}
}
pflags |= PUFFS_FLAG_BUILDPATH;
argv += optind;
argc -= optind;
if (pflags & PUFFS_FLAG_OPDUMP)
detach = 0;
if (argc != 2)
usage();
if (lstat(argv[0], &sb) == -1)
err(1, "stat %s", argv[0]);
if ((sb.st_mode & S_IFDIR) == 0)
errx(1, "%s is not a directory", argv[0]);
PUFFSOP_INIT(pops);
puffs_null_setops(pops);
PUFFSOP_SET(pops, datefs, node, readdir);
PUFFSOP_SET(pops, datefs, node, read);
PUFFSOP_SET(pops, datefs, node, write);
if ((pu = puffs_init(pops, argv[0], "datefs", NULL, pflags)) == NULL)
err(1, "init");
node = puffs_pn_new(pu, NULL);
if (node == NULL)
err(1, "puffs_pn_new");
puffs_setroot(pu, node);
puffs_setfhsize(pu, 0, PUFFS_FHFLAG_PASSTHROUGH);
po_root = puffs_getrootpathobj(pu);
if (po_root == NULL)
err(1, "getrootpathobj");
po_root->po_path = argv[0];
po_root->po_len = strlen(argv[0]);
puffs_stat2vattr(&node->pn_va, &sb);
if (detach)
if (puffs_daemon(pu, 1, 1) == -1)
err(1, "puffs_daemon");
if (puffs_mount(pu, argv[1], mntflags, node) == -1)
err(1, "puffs_mount");
if (puffs_mainloop(pu) == -1)
err(1, "mainloop");
return 0;
}
static void
get_date(char *buf, size_t bufsz)
{
static time_t tval;
struct tm *tm;
if (time(&tval) == -1) {
strncpy(buf, "unknown_date.txt", bufsz);
return;
}
if ((tm = localtime(&tval)) == NULL) {
strncpy(buf, "unknown_date.txt", bufsz);
}
strftime(buf, bufsz, "%Y%m%d-%H%M%S.txt", tm);
return;
}
int
datefs_node_readdir(
struct puffs_usermount *pu,
void *opc, struct dirent *dent,
off_t *readoff,
size_t *reslen,
const struct puffs_cred *pcr,
int *eofflag,
off_t *cookies,
size_t *ncookies)
{
struct dirent *dp;
size_t rl;
int rv;
char buf[BUFSIZ];
dp = dent;
rl = *reslen;
rv = puffs_null_node_readdir(pu, opc, dent, readoff, reslen, pcr,
eofflag, cookies, ncookies);
if (rv)
return rv;
get_date(buf, BUFSIZ);
while (rl > *reslen) {
strcpy(dp->d_name, buf);
dp->d_namlen = strlen(buf);
rl -= _DIRENT_SIZE(dp);
dp = _DIRENT_NEXT(dp);
}
return 0;
}
int
datefs_node_read(
struct puffs_usermount *pu,
void *opc,
uint8_t *buf,
off_t offset,
size_t *resid,
const struct puffs_cred *pcr,
int ioflag)
{
uint8_t *prebuf = buf;
size_t preres = *resid;
int rv;
rv = puffs_null_node_read(pu, opc, buf, offset, resid, pcr, ioflag);
if (rv)
return rv;
get_date((char *)prebuf, preres - *resid);
return rv;
}
int
datefs_node_write(
struct puffs_usermount *pu,
void *opc,
uint8_t *buf,
off_t offset,
size_t *resid,
const struct puffs_cred *pcr,
int ioflag)
{
get_date((char *)buf, *resid);
return puffs_null_node_write(pu, opc, buf, offset, resid, pcr, ioflag);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment