4 * Copyright (C) 2005 Linus Torvalds
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.
11 #include "csum-file.h"
13 static void sha1flush(struct sha1file
*f
, unsigned int count
)
15 void *buf
= f
->buffer
;
18 int ret
= xwrite(f
->fd
, buf
, count
);
20 buf
= (char *) buf
+ ret
;
27 die("sha1 file '%s' write error. Out of diskspace", f
->name
);
28 die("sha1 file '%s' write error (%s)", f
->name
, strerror(errno
));
32 int sha1close(struct sha1file
*f
, unsigned char *result
, int final
)
35 unsigned offset
= f
->offset
;
37 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
42 /* write checksum and close fd */
43 SHA1_Final(f
->buffer
, &f
->ctx
);
45 hashcpy(result
, f
->buffer
);
48 die("%s: sha1 file error on close (%s)",
49 f
->name
, strerror(errno
));
57 int sha1write(struct sha1file
*f
, void *buf
, unsigned int count
)
60 f
->crc32
= crc32(f
->crc32
, buf
, count
);
62 unsigned offset
= f
->offset
;
63 unsigned left
= sizeof(f
->buffer
) - offset
;
64 unsigned nr
= count
> left
? left
: count
;
66 memcpy(f
->buffer
+ offset
, buf
, nr
);
69 buf
= (char *) buf
+ nr
;
72 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
81 struct sha1file
*sha1fd(int fd
, const char *name
)
86 f
= xmalloc(sizeof(*f
));
90 die("you wascally wabbit, you");
92 memcpy(f
->name
, name
, len
+1);
102 void crc32_begin(struct sha1file
*f
)
104 f
->crc32
= crc32(0, Z_NULL
, 0);
108 uint32_t crc32_end(struct sha1file
*f
)