csum-file.con commit Change xdl_merge to generate output even for null merges (5719db9)
   1/*
   2 * csum-file.c
   3 *
   4 * Copyright (C) 2005 Linus Torvalds
   5 *
   6 * Simple file write infrastructure for writing SHA1-summed
   7 * files. Useful when you write a file that you want to be
   8 * able to verify hasn't been messed with afterwards.
   9 */
  10#include "cache.h"
  11#include "progress.h"
  12#include "csum-file.h"
  13
  14static void flush(struct sha1file *f, unsigned int count)
  15{
  16        void *buf = f->buffer;
  17
  18        for (;;) {
  19                int ret = xwrite(f->fd, buf, count);
  20                if (ret > 0) {
  21                        f->total += ret;
  22                        display_throughput(f->tp, f->total);
  23                        buf = (char *) buf + ret;
  24                        count -= ret;
  25                        if (count)
  26                                continue;
  27                        return;
  28                }
  29                if (!ret)
  30                        die("sha1 file '%s' write error. Out of diskspace", f->name);
  31                die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
  32        }
  33}
  34
  35void sha1flush(struct sha1file *f)
  36{
  37        unsigned offset = f->offset;
  38
  39        if (offset) {
  40                SHA1_Update(&f->ctx, f->buffer, offset);
  41                flush(f, offset);
  42                f->offset = 0;
  43        }
  44}
  45
  46int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
  47{
  48        int fd;
  49
  50        sha1flush(f);
  51        SHA1_Final(f->buffer, &f->ctx);
  52        if (result)
  53                hashcpy(result, f->buffer);
  54        if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
  55                /* write checksum and close fd */
  56                flush(f, 20);
  57                if (flags & CSUM_FSYNC)
  58                        fsync_or_die(f->fd, f->name);
  59                if (close(f->fd))
  60                        die("%s: sha1 file error on close (%s)",
  61                            f->name, strerror(errno));
  62                fd = 0;
  63        } else
  64                fd = f->fd;
  65        free(f);
  66        return fd;
  67}
  68
  69int sha1write(struct sha1file *f, void *buf, unsigned int count)
  70{
  71        if (f->do_crc)
  72                f->crc32 = crc32(f->crc32, buf, count);
  73        while (count) {
  74                unsigned offset = f->offset;
  75                unsigned left = sizeof(f->buffer) - offset;
  76                unsigned nr = count > left ? left : count;
  77
  78                memcpy(f->buffer + offset, buf, nr);
  79                count -= nr;
  80                offset += nr;
  81                buf = (char *) buf + nr;
  82                left -= nr;
  83                if (!left) {
  84                        SHA1_Update(&f->ctx, f->buffer, offset);
  85                        flush(f, offset);
  86                        offset = 0;
  87                }
  88                f->offset = offset;
  89        }
  90        return 0;
  91}
  92
  93struct sha1file *sha1fd(int fd, const char *name)
  94{
  95        return sha1fd_throughput(fd, name, NULL);
  96}
  97
  98struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
  99{
 100        struct sha1file *f = xmalloc(sizeof(*f));
 101        f->fd = fd;
 102        f->offset = 0;
 103        f->total = 0;
 104        f->tp = tp;
 105        f->name = name;
 106        f->do_crc = 0;
 107        SHA1_Init(&f->ctx);
 108        return f;
 109}
 110
 111void crc32_begin(struct sha1file *f)
 112{
 113        f->crc32 = crc32(0, Z_NULL, 0);
 114        f->do_crc = 1;
 115}
 116
 117uint32_t crc32_end(struct sha1file *f)
 118{
 119        f->do_crc = 0;
 120        return f->crc32;
 121}