mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
synced 2026-04-25 00:52:45 -04:00
selftests/powerpc: Add generic read/write file util
File read/write is reimplemented in about 5 different ways in the various PowerPC selftests. This indicates it should be a common util. Add a common read_file / write_file implementation and convert users to it where (easily) possible. Signed-off-by: Benjamin Gray <bgray@linux.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20230203003947.38033-2-bgray@linux.ibm.com
This commit is contained in:
committed by
Michael Ellerman
parent
b505063910
commit
a974f0c131
@@ -146,49 +146,37 @@ int gzip_header_blank(char *buf)
|
||||
/* Caller must free the allocated buffer return nonzero on error. */
|
||||
int read_alloc_input_file(char *fname, char **buf, size_t *bufsize)
|
||||
{
|
||||
int err;
|
||||
struct stat statbuf;
|
||||
FILE *fp;
|
||||
char *p;
|
||||
size_t num_bytes;
|
||||
|
||||
if (stat(fname, &statbuf)) {
|
||||
perror(fname);
|
||||
return(-1);
|
||||
}
|
||||
fp = fopen(fname, "r");
|
||||
if (fp == NULL) {
|
||||
perror(fname);
|
||||
return(-1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
assert(NULL != (p = (char *) malloc(statbuf.st_size)));
|
||||
num_bytes = fread(p, 1, statbuf.st_size, fp);
|
||||
if (ferror(fp) || (num_bytes != statbuf.st_size)) {
|
||||
|
||||
err = read_file(fname, p, statbuf.st_size, &num_bytes);
|
||||
if (err) {
|
||||
perror(fname);
|
||||
return(-1);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (num_bytes != statbuf.st_size) {
|
||||
fprintf(stderr, "Actual bytes != expected bytes\n");
|
||||
err = -1;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
*buf = p;
|
||||
*bufsize = num_bytes;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Returns nonzero on error */
|
||||
int write_output_file(char *fname, char *buf, size_t bufsize)
|
||||
{
|
||||
FILE *fp;
|
||||
size_t num_bytes;
|
||||
|
||||
fp = fopen(fname, "w");
|
||||
if (fp == NULL) {
|
||||
perror(fname);
|
||||
return(-1);
|
||||
}
|
||||
num_bytes = fwrite(buf, 1, bufsize, fp);
|
||||
if (ferror(fp) || (num_bytes != bufsize)) {
|
||||
perror(fname);
|
||||
return(-1);
|
||||
}
|
||||
fclose(fp);
|
||||
return 0;
|
||||
fail:
|
||||
free(p);
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -399,7 +387,7 @@ int compress_file(int argc, char **argv, void *handle)
|
||||
assert(FNAME_MAX > (strlen(argv[1]) + strlen(FEXT)));
|
||||
strcpy(outname, argv[1]);
|
||||
strcat(outname, FEXT);
|
||||
if (write_output_file(outname, outbuf, dsttotlen)) {
|
||||
if (write_file(outname, outbuf, dsttotlen)) {
|
||||
fprintf(stderr, "write error: %s\n", outname);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user