1/*2* csum-file.c3*4* Copyright (C) 2005 Linus Torvalds5*6* Simple file write infrastructure for writing SHA1-summed7* files. Useful when you write a file that you want to be8* able to verify hasn't been messed with afterwards.9*/10#include "cache.h"11#include "csum-file.h"1213static int sha1flush(struct sha1file *f, unsigned int count)14{15void *buf = f->buffer;1617for (;;) {18int ret = write(f->fd, buf, count);19if (ret > 0) {20buf += ret;21count -= ret;22if (count)23continue;24return 0;25}26if (!ret)27die("sha1 file '%s' write error. Out of diskspace", f->name);28if (errno == EAGAIN || errno == EINTR)29continue;30die("sha1 file '%s' write error (%s)", f->name, strerror(errno));31}32}3334int sha1close(struct sha1file *f, unsigned char *result, int update)35{36unsigned offset = f->offset;37if (offset) {38SHA1_Update(&f->ctx, f->buffer, offset);39sha1flush(f, offset);40}41SHA1_Final(f->buffer, &f->ctx);42if (result)43memcpy(result, f->buffer, 20);44if (update)45sha1flush(f, 20);46if (close(f->fd))47die("%s: sha1 file error on close (%s)", f->name, strerror(errno));48free(f);49return 0;50}5152int sha1write(struct sha1file *f, void *buf, unsigned int count)53{54while (count) {55unsigned offset = f->offset;56unsigned left = sizeof(f->buffer) - offset;57unsigned nr = count > left ? left : count;5859memcpy(f->buffer + offset, buf, nr);60count -= nr;61offset += nr;62buf += nr;63left -= nr;64if (!left) {65SHA1_Update(&f->ctx, f->buffer, offset);66sha1flush(f, offset);67offset = 0;68}69f->offset = offset;70}71return 0;72}7374struct sha1file *sha1create(const char *fmt, ...)75{76struct sha1file *f;77unsigned len;78va_list arg;79int fd;8081f = xmalloc(sizeof(*f));8283va_start(arg, fmt);84len = vsnprintf(f->name, sizeof(f->name), fmt, arg);85va_end(arg);86if (len >= PATH_MAX)87die("you wascally wabbit, you");88f->namelen = len;8990fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0666);91if (fd < 0)92die("unable to open %s (%s)", f->name, strerror(errno));93f->fd = fd;94f->error = 0;95f->offset = 0;96SHA1_Init(&f->ctx);97return f;98}99100struct sha1file *sha1fd(int fd, const char *name)101{102struct sha1file *f;103unsigned len;104105f = xmalloc(sizeof(*f));106107len = strlen(name);108if (len >= PATH_MAX)109die("you wascally wabbit, you");110f->namelen = len;111memcpy(f->name, name, len+1);112113f->fd = fd;114f->error = 0;115f->offset = 0;116SHA1_Init(&f->ctx);117return f;118}119120int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)121{122z_stream stream;123unsigned long maxsize;124void *out;125126memset(&stream, 0, sizeof(stream));127deflateInit(&stream, Z_DEFAULT_COMPRESSION);128maxsize = deflateBound(&stream, size);129out = xmalloc(maxsize);130131/* Compress it */132stream.next_in = in;133stream.avail_in = size;134135stream.next_out = out;136stream.avail_out = maxsize;137138while (deflate(&stream, Z_FINISH) == Z_OK)139/* nothing */;140deflateEnd(&stream);141142size = stream.total_out;143sha1write(f, out, size);144free(out);145return size;146}147148