Create libioutil, implement a filewriter
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m21s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m21s
This commit is contained in:
4
libioutil/.gitignore
vendored
Normal file
4
libioutil/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
*.o
|
||||
*.json
|
||||
docs/
|
||||
.cache/
|
||||
8
libioutil/Makefile
Normal file
8
libioutil/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
include ../make/ufuncs.mk
|
||||
|
||||
$(eval $(call add_include,libsystem))
|
||||
$(eval $(call add_include,libstring))
|
||||
|
||||
libname := libioutil
|
||||
|
||||
include ../make/lib.mk
|
||||
83
libioutil/filewriter.c
Normal file
83
libioutil/filewriter.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <desc.h>
|
||||
#include <filewriter.h>
|
||||
#include <path_defs.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <system.h>
|
||||
#include <write_file.h>
|
||||
|
||||
int filewriter_init (struct filewriter* fw, const char* volume, const char* path, uint32_t flags) {
|
||||
memset (fw, 0, sizeof (*fw));
|
||||
strncpy (fw->volume, volume, VOLUME_MAX);
|
||||
strncpy (fw->path, path, PATH_MAX);
|
||||
fw->flags = flags;
|
||||
|
||||
int ret;
|
||||
struct desc desc;
|
||||
|
||||
if ((ret = volume_open (fw->volume)) < 0)
|
||||
return -FW_VOLUME_OPEN_ERROR;
|
||||
|
||||
if ((fw->flags & FW_CREATE_FILE)) {
|
||||
if ((ret = create_file (fw->path)) < 0) {
|
||||
volume_close ();
|
||||
return -FW_CREATE_FILE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if ((ret = describe (fw->path, &desc)) < 0) {
|
||||
volume_close ();
|
||||
return -FW_DESC_ERROR;
|
||||
}
|
||||
|
||||
if (desc.type != FS_FILE)
|
||||
return -FW_NOT_FILE;
|
||||
|
||||
fw->file_size = desc.size;
|
||||
fw->flags |= FW_OPEN;
|
||||
|
||||
if ((fw->flags & FW_APPEND))
|
||||
fw->write_cursor = desc.size;
|
||||
|
||||
return FW_OK;
|
||||
}
|
||||
|
||||
int filewriter_fini (struct filewriter* fw) {
|
||||
if ((fw->flags & FW_OPEN)) {
|
||||
volume_close ();
|
||||
fw->flags &= ~FW_OPEN;
|
||||
}
|
||||
|
||||
return FW_OK;
|
||||
}
|
||||
|
||||
int filewriter_write (struct filewriter* fw, uint8_t* buffer, size_t buffer_size) {
|
||||
if (!(fw->flags & FW_OPEN))
|
||||
return -FW_VOLUME_NOT_OPENED;
|
||||
|
||||
struct desc desc;
|
||||
int ret;
|
||||
|
||||
if ((fw->flags & FW_APPEND)) {
|
||||
if ((ret = describe (fw->path, &desc)) < 0)
|
||||
return -FW_DESC_ERROR;
|
||||
|
||||
fw->file_size = desc.size;
|
||||
fw->write_cursor = fw->file_size;
|
||||
}
|
||||
|
||||
if (buffer_size > 0) {
|
||||
if (!(fw->flags & FW_APPEND) && !(fw->write_cursor + buffer_size <= fw->file_size))
|
||||
return -FW_CURSOR_OOB;
|
||||
|
||||
uint32_t flags = (fw->flags & FW_APPEND) ? WF_APPEND : 0;
|
||||
if ((ret = write_file (fw->path, fw->write_cursor, buffer, buffer_size, flags)) < 0)
|
||||
return -FW_WRITE_ERROR;
|
||||
|
||||
fw->write_cursor += buffer_size;
|
||||
}
|
||||
|
||||
return FW_OK;
|
||||
}
|
||||
36
libioutil/filewriter.h
Normal file
36
libioutil/filewriter.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef _LIBIOUTIL_FILEWRITER_H
|
||||
#define _LIBIOUTIL_FILEWRITER_H
|
||||
|
||||
#include <path_defs.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define FW_OK 0
|
||||
#define FW_VOLUME_OPEN_ERROR 1
|
||||
#define FW_DESC_ERROR 2
|
||||
#define FW_NOT_FILE 3
|
||||
#define FW_VOLUME_NOT_OPENED 4
|
||||
#define FW_WRITE_ERROR 5
|
||||
#define FW_CURSOR_OOB 6
|
||||
#define FW_CREATE_FILE_ERROR 7
|
||||
|
||||
#define FW_CREATE_FILE (1 << 0)
|
||||
#define FW_APPEND (1 << 1)
|
||||
#define FW_OPEN (1 << 31)
|
||||
|
||||
struct filewriter {
|
||||
char volume[VOLUME_MAX];
|
||||
char path[PATH_MAX];
|
||||
size_t write_cursor;
|
||||
size_t file_size;
|
||||
uint32_t flags;
|
||||
};
|
||||
|
||||
int filewriter_init (struct filewriter* fw, const char* volume, const char* path, uint32_t flags);
|
||||
|
||||
int filewriter_fini (struct filewriter* fw);
|
||||
|
||||
int filewriter_write (struct filewriter* fw, uint8_t* buffer, size_t buffer_size);
|
||||
|
||||
#endif // _LIBIOUTIL_FILEWRITER_H
|
||||
3
libioutil/src.mk
Normal file
3
libioutil/src.mk
Normal file
@@ -0,0 +1,3 @@
|
||||
c += filewriter.c
|
||||
|
||||
o += filewriter.o
|
||||
Reference in New Issue
Block a user