CID 991761: potential double close (and white space fix)
This commit is contained in:
parent
d32cb23734
commit
eda1d21fde
|
@ -178,7 +178,7 @@ void
|
||||||
chop_file(int fdin, char *fname, off_t fsize)
|
chop_file(int fdin, char *fname, off_t fsize)
|
||||||
{
|
{
|
||||||
const off_t chunk_size = KBytesPerChunk * 1024; // max bytes written to any output file
|
const off_t chunk_size = KBytesPerChunk * 1024; // max bytes written to any output file
|
||||||
|
|
||||||
bool open_next_file = true; // when to open a new output file
|
bool open_next_file = true; // when to open a new output file
|
||||||
char fnameN[256]; // name of the current output file (file01, file02, etc.)
|
char fnameN[256]; // name of the current output file (file01, file02, etc.)
|
||||||
int index = 0; // used to generate the next output file name
|
int index = 0; // used to generate the next output file name
|
||||||
|
@ -190,27 +190,27 @@ chop_file(int fdin, char *fname, off_t fsize)
|
||||||
ssize_t avail; // how many bytes we can safely grab from the current data block
|
ssize_t avail; // how many bytes we can safely grab from the current data block
|
||||||
off_t curr_written = 0; // number of bytes written to the current output file
|
off_t curr_written = 0; // number of bytes written to the current output file
|
||||||
off_t total_written = 0; // total bytes written out to all output files
|
off_t total_written = 0; // total bytes written out to all output files
|
||||||
|
|
||||||
char *beg = Block; // pointer to the beginning of the block data to be written out
|
char *beg = Block; // pointer to the beginning of the block data to be written out
|
||||||
char *end = Block; // end of the current block (init to beginning to force first block read)
|
char *end = Block; // end of the current block (init to beginning to force first block read)
|
||||||
|
|
||||||
printf("Chopping up %s into %d kbyte chunks\n", fname, KBytesPerChunk);
|
printf("Chopping up %s into %d kbyte chunks\n", fname, KBytesPerChunk);
|
||||||
|
|
||||||
while (total_written < fsize) {
|
while (total_written < fsize) {
|
||||||
if (beg >= end) {
|
if (beg >= end) {
|
||||||
// read in another block
|
// read in another block
|
||||||
got = read(fdin, Block, BLOCKSIZE);
|
got = read(fdin, Block, BLOCKSIZE);
|
||||||
if (got <= 0)
|
if (got <= 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
beg = Block;
|
beg = Block;
|
||||||
end = Block + got - 1;
|
end = Block + got - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (open_next_file) {
|
if (open_next_file) {
|
||||||
// start a new output file
|
// start a new output file
|
||||||
sprintf(fnameN, "%s%02d", fname, index++);
|
sprintf(fnameN, "%s%02d", fname, index++);
|
||||||
|
|
||||||
fdout = open(fnameN, O_WRONLY|O_CREAT);
|
fdout = open(fnameN, O_WRONLY|O_CREAT);
|
||||||
if (fdout < 0) {
|
if (fdout < 0) {
|
||||||
fprintf(stderr, "unable to create chunk file '%s': %s\n", fnameN, strerror(errno));
|
fprintf(stderr, "unable to create chunk file '%s': %s\n", fnameN, strerror(errno));
|
||||||
|
@ -219,7 +219,7 @@ chop_file(int fdin, char *fname, off_t fsize)
|
||||||
curr_written = 0;
|
curr_written = 0;
|
||||||
open_next_file = false;
|
open_next_file = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
needed = chunk_size - curr_written;
|
needed = chunk_size - curr_written;
|
||||||
avail = end - beg + 1;
|
avail = end - beg + 1;
|
||||||
if (needed > avail)
|
if (needed > avail)
|
||||||
|
@ -228,18 +228,19 @@ chop_file(int fdin, char *fname, off_t fsize)
|
||||||
if (needed > 0) {
|
if (needed > 0) {
|
||||||
put = write(fdout, beg, needed);
|
put = write(fdout, beg, needed);
|
||||||
beg += put;
|
beg += put;
|
||||||
|
|
||||||
curr_written += put;
|
curr_written += put;
|
||||||
total_written += put;
|
total_written += put;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (curr_written >= chunk_size) {
|
if (curr_written >= chunk_size) {
|
||||||
// the current output file is full
|
// the current output file is full
|
||||||
close(fdout);
|
close(fdout);
|
||||||
open_next_file = true;
|
open_next_file = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// close up the last output file
|
// close up the last output file if it's still open
|
||||||
close(fdout);
|
if (!open_next_file)
|
||||||
|
close(fdout);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue