csum-file.con commit [PATCH] Remove "delta" object representation. (c4584ae)
   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 "csum-file.h"
  12
  13static int sha1flush(struct sha1file *f, unsigned int count)
  14{
  15        void *buf = f->buffer;
  16
  17        for (;;) {
  18                int ret = write(f->fd, buf, count);
  19                if (ret > 0) {
  20                        buf += ret;
  21                        count -= ret;
  22                        if (count)
  23                                continue;
  24                        return 0;
  25                }
  26                if (!ret)
  27                        die("sha1 file '%s' write error. Out of diskspace", f->name);
  28                if (errno == EAGAIN || errno == EINTR)
  29                        continue;
  30                die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
  31        }
  32}
  33
  34int sha1close(struct sha1file *f, unsigned char *result, int update)
  35{
  36        unsigned offset = f->offset;
  37        if (offset) {
  38                SHA1_Update(&f->ctx, f->buffer, offset);
  39                sha1flush(f, offset);
  40        }
  41        SHA1_Final(f->buffer, &f->ctx);
  42        if (result)
  43                memcpy(result, f->buffer, 20);
  44        if (update)
  45                sha1flush(f, 20);
  46        if (close(f->fd))
  47                die("%s: sha1 file error on close (%s)", f->name, strerror(errno));
  48        return 0;
  49}
  50
  51int sha1write(struct sha1file *f, void *buf, unsigned int count)
  52{
  53        while (count) {
  54                unsigned offset = f->offset;
  55                unsigned left = sizeof(f->buffer) - offset;
  56                unsigned nr = count > left ? left : count;
  57
  58                memcpy(f->buffer + offset, buf, nr);
  59                count -= nr;
  60                offset += nr;
  61                left -= nr;
  62                if (!left) {
  63                        SHA1_Update(&f->ctx, f->buffer, offset);
  64                        sha1flush(f, offset);
  65                        offset = 0;
  66                }
  67                f->offset = offset;
  68        }
  69        return 0;
  70}
  71
  72struct sha1file *sha1create(const char *fmt, ...)
  73{
  74        struct sha1file *f;
  75        unsigned len;
  76        va_list arg;
  77        int fd;
  78
  79        f = xmalloc(sizeof(*f));
  80
  81        va_start(arg, fmt);
  82        len = vsnprintf(f->name, sizeof(f->name), fmt, arg);
  83        va_end(arg);
  84        if (len >= PATH_MAX)
  85                die("you wascally wabbit, you");
  86        f->namelen = len;
  87
  88        fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0644);
  89        if (fd < 0)
  90                die("unable to open %s (%s)", f->name, strerror(errno));
  91        f->fd = fd;
  92        f->error = 0;
  93        f->offset = 0;
  94        SHA1_Init(&f->ctx);
  95        return f;
  96}
  97
  98int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)
  99{
 100        z_stream stream;
 101        unsigned long maxsize;
 102        void *out;
 103
 104        memset(&stream, 0, sizeof(stream));
 105        deflateInit(&stream, Z_DEFAULT_COMPRESSION);
 106        maxsize = deflateBound(&stream, size);
 107        out = xmalloc(maxsize);
 108
 109        /* Compress it */
 110        stream.next_in = in;
 111        stream.avail_in = size;
 112
 113        stream.next_out = out;
 114        stream.avail_out = maxsize;
 115
 116        while (deflate(&stream, Z_FINISH) == Z_OK)
 117                /* nothing */;
 118        deflateEnd(&stream);
 119
 120        size = stream.total_out;
 121        sha1write(f, out, size);
 122        free(out);
 123        return size;
 124}
 125
 126