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.
12 #include "csum-file.h"
14 static void sha1flush(struct sha1file
*f
, unsigned int count
)
16 void *buf
= f
->buffer
;
19 int ret
= xwrite(f
->fd
, buf
, count
);
21 display_throughput(f
->tp
, ret
);
22 buf
= (char *) buf
+ ret
;
29 die("sha1 file '%s' write error. Out of diskspace", f
->name
);
30 die("sha1 file '%s' write error (%s)", f
->name
, strerror(errno
));
34 int sha1close(struct sha1file
*f
, unsigned char *result
, int final
)
37 unsigned offset
= f
->offset
;
39 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
44 /* write checksum and close fd */
45 SHA1_Final(f
->buffer
, &f
->ctx
);
47 hashcpy(result
, f
->buffer
);
50 die("%s: sha1 file error on close (%s)",
51 f
->name
, strerror(errno
));
59 int sha1write(struct sha1file
*f
, void *buf
, unsigned int count
)
62 f
->crc32
= crc32(f
->crc32
, buf
, count
);
64 unsigned offset
= f
->offset
;
65 unsigned left
= sizeof(f
->buffer
) - offset
;
66 unsigned nr
= count
> left
? left
: count
;
68 memcpy(f
->buffer
+ offset
, buf
, nr
);
71 buf
= (char *) buf
+ nr
;
74 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
83 struct sha1file
*sha1fd(int fd
, const char *name
)
85 return sha1fd_throughput(fd
, name
, NULL
);
88 struct sha1file
*sha1fd_throughput(int fd
, const char *name
, struct progress
*tp
)
93 f
= xmalloc(sizeof(*f
));
97 die("you wascally wabbit, you");
99 memcpy(f
->name
, name
, len
+1);
110 void crc32_begin(struct sha1file
*f
)
112 f
->crc32
= crc32(0, Z_NULL
, 0);
116 uint32_t crc32_end(struct sha1file
*f
)