URI: 
       tdinit.c - dedup - data deduplication program
  HTML git clone git://bitreich.org/dedup/ git://hg6vgqziawt5s4dj.onion/dedup/
   DIR Log
   DIR Files
   DIR Refs
   DIR Tags
   DIR README
   DIR LICENSE
       ---
       tdinit.c (2059B)
       ---
            1 #include <sys/types.h>
            2 #include <sys/stat.h>
            3 #include <sys/file.h>
            4 
            5 #include <err.h>
            6 #include <fcntl.h>
            7 #include <stdio.h>
            8 #include <stdint.h>
            9 #include <stdlib.h>
           10 #include <string.h>
           11 #include <unistd.h>
           12 
           13 #include "arg.h"
           14 #include "blake2.h"
           15 #include "dedup.h"
           16 
           17 static struct snap_hdr snap_hdr;
           18 static struct blk_hdr blk_hdr;
           19 static int ifd;
           20 static int sfd;
           21 static int hash_algo = HASH_BLAKE2B;
           22 static int compr_algo = COMPR_LZ4;
           23 
           24 int verbose;
           25 char *argv0;
           26 
           27 static void
           28 init(void)
           29 {
           30         int flags;
           31 
           32         flags = O_RDWR | O_CREAT | O_EXCL;
           33         ifd = open(SNAPSF, flags, 0600);
           34         if (ifd < 0)
           35                 err(1, "open %s", SNAPSF);
           36 
           37         sfd = open(STOREF, flags, 0600);
           38         if (sfd < 0)
           39                 err(1, "open %s", STOREF);
           40 
           41         if (flock(ifd, LOCK_NB | LOCK_EX) < 0 ||
           42             flock(sfd, LOCK_NB | LOCK_EX) < 0)
           43                 err(1, "flock");
           44 
           45         init_snap_hdr(&snap_hdr);
           46         init_blk_hdr(&blk_hdr, compr_algo, hash_algo);
           47 }
           48 
           49 static void
           50 term(void)
           51 {
           52         xlseek(ifd, 0, SEEK_SET);
           53         write_snap_hdr(ifd, &snap_hdr);
           54         xlseek(sfd, 0, SEEK_SET);
           55         write_blk_hdr(sfd, &blk_hdr);
           56 
           57         fsync(ifd);
           58         fsync(sfd);
           59 
           60         close(ifd);
           61         close(sfd);
           62 }
           63 
           64 static void
           65 usage(void)
           66 {
           67         fprintf(stderr, "usage: %s [-v] [-H hash] [-Z compressor] [repo]\n", argv0);
           68         exit(1);
           69 }
           70 
           71 int
           72 main(int argc, char *argv[])
           73 {
           74         char *hash_name = NULL, *compr_name = NULL;
           75         char *repo;
           76 
           77         ARGBEGIN {
           78         case 'H':
           79                 hash_name = EARGF(usage());
           80                 if (strcmp(hash_name, "?") == 0) {
           81                         hash_list(STDERR_FILENO);
           82                         return 0;
           83                 }
           84                 hash_algo = hash_name2type(hash_name);
           85                 if (hash_algo < 0)
           86                         errx(1, "unknown hash: %s", hash_name);
           87                 break;
           88         case 'Z':
           89                 compr_name = EARGF(usage());
           90                 if (strcmp(compr_name, "?") == 0) {
           91                         compr_list(STDERR_FILENO);
           92                         return 0;
           93                 }
           94                 compr_algo = compr_name2type(compr_name);
           95                 if (compr_algo < 0)
           96                         errx(1, "unknown compressor: %s", compr_name);
           97                 break;
           98         case 'v':
           99                 verbose++;
          100                 break;
          101         default:
          102                 usage();
          103         } ARGEND
          104 
          105         switch (argc) {
          106         case 0:
          107                 repo = ".";
          108                 break;
          109         case 1:
          110                 repo = argv[0];
          111                 break;
          112         default:
          113                 usage();
          114         };
          115 
          116         mkdir(repo, 0700);
          117         if (chdir(repo) < 0)
          118                 err(1, "chdir: %s", repo);
          119 
          120         init();
          121         term();
          122         return 0;
          123 }