libioutil Implement a file reader
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m23s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m23s
This commit is contained in:
56
ce/ce.c
56
ce/ce.c
@@ -1,5 +1,6 @@
|
||||
#include <arena.h>
|
||||
#include <desc.h>
|
||||
#include <filereader.h>
|
||||
#include <filewriter.h>
|
||||
#include <liballoc.h>
|
||||
#include <list.h>
|
||||
@@ -386,7 +387,6 @@ static void mkfile (struct context* context, char** file_paths, size_t files_cou
|
||||
}
|
||||
|
||||
static void cat (struct context* context, char** file_paths, size_t files_count) {
|
||||
struct desc desc;
|
||||
char volume[VOLUME_MAX];
|
||||
const char* path;
|
||||
int ret;
|
||||
@@ -399,29 +399,39 @@ static void cat (struct context* context, char** file_paths, size_t files_count)
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((ret = volume_open (volume)) < 0) {
|
||||
cprintf (context, "ERROR could not open volume '%s': %s\n", volume, str_status[-ret]);
|
||||
continue;
|
||||
struct filereader fr;
|
||||
if ((ret = filereader_init (&fr, volume, path)) < 0) {
|
||||
cprintf (context, "ERROR could not initialize filereader for '%s:%s'\n", volume, path);
|
||||
return;
|
||||
}
|
||||
|
||||
describe (path, &desc);
|
||||
size_t chunk_size = 1024;
|
||||
char* buffer = arena_malloc (&arena, chunk_size);
|
||||
|
||||
if (desc.type != FS_FILE)
|
||||
goto close;
|
||||
size_t chunks = fr.file_size / chunk_size;
|
||||
size_t rem = fr.file_size % chunk_size;
|
||||
|
||||
char* buffer = malloc (desc.size + 1);
|
||||
for (size_t chunk = 0; chunk < chunks; chunk++) {
|
||||
if ((ret = filereader_read (&fr, (uint8_t*)buffer, chunk_size)) < 0) {
|
||||
filereader_fini (&fr);
|
||||
cprintf (context, "ERROR filereader failed to read from '%s:%s'\n", volume, path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (buffer == NULL)
|
||||
goto close;
|
||||
mail_send (e_pgid, buffer, chunk_size);
|
||||
}
|
||||
|
||||
memset (buffer, 0, desc.size + 1);
|
||||
read_file (path, 0, (uint8_t*)buffer, desc.size);
|
||||
cprintf (context, "%s\n", buffer);
|
||||
if (rem > 0) {
|
||||
if ((ret = filereader_read (&fr, (uint8_t*)buffer, rem)) < 0) {
|
||||
filereader_fini (&fr);
|
||||
cprintf (context, "ERROR filereader failed to read from '%s:%s'\n", volume, path);
|
||||
return;
|
||||
}
|
||||
|
||||
close:
|
||||
if (buffer != NULL)
|
||||
free (buffer);
|
||||
volume_close ();
|
||||
mail_send (e_pgid, buffer, rem);
|
||||
}
|
||||
|
||||
filereader_fini (&fr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -566,11 +576,13 @@ static void execute_redir (struct ast_redir* redir, struct context* context) {
|
||||
}
|
||||
}
|
||||
|
||||
if ((ret = filewriter_write (&fw, (uint8_t*)&context->strbuf.items[chunks * chunk_size], rem)) <
|
||||
0) {
|
||||
filewriter_fini (&fw);
|
||||
cprintf (context, "ERROR filewriter failed to write to '%s:%s'\n", volume, path);
|
||||
return;
|
||||
if (rem > 0) {
|
||||
if ((ret = filewriter_write (&fw, (uint8_t*)&context->strbuf.items[chunks * chunk_size], rem)) <
|
||||
0) {
|
||||
filewriter_fini (&fw);
|
||||
cprintf (context, "ERROR filewriter failed to write to '%s:%s'\n", volume, path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
filewriter_fini (&fw);
|
||||
|
||||
Reference in New Issue
Block a user