URI: 
       ttypes.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
       ---
       ttypes.c (3674B)
       ---
            1 #include <sys/types.h>
            2 
            3 #include <assert.h>
            4 #include <err.h>
            5 #include <stdio.h>
            6 #include <stdint.h>
            7 #include <stdlib.h>
            8 
            9 #include "blake2.h"
           10 #include "dedup.h"
           11 
           12 void
           13 read_snap_hdr(int fd, struct snap_hdr *hdr)
           14 {
           15         uint8_t buf[SNAP_HDR_SIZE];
           16         int n;
           17 
           18         if (xread(fd, buf, sizeof(buf)) == 0)
           19                 errx(1, "%s: unexpected EOF", __func__);
           20 
           21         n = unpack(buf, "qqq",
           22                    &hdr->flags,
           23                    &hdr->size,
           24                    &hdr->nr_snaps);
           25 
           26         n += unpack(&buf[n], "qqqqqq",
           27                     &hdr->st.orig_size,
           28                     &hdr->st.compr_size,
           29                     &hdr->st.dedup_size,
           30                     &hdr->st.min_blk_size,
           31                     &hdr->st.max_blk_size,
           32                     &hdr->st.nr_blks);
           33 
           34         n += unpack(&buf[n], "qqqq",
           35                     &hdr->st.reserved[0],
           36                     &hdr->st.reserved[1],
           37                     &hdr->st.reserved[2],
           38                     &hdr->st.reserved[3]);
           39 
           40         assert(n == SNAP_HDR_SIZE);
           41 }
           42 
           43 void
           44 write_snap_hdr(int fd, struct snap_hdr *hdr)
           45 {
           46         uint8_t buf[SNAP_HDR_SIZE];
           47         int n;
           48 
           49         n = pack(buf, "qqq",
           50                  hdr->flags,
           51                  hdr->size,
           52                  hdr->nr_snaps);
           53 
           54         n += pack(&buf[n], "qqqqqq",
           55                   hdr->st.orig_size,
           56                   hdr->st.compr_size,
           57                   hdr->st.dedup_size,
           58                   hdr->st.min_blk_size,
           59                   hdr->st.max_blk_size,
           60                   hdr->st.nr_blks);
           61 
           62         n += pack(&buf[n], "qqqq",
           63                   hdr->st.reserved[0],
           64                   hdr->st.reserved[1],
           65                   hdr->st.reserved[2],
           66                   hdr->st.reserved[3]);
           67 
           68         assert(n == SNAP_HDR_SIZE);
           69         xwrite(fd, buf, n);
           70 }
           71 
           72 void
           73 read_blk_hdr(int fd, struct blk_hdr *hdr)
           74 {
           75         uint8_t buf[BLK_HDR_SIZE];
           76         int n;
           77 
           78         if (xread(fd, buf, sizeof(buf)) == 0)
           79                 errx(1, "%s: unexpected EOF", __func__);
           80 
           81         n = unpack(buf, "qq",
           82                    &hdr->flags,
           83                    &hdr->size);
           84 
           85         assert(n == BLK_HDR_SIZE);
           86 }
           87 
           88 void
           89 write_blk_hdr(int fd, struct blk_hdr *hdr)
           90 {
           91         uint8_t buf[BLK_HDR_SIZE];
           92         int n;
           93 
           94         n = pack(buf, "qq",
           95                  hdr->flags,
           96                  hdr->size);
           97 
           98         assert(n == BLK_HDR_SIZE);
           99         xwrite(fd, buf, n);
          100 }
          101 
          102 void
          103 read_blk_desc(int fd, struct blk_desc *desc)
          104 {
          105         uint8_t buf[BLK_DESC_SIZE];
          106         char fmt[BUFSIZ];
          107         int n;
          108 
          109         if (xread(fd, buf, sizeof(buf)) == 0)
          110                 errx(1, "%s: unexpected EOF", __func__);
          111 
          112         snprintf(fmt, sizeof(fmt), "'%dqq", MD_SIZE);
          113         n = unpack(buf, fmt,
          114                    desc->md,
          115                    &desc->offset,
          116                    &desc->size);
          117 
          118         assert(n == BLK_DESC_SIZE);
          119 }
          120 
          121 void
          122 write_blk_desc(int fd, struct blk_desc *desc)
          123 {
          124         uint8_t buf[BLK_DESC_SIZE];
          125         char fmt[BUFSIZ];
          126         int n;
          127 
          128         snprintf(fmt, sizeof(fmt), "'%dqq", MD_SIZE);
          129         n = pack(buf, fmt,
          130                  desc->md,
          131                  desc->offset,
          132                  desc->size);
          133 
          134         assert(n == BLK_DESC_SIZE);
          135         xwrite(fd, buf, n);
          136 }
          137 
          138 void
          139 read_snap(int fd, struct snap *snap)
          140 {
          141         uint8_t buf[SNAPSHOT_SIZE];
          142         char fmt[BUFSIZ];
          143         int n;
          144 
          145         if (xread(fd, buf, sizeof(buf)) == 0)
          146                 errx(1, "%s: unexpected EOF", __func__);
          147 
          148         snprintf(fmt, sizeof(fmt), "q'%d'%dq", MSG_SIZE, MD_SIZE);
          149         n = unpack(buf, fmt,
          150                    &snap->size,
          151                    snap->msg,
          152                    snap->md,
          153                    &snap->nr_blk_descs);
          154 
          155         assert(n == SNAPSHOT_SIZE);
          156 };
          157 
          158 void
          159 read_snap_descs(int fd, struct snap *snap)
          160 {
          161         uint64_t i;
          162 
          163         for (i = 0; i < snap->nr_blk_descs; i++)
          164                 read_blk_desc(fd, &snap->blk_desc[i]);
          165 }
          166 
          167 void
          168 write_snap(int fd, struct snap *snap)
          169 {
          170         uint8_t buf[SNAPSHOT_SIZE];
          171         char fmt[BUFSIZ];
          172         int n;
          173 
          174         snprintf(fmt, sizeof(fmt), "q'%d'%dq", MSG_SIZE, MD_SIZE);
          175         n = pack(buf, fmt,
          176                  snap->size,
          177                  snap->msg,
          178                  snap->md,
          179                  snap->nr_blk_descs);
          180 
          181         assert(n == SNAPSHOT_SIZE);
          182         xwrite(fd, buf, n);
          183 }
          184 
          185 void
          186 write_snap_blk_descs(int fd, struct snap *snap)
          187 {
          188         uint64_t i;
          189 
          190         for (i = 0; i < snap->nr_blk_descs; i++)
          191                 write_blk_desc(fd, &snap->blk_desc[i]);
          192 }