changed error handling for empty file

This commit is contained in:
Fufu Fang 2021-08-31 18:54:58 +01:00
parent 5e87ac92b0
commit 3c7e79089b
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
2 changed files with 28 additions and 39 deletions

View File

@ -189,45 +189,38 @@ static int Meta_read(Cache * cf)
return EFBIG;
}
if (cf->segbc > 0) {
/*
* Allocate memory for all segments, and read them in
*/
cf->seg = CALLOC(cf->segbc, sizeof(Seg));
nmemb = fread(cf->seg, sizeof(Seg), cf->segbc, fp);
/*
* Allocate memory for all segments, and read them in
*/
cf->seg = CALLOC(cf->segbc, sizeof(Seg));
nmemb = fread(cf->seg, sizeof(Seg), cf->segbc, fp);
/*
* We shouldn't have gone past the end of the file
*/
if (feof(fp)) {
/*
* We shouldn't have gone past the end of the file
* reached EOF
*/
if (feof(fp)) {
/*
* reached EOF
*/
lprintf(error, "attempted to read past the end of the \
lprintf(error, "attempted to read past the end of the \
file!\n");
return EBADMSG;
}
return EBADMSG;
}
/*
* Error checking for fread
*/
if (ferror(fp)) {
lprintf(error, "error reading bitmap!\n");
return EIO;
}
/*
* Error checking for fread
*/
if (ferror(fp)) {
lprintf(error, "error reading bitmap!\n");
return EIO;
}
/*
* Check for inconsistent metadata file
*/
if (nmemb != cf->segbc) {
lprintf(error, "corrupted metadata!\n");
return EBADMSG;
}
} else {
/*
* Allocate one single segment for empty file to prevent segfault
*/
cf->seg = CALLOC(1, sizeof(Seg));
/*
* Check for inconsistent metadata file
*/
if (nmemb != cf->segbc) {
lprintf(error, "corrupted metadata!\n");
return EBADMSG;
}
return 0;
@ -264,11 +257,7 @@ static int Meta_write(Cache * cf)
fwrite(&cf->content_length, sizeof(off_t), 1, fp);
fwrite(&cf->blksz, sizeof(int), 1, fp);
fwrite(&cf->segbc, sizeof(long), 1, fp);
if (cf->segbc > 0) {
fwrite(cf->seg, sizeof(Seg), cf->segbc, fp);
} else {
lprintf(error, "cg->seg <= 0!\n");
}
fwrite(cf->seg, sizeof(Seg), cf->segbc, fp);
/*
* Error checking for fwrite

View File

@ -345,7 +345,7 @@ void Link_set_file_stat(Link * this_link, CURL * curl)
double cl = 0;
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(this_link->time));
if (cl < 0) {
if (cl <= 0) {
this_link->type = LINK_INVALID;
} else {
this_link->type = LINK_FILE;