usecfs/test/main.c

105 lines
2.7 KiB
C
Raw Normal View History

2019-10-21 19:23:55 +02:00
#include "usecfs.h"
#include <stdio.h>
const uint8_t test_uuid[UUID_LEN] = {
0xb3, 0x6b, 0x82, 0x05, 0xb6, 0xcd, 0x28, 0xaa, 0xc7, 0x81, 0x2e, 0x2d,
0xfd, 0xdd, 0x5e, 0x70, 0x3f, 0xbf, 0x09, 0x03, 0x1b, 0x5f, 0xbe, 0xc6,
0x09, 0x62, 0xa8, 0x23, 0xe9, 0x99, 0x5e, 0xcb, 0xb4, 0xab, 0x28, 0xdc,
0x14, 0xca, 0x35, 0xb4, 0x7d, 0xfe, 0x26, 0x81, 0x33, 0xd0, 0x4b, 0xc2,
0x49, 0x53, 0x05, 0xc3, 0xe7, 0xbd, 0x9a, 0x50, 0xb8, 0x01, 0x30, 0x0b,
0x62, 0x58, 0xad, 0xbf
};
2019-10-21 19:23:55 +02:00
int main(void)
{
int fd;
2019-10-21 20:43:23 +02:00
int buf[40] = { };
int uuid[UUID_LEN];
2019-10-21 19:23:55 +02:00
if (usecfs_init("sEcret", 0, NULL) < 0)
2019-10-21 19:23:55 +02:00
{
printf("Filesystem is not formatted. Formatting.\n");
if (usecfs_init("sEcret", 1, test_uuid) != 0) {
printf("Format failed.\n");
return 1;
}
2019-10-21 19:23:55 +02:00
}
printf("Creating file 'file1' \n");
2019-10-21 19:23:55 +02:00
fd = usecfs_creat("file1");
if (fd < 0) {
printf("could not create file (this is OK unless open fails)\n");
fd = usecfs_open("file1");
if (fd < 0) {
printf("error: open\n");
return 1;
}
2019-10-21 19:23:55 +02:00
}
printf("Writing to file 'file1' \n");
if (usecfs_write(fd, "test string file 1 content\n", 27) < 0) {
2019-10-21 19:23:55 +02:00
printf("error: write.\n");
return 1;
}
usecfs_close(fd);
printf("Re-opening\n");
2019-10-21 19:23:55 +02:00
fd = usecfs_open("file1");
if (fd < 0) {
printf("error: open\n");
return 1;
}
printf("Reading file...\n");
if (usecfs_read(fd, buf, 27) != 27) {
2019-10-21 19:23:55 +02:00
printf("error: read.\n");
return 1;
}
printf("content of file 'file1': ");
printf("%s\n", buf);
printf("appending 'test string2'\n");
if (usecfs_write(fd, "test string2\n", 13) < 0) {
2019-10-21 19:23:55 +02:00
printf("error: write.\n");
return 1;
}
printf("seek to 0\n");
2019-10-21 19:23:55 +02:00
if (usecfs_seek(fd, 0, 0) != 0) {
printf("error: seek.\n");
return 1;
}
printf("Reading...\n");
if (usecfs_read(fd, buf, 40) < 0) {
2019-10-21 20:43:23 +02:00
printf("error: read.\n");
return 1;
}
printf("%s", buf);
usecfs_close(fd);
printf("Creating file 'file2' \n");
2019-10-21 20:43:23 +02:00
fd = usecfs_creat("file2");
if (fd < 0) {
printf("could not create file (this is OK unless open fails)\n");
fd = usecfs_open("file2");
if (fd < 0) {
printf("error: open\n");
return 1;
}
2019-10-21 20:43:23 +02:00
}
usecfs_close(fd);
printf("Creating file 'file3' \n");
2019-10-21 20:43:23 +02:00
fd = usecfs_creat("file3");
if (fd < 0) {
if (fd < 0) {
printf("could not create file (this is OK unless open fails)\n");
fd = usecfs_open("file3");
if (fd < 0) {
printf("error: open\n");
return 1;
}
}
2019-10-21 20:43:23 +02:00
}
usecfs_close(fd);
2019-10-21 19:23:55 +02:00
return 0;
}