diff --git a/racket/src/rktio/Makefile.in b/racket/src/rktio/Makefile.in index 846e7f5fb6..b811e6b6d7 100644 --- a/racket/src/rktio/Makefile.in +++ b/racket/src/rktio/Makefile.in @@ -6,7 +6,15 @@ CFLAGS = @CFLAGS@ @CPPFLAGS@ RKTIO_HEADERS = $(srcdir)/rktio.h $(srcdir)/rktio_private.h rktio_config.h -all: rktio_filesystem.o rktio_read_write.o rktio_poll_set.o rktio_error.o +OBJS = rktio_filesystem.o \ + rktio_read_write.o \ + rktio_poll_set.o \ + rktio_error.o + +all: $(OBJS) + +demo: $(OBJS) main.o + $(CC) -o demo $(CFLAGS) $(LDFLAGS) main.o $(OBJS) rktio_filesystem.o: $(srcdir)/rktio_filesystem.c $(RKTIO_HEADERS) $(CC) $(CFLAGS) -I$(srcdir) -I. -o rktio_filesystem.o -c $(srcdir)/rktio_filesystem.c @@ -19,3 +27,6 @@ rktio_poll_set.o: $(srcdir)/rktio_poll_set.c $(RKTIO_HEADERS) rktio_error.o: $(srcdir)/rktio_error.c $(RKTIO_HEADERS) $(CC) $(CFLAGS) -I$(srcdir) -I. -o rktio_error.o -c $(srcdir)/rktio_error.c + +main.o: $(srcdir)/main.c $(RKTIO_HEADERS) + $(CC) $(CFLAGS) -I$(srcdir) -I. -o main.o -c $(srcdir)/main.c diff --git a/racket/src/rktio/main.c b/racket/src/rktio/main.c new file mode 100644 index 0000000000..eb0466a8bf --- /dev/null +++ b/racket/src/rktio/main.c @@ -0,0 +1,150 @@ +#include "rktio.h" +#include +#include +#include + +static void do_check_valid(int ok, int where) +{ + if (!ok) { + printf("error at %d: %d@%d = %s\n", + where, + rktio_get_last_error(), + rktio_get_last_error_kind(), + rktio_get_error_string(rktio_get_last_error(), + rktio_get_last_error_kind())); + } +} + +static void do_check_expected_error(int err, int where) +{ + if (!err) { + printf("error expected at %d\n", + where); + } +} + +static void do_check_expected_racket_error(int err, int what, int where) +{ + if (!err) { + printf("error expected at %d\n", + where); + } else if ((what != rktio_get_last_error()) + || (RKTIO_ERROR_KIND_RACKET != rktio_get_last_error_kind())) { + printf("wrong error at %d: %d@%d = %s\n", + where, + rktio_get_last_error(), + rktio_get_last_error_kind(), + rktio_get_error_string(rktio_get_last_error(), + rktio_get_last_error_kind())); + } +} + +#define check_valid(e) do_check_valid(e, __LINE__) +#define check_expected_error(e) do_check_expected_error(e, __LINE__) +#define check_expected_racket_error(e, what) do_check_expected_racket_error(e, what, __LINE__) + +static void check_hello_content(char *fn) +{ + rktio_fd_t *fd; + intptr_t amt; + char buffer[256], *s; + + fd = rktio_open(fn, RKTIO_OPEN_READ); + check_valid(!!fd); + check_valid(rktio_poll_read_ready(fd) != RKTIO_POLL_ERROR); + amt = rktio_read(fd, buffer, sizeof(buffer)); + check_valid(amt == 5); + check_valid(!strncmp(buffer, "hello", 5)); + check_valid(rktio_close(fd)); +} + +int main() +{ + rktio_size_t *sz; + rktio_fd_t *fd; + intptr_t amt, i,saw_file; + char *s, *pwd; + rktio_directory_list_t *ls; + rktio_file_copy_t *cp; + + fd = rktio_open("test1", RKTIO_OPEN_WRITE | RKTIO_OPEN_CAN_EXIST); + check_valid(!!fd); + check_valid(rktio_poll_write_ready(fd) != RKTIO_POLL_ERROR); + amt = rktio_write(fd, "hello", 5); + check_valid(amt == 5); + check_valid(rktio_close(fd)); + + check_valid(rktio_file_exists("test1")); + check_valid(!rktio_directory_exists("test1")); + check_valid(rktio_is_regular_file("test1")); + + s = rktio_get_current_directory(); + check_valid(!!s); + check_valid(rktio_directory_exists(s)); + check_valid(!rktio_file_exists(s)); + check_valid(!rktio_is_regular_file(s)); + check_expected_racket_error(!rktio_open(s, RKTIO_OPEN_WRITE | RKTIO_OPEN_CAN_EXIST), + RKTIO_ERROR_IS_A_DIRECTORY); + pwd = s; + + sz = rktio_file_size("test1"); + check_valid(!!sz); + check_valid(sz->lo == 5); + check_valid(sz->hi == 0); + free(sz); + + fd = rktio_open("test2", RKTIO_OPEN_WRITE | RKTIO_OPEN_MUST_EXIST); + check_expected_error(!fd); + + fd = rktio_open("test1", RKTIO_OPEN_WRITE); + check_expected_racket_error(!fd, RKTIO_ERROR_EXISTS); + + check_hello_content("test1"); + + if (rktio_file_exists("test1a")) + check_valid(rktio_delete_file("test1a", 1)); + if (rktio_file_exists("test1b")) + check_valid(rktio_delete_file("test1b", 1)); + + cp = rktio_copy_file_start("test1a", "test1", 0); + check_valid(!!cp); + while (!rktio_copy_file_is_done(cp)) { + check_valid(rktio_copy_file_step(cp)); + } + rktio_copy_file_stop(cp); + check_hello_content("test1a"); + + check_valid(rktio_file_exists("test1a")); + cp = rktio_copy_file_start("test1a", "test1", 0); + check_expected_racket_error(!cp, RKTIO_ERROR_EXISTS); + + cp = rktio_copy_file_start("test1a", "test1", 1); + check_valid(!!cp); + rktio_copy_file_stop(cp); + + check_valid(rktio_rename_file("test1b", "test1a", 0)); + check_valid(rktio_file_exists("test1b")); + check_expected_racket_error(!rktio_rename_file("test1b", "test1", 0), + RKTIO_ERROR_EXISTS); + check_valid(rktio_file_exists("test1")); + check_valid(rktio_file_exists("test1b")); + check_valid(!rktio_file_exists("test1a")); + + check_valid(rktio_delete_file("test1b", 0)); + check_valid(!rktio_file_exists("test1b")); + + ls = rktio_directory_list_start(pwd, 0); + check_valid(!!ls); + saw_file = 0; + while (1) { + s = rktio_directory_list_step(ls); + check_valid(!!s); + if (!*s) break; + if (!strcmp(s, "test1")) + saw_file = 1; + check_valid(strcmp(s, "test1b")); + } + check_valid(saw_file); + + return 0; +} diff --git a/racket/src/rktio/rktio.h b/racket/src/rktio/rktio.h index 84e1f10a20..21728f2214 100644 --- a/racket/src/rktio/rktio.h +++ b/racket/src/rktio/rktio.h @@ -16,7 +16,8 @@ typedef struct rktio_fd_t rktio_fd_t; #define RKTIO_OPEN_MUST_EXIST (1<<5) #define RKTIO_OPEN_CAN_EXIST (1<<6) -rktio_fd_t *rktio_fd(intptr_t system_fd, int modes); +rktio_fd_t *rktio_system_fd(intptr_t system_fd, int modes); +intptr_t rktio_fd_system_fd(rktio_fd_t *rfd); rktio_fd_t *rktio_open(char *src, int modes); int rktio_close(rktio_fd_t *fd); diff --git a/racket/src/rktio/rktio_filesystem.c b/racket/src/rktio/rktio_filesystem.c index 60f1d50455..e982e6f45d 100644 --- a/racket/src/rktio/rktio_filesystem.c +++ b/racket/src/rktio/rktio_filesystem.c @@ -769,7 +769,7 @@ int rktio_delete_file(char *fn, int enable_write_on_fail) int rktio_rename_file(char *dest, char *src, int exists_ok) { -# ifdef RKTIO_SYSTEM_WINDOWS +#ifdef RKTIO_SYSTEM_WINDOWS int errid; wchar_t *src_w = WIDE_PATH_copy(src); @@ -797,7 +797,7 @@ int rktio_rename_file(char *dest, char *src, int exists_ok) free(src_w); return 0; -# else +#else if (!exists_ok && (rktio_file_exists(dest) || rktio_directory_exists(dest))) { /* We use a specialized error here, because it's not a system error (e.g., setting `errval` to `EEXIST` would @@ -815,7 +815,7 @@ int rktio_rename_file(char *dest, char *src, int exists_ok) get_posix_error(); return 0; -# endif +#endif } char *rktio_readlink(char *fullfilename) @@ -1474,7 +1474,7 @@ rktio_file_copy_t *rktio_copy_file_start(char *dest, char *src, int exists_ok) return NULL; do { - ok = fstat(rktio_fd_value(src_fd), &buf); + ok = fstat(rktio_fd_system_fd(src_fd), &buf); } while ((ok == -1) && (errno == EINTR)); if (ok || S_ISDIR(buf.st_mode)) { diff --git a/racket/src/rktio/rktio_private.h b/racket/src/rktio/rktio_private.h index 5463121634..6aca9a7e02 100644 --- a/racket/src/rktio/rktio_private.h +++ b/racket/src/rktio/rktio_private.h @@ -24,8 +24,6 @@ void rktio_get_windows_error(void); # define get_windows_error() rktio_get_windows_error() #endif -intptr_t rktio_fd_value(rktio_fd_t *fd); - /*******************************************************************/ typedef struct rktio_poll_set_t rktio_poll_set_t; diff --git a/racket/src/rktio/rktio_read_write.c b/racket/src/rktio/rktio_read_write.c index 60d3a37b99..e3095208ad 100644 --- a/racket/src/rktio/rktio_read_write.c +++ b/racket/src/rktio/rktio_read_write.c @@ -97,7 +97,7 @@ static void init_read_fd(rktio_fd_t *rfd) #endif } -rktio_fd_t *rktio_fd(intptr_t system_fd, int modes) +rktio_fd_t *rktio_system_fd(intptr_t system_fd, int modes) { rktio_fd_t *rfd; @@ -128,6 +128,11 @@ rktio_fd_t *rktio_fd(intptr_t system_fd, int modes) return rfd; } +intptr_t rktio_fd_system_fd(rktio_fd_t *rfd) +{ + return rfd->fd; +} + /*************************************************************/ /* opening a file fd */ /*************************************************************/