Refactor error handling in io.cpp. Update image.cpp and header files for changes.
parent
5a55f3db28
commit
06f02d1314
|
@ -301,7 +301,7 @@ bool factor_vm::save_image(const vm_char *saving_filename, const vm_char *filena
|
||||||
if(safe_fwrite(&h,sizeof(image_header),1,file) != 1) ok = false;
|
if(safe_fwrite(&h,sizeof(image_header),1,file) != 1) ok = false;
|
||||||
if(safe_fwrite((void*)data->tenured->start,h.data_size,1,file) != 1) ok = false;
|
if(safe_fwrite((void*)data->tenured->start,h.data_size,1,file) != 1) ok = false;
|
||||||
if(safe_fwrite(code->allocator->first_block(),h.code_size,1,file) != 1) ok = false;
|
if(safe_fwrite(code->allocator->first_block(),h.code_size,1,file) != 1) ok = false;
|
||||||
if(safe_fclose(file)) ok = false;
|
safe_fclose(file);
|
||||||
|
|
||||||
if(!ok)
|
if(!ok)
|
||||||
std::cout << "save-image failed: " << strerror(errno) << std::endl;
|
std::cout << "save-image failed: " << strerror(errno) << std::endl;
|
||||||
|
|
174
vm/io.cpp
174
vm/io.cpp
|
@ -31,7 +31,38 @@ void factor_vm::io_error()
|
||||||
general_error(ERROR_IO,tag_fixnum(errno),false_object,NULL);
|
general_error(ERROR_IO,tag_fixnum(errno),false_object,NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream)
|
FILE *factor_vm::safe_fopen(char *filename, char *mode)
|
||||||
|
{
|
||||||
|
FILE *file;
|
||||||
|
do {
|
||||||
|
file = fopen(filename,mode);
|
||||||
|
if(file == NULL)
|
||||||
|
io_error();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while(errno == EINTR);
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
int factor_vm::safe_fgetc(FILE *stream)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
do {
|
||||||
|
c = fgetc(stream);
|
||||||
|
if(c == EOF)
|
||||||
|
{
|
||||||
|
if(feof(stream))
|
||||||
|
return EOF;
|
||||||
|
else
|
||||||
|
io_error();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while(errno == EINTR);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t factor_vm::safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream)
|
||||||
{
|
{
|
||||||
size_t items_read = 0;
|
size_t items_read = 0;
|
||||||
|
|
||||||
|
@ -42,7 +73,17 @@ size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream)
|
||||||
return items_read;
|
return items_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream)
|
void factor_vm::safe_fputc(int c, FILE *stream)
|
||||||
|
{
|
||||||
|
do {
|
||||||
|
if(fputc(c,stream) == EOF)
|
||||||
|
io_error();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while(errno == EINTR);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t factor_vm::safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream)
|
||||||
{
|
{
|
||||||
size_t items_written = 0;
|
size_t items_written = 0;
|
||||||
|
|
||||||
|
@ -53,15 +94,55 @@ size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream)
|
||||||
return items_written;
|
return items_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
int safe_fclose(FILE *stream)
|
int factor_vm::safe_ftell(FILE *stream)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
off_t offset;
|
||||||
|
do {
|
||||||
|
if((offset = FTELL(stream)) == -1)
|
||||||
|
io_error();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while(errno == EINTR);
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
void factor_vm::safe_fseek(FILE *stream, off_t offset, int whence)
|
||||||
|
{
|
||||||
|
switch(whence)
|
||||||
|
{
|
||||||
|
case 0: whence = SEEK_SET; break;
|
||||||
|
case 1: whence = SEEK_CUR; break;
|
||||||
|
case 2: whence = SEEK_END; break;
|
||||||
|
default:
|
||||||
|
critical_error("Bad value for whence",whence);
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ret = fclose(stream);
|
if(FSEEK(stream,offset,whence) == -1)
|
||||||
} while(ret != 0 && errno == EINTR);
|
io_error();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while(errno == EINTR);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
void factor_vm::safe_fflush(FILE *stream)
|
||||||
|
{
|
||||||
|
do {
|
||||||
|
if(fflush(stream) == EOF)
|
||||||
|
io_error();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while(errno == EINTR);
|
||||||
|
}
|
||||||
|
|
||||||
|
void factor_vm::safe_fclose(FILE *stream)
|
||||||
|
{
|
||||||
|
do {
|
||||||
|
if(fclose(stream) == EOF)
|
||||||
|
io_error();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} while(errno == EINTR);
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_fopen()
|
void factor_vm::primitive_fopen()
|
||||||
|
@ -72,13 +153,8 @@ void factor_vm::primitive_fopen()
|
||||||
path.untag_check(this);
|
path.untag_check(this);
|
||||||
|
|
||||||
FILE *file;
|
FILE *file;
|
||||||
do {
|
file = safe_fopen((char *)(path.untagged() + 1),
|
||||||
file = fopen((char *)(path.untagged() + 1),
|
(char *)(mode.untagged() + 1));
|
||||||
(char *)(mode.untagged() + 1));
|
|
||||||
if(file == NULL)
|
|
||||||
io_error();
|
|
||||||
} while(errno == EINTR);
|
|
||||||
|
|
||||||
ctx->push(allot_alien(file));
|
ctx->push(allot_alien(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,24 +167,11 @@ void factor_vm::primitive_fgetc()
|
||||||
{
|
{
|
||||||
FILE *file = pop_file_handle();
|
FILE *file = pop_file_handle();
|
||||||
|
|
||||||
do {
|
int c = safe_fgetc(file);
|
||||||
int c = fgetc(file);
|
if(c == EOF && feof(file))
|
||||||
if(c == EOF)
|
ctx->push(false_object);
|
||||||
{
|
else
|
||||||
if(feof(file))
|
ctx->push(tag_fixnum(c));
|
||||||
{
|
|
||||||
ctx->push(false_object);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
io_error();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ctx->push(tag_fixnum(c));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} while(errno == EINTR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_fread()
|
void factor_vm::primitive_fread()
|
||||||
|
@ -156,13 +219,7 @@ void factor_vm::primitive_fputc()
|
||||||
{
|
{
|
||||||
FILE *file = pop_file_handle();
|
FILE *file = pop_file_handle();
|
||||||
fixnum ch = to_fixnum(ctx->pop());
|
fixnum ch = to_fixnum(ctx->pop());
|
||||||
|
safe_fputc(ch, file);
|
||||||
do {
|
|
||||||
if(fputc(ch,file) == EOF)
|
|
||||||
io_error();
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
} while(errno == EINTR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_fwrite()
|
void factor_vm::primitive_fwrite()
|
||||||
|
@ -183,16 +240,7 @@ void factor_vm::primitive_fwrite()
|
||||||
void factor_vm::primitive_ftell()
|
void factor_vm::primitive_ftell()
|
||||||
{
|
{
|
||||||
FILE *file = pop_file_handle();
|
FILE *file = pop_file_handle();
|
||||||
off_t offset;
|
ctx->push(from_signed_8(safe_ftell(file)));
|
||||||
|
|
||||||
do {
|
|
||||||
if((offset = FTELL(file)) == -1)
|
|
||||||
io_error();
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
} while(errno == EINTR);
|
|
||||||
|
|
||||||
ctx->push(from_signed_8(offset));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_fseek()
|
void factor_vm::primitive_fseek()
|
||||||
|
@ -200,41 +248,19 @@ void factor_vm::primitive_fseek()
|
||||||
int whence = to_fixnum(ctx->pop());
|
int whence = to_fixnum(ctx->pop());
|
||||||
FILE *file = pop_file_handle();
|
FILE *file = pop_file_handle();
|
||||||
off_t offset = to_signed_8(ctx->pop());
|
off_t offset = to_signed_8(ctx->pop());
|
||||||
|
safe_fseek(file,offset,whence);
|
||||||
switch(whence)
|
|
||||||
{
|
|
||||||
case 0: whence = SEEK_SET; break;
|
|
||||||
case 1: whence = SEEK_CUR; break;
|
|
||||||
case 2: whence = SEEK_END; break;
|
|
||||||
default:
|
|
||||||
critical_error("Bad value for whence",whence);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
if(FSEEK(file,offset,whence) == -1)
|
|
||||||
io_error();
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
} while(errno == EINTR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_fflush()
|
void factor_vm::primitive_fflush()
|
||||||
{
|
{
|
||||||
FILE *file = pop_file_handle();
|
FILE *file = pop_file_handle();
|
||||||
do {
|
safe_fflush(file);
|
||||||
if(fflush(file) == EOF)
|
|
||||||
io_error();
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
} while(errno == EINTR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_fclose()
|
void factor_vm::primitive_fclose()
|
||||||
{
|
{
|
||||||
FILE *file = pop_file_handle();
|
FILE *file = pop_file_handle();
|
||||||
if(safe_fclose(file) == EOF)
|
safe_fclose(file);
|
||||||
io_error();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This function is used by FFI I/O. Accessing the errno global directly is
|
/* This function is used by FFI I/O. Accessing the errno global directly is
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
namespace factor
|
namespace factor
|
||||||
{
|
{
|
||||||
|
|
||||||
size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream);
|
|
||||||
size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream);
|
|
||||||
int safe_fclose(FILE *stream);
|
|
||||||
|
|
||||||
/* Platform specific primitives */
|
/* Platform specific primitives */
|
||||||
|
|
||||||
VM_C_API int err_no();
|
VM_C_API int err_no();
|
||||||
|
|
|
@ -492,6 +492,15 @@ struct factor_vm
|
||||||
//io
|
//io
|
||||||
void init_c_io();
|
void init_c_io();
|
||||||
void io_error();
|
void io_error();
|
||||||
|
size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream);
|
||||||
|
size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream);
|
||||||
|
void safe_fclose(FILE *stream);
|
||||||
|
FILE* safe_fopen(char *filename, char *mode);
|
||||||
|
int safe_fgetc(FILE *stream);
|
||||||
|
void safe_fputc(int c, FILE* stream);
|
||||||
|
void safe_fflush(FILE *stream);
|
||||||
|
int safe_ftell(FILE *stream);
|
||||||
|
void safe_fseek(FILE *stream, off_t offset, int whence);
|
||||||
void primitive_fopen();
|
void primitive_fopen();
|
||||||
FILE *pop_file_handle();
|
FILE *pop_file_handle();
|
||||||
void primitive_fgetc();
|
void primitive_fgetc();
|
||||||
|
|
Loading…
Reference in New Issue