author | Alberto Bertogli
<albertito@gmail.com> 2004-09-17 13:29:27 UTC |
committer | Alberto Bertogli
<albertito@gmail.com> 2007-07-15 13:23:56 UTC |
parent | d98b1b889b3fd3914fc4ca490ae52ff05ea81e06 |
samples/full.c | +55 | -0 |
diff --git a/samples/full.c b/samples/full.c new file mode 100644 index 0000000..90a009e --- /dev/null +++ b/samples/full.c @@ -0,0 +1,55 @@ + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <libjio.h> + +#define FILENAME "test1" +#define TEXT "Hello world!\n" + +int main(void) +{ + int r; + struct jfs file; + struct jtrans trans; + struct jfsck_result result; + + /* check the file is OK */ + jfsck(FILENAME, &result); + jfsck_cleanup(FILENAME); + + /* and open it */ + r = jopen(&file, FILENAME, O_SYNC | O_CREAT | O_TRUNC, 0600, 0); + if (r < 0) { + perror("jopen"); + return 1; + } + + /* write two "Hello world"s next to each other */ + jtrans_init(&file, &trans); + jtrans_add(&trans, TEXT, strlen(TEXT), 0); + jtrans_add(&trans, TEXT, strlen(TEXT), strlen(TEXT)); + r = jtrans_commit(&trans); + if (r < 0) { + perror("jtrans_commit"); + return 1; + } + + /* at this point the file has "Hello world!\nHello world!\n" */ + + /* now we rollback */ + r = jtrans_rollback(&trans); + if (r < 0) { + perror("jtrans_rollback"); + return 1; + } + + /* and now the file is empty! */ + + jtrans_free(&trans); + jclose(&file); + return 0; +} +