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
);
22 display_throughput(f
->tp
, f
->total
);
23 buf
= (char *) buf
+ 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
));
35 int sha1close(struct sha1file
*f
, unsigned char *result
, unsigned int flags
)
38 unsigned offset
= f
->offset
;
41 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
45 SHA1_Final(f
->buffer
, &f
->ctx
);
47 hashcpy(result
, f
->buffer
);
48 if (flags
& (CSUM_CLOSE
| CSUM_FSYNC
)) {
49 /* write checksum and close fd */
51 if (flags
& CSUM_FSYNC
)
52 fsync_or_die(f
->fd
, f
->name
);
54 die("%s: sha1 file error on close (%s)",
55 f
->name
, strerror(errno
));
63 int sha1write(struct sha1file
*f
, void *buf
, unsigned int count
)
66 f
->crc32
= crc32(f
->crc32
, buf
, count
);
68 unsigned offset
= f
->offset
;
69 unsigned left
= sizeof(f
->buffer
) - offset
;
70 unsigned nr
= count
> left
? left
: count
;
72 memcpy(f
->buffer
+ offset
, buf
, nr
);
75 buf
= (char *) buf
+ nr
;
78 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
87 struct sha1file
*sha1fd(int fd
, const char *name
)
89 return sha1fd_throughput(fd
, name
, NULL
);
92 struct sha1file
*sha1fd_throughput(int fd
, const char *name
, struct progress
*tp
)
94 struct sha1file
*f
= xmalloc(sizeof(*f
));
105 void crc32_begin(struct sha1file
*f
)
107 f
->crc32
= crc32(0, Z_NULL
, 0);
111 uint32_t crc32_end(struct sha1file
*f
)