1/*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Semihalf under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD$"); 33#include <sys/types.h> 34 35#include <stdio.h> 36#include <stdlib.h> 37#include <sysexits.h> 38#include <time.h> 39 40#include <fs/nandfs/nandfs_fs.h> 41#include <libnandfs.h> 42 43#include "nandfs.h" 44 45#define NCPINFO 512 46 47static void 48lssnap_usage(void) 49{ 50 51 fprintf(stderr, "usage:\n"); 52 fprintf(stderr, "\tlssnap node\n"); 53} 54 55static void 56print_cpinfo(struct nandfs_cpinfo *cpinfo) 57{ 58 struct tm tm; 59 time_t t; 60 char timebuf[128]; 61 62 t = (time_t)cpinfo->nci_create; 63 localtime_r(&t, &tm); 64 strftime(timebuf, sizeof(timebuf), "%F %T", &tm); 65 66 printf("%20llu %s\n", (unsigned long long)cpinfo->nci_cno, timebuf); 67} 68 69int 70nandfs_lssnap(int argc, char **argv) 71{ 72 struct nandfs_cpinfo *cpinfos; 73 struct nandfs fs; 74 uint64_t next; 75 int error, nsnap, i; 76 77 if (argc != 1) { 78 lssnap_usage(); 79 return (EX_USAGE); 80 } 81 82 cpinfos = malloc(sizeof(*cpinfos) * NCPINFO); 83 if (cpinfos == NULL) { 84 fprintf(stderr, "cannot allocate memory\n"); 85 return (-1); 86 } 87 88 nandfs_init(&fs, argv[0]); 89 error = nandfs_open(&fs); 90 if (error == -1) { 91 fprintf(stderr, "nandfs_open: %s\n", nandfs_errmsg(&fs)); 92 goto out; 93 } 94 95 for (next = 1; next != 0; next = cpinfos[nsnap - 1].nci_next) { 96 nsnap = nandfs_get_snap(&fs, next, cpinfos, NCPINFO); 97 if (nsnap < 1) 98 break; 99 100 for (i = 0; i < nsnap; i++) 101 print_cpinfo(&cpinfos[i]); 102 } 103 104 if (nsnap == -1) 105 fprintf(stderr, "nandfs_get_snap: %s\n", nandfs_errmsg(&fs)); 106 107out: 108 nandfs_close(&fs); 109 nandfs_destroy(&fs); 110 free(cpinfos); 111 return (error); 112} 113