/*
* Cat
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <async.h>
#include <align.h>
#include <as.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include <sys/mman.h>
#include "../fs/fs.h"
#include "../share/shared_proto.h"
#include "../console/console.h"
#define CON_FS_ATTEMPTS 1000
static int fs_phone;
int main(int argc, char *argv[])
{
char fname[30];
unsigned int file_handle;
int retval, flags, entry;
unsigned short entries_num, inode_num;
size_t size;
void *share = NULL;
printf("Connecting to the SERVICE_FS...");
if (!connect_to_fs(&fs_phone, CON_FS_ATTEMPTS)) {
printf("Connection to SERVICE_FS was refused\n");
return -1;
}
printf("Creating address space area...");
size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE);
share = mmap(share, size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
if ((int)share < 0){
printf("As_area_create error: %d\n", (int)share
);
return -1;
}
printf("Connecting the task to FS...");
retval = async_req_2(fs_phone, FS_NEW_CONSUMER, task_get_id(), 0, NULL, NULL);
if (retval < 0) {
printf("FS_NEW_CONSUMER error: %d\n", retval
);
return -1;
}
printf("Sending memory to FS_SERVICE...");
flags = 0;
flags = AS_AREA_READ | AS_AREA_WRITE;
retval = async_req_3(fs_phone, IPC_M_AS_AREA_SEND, (uintptr_t)share, size, flags, NULL, NULL, NULL);
if (retval < 0) {
return -1;
}
/* Allocating structure for extended message. */
message_params_t *params;
params
= malloc(sizeof(message_params_t
));
memset((void*)params
, 0, sizeof(message_params_t
));
/* We want lookup our work directory. */
printf("Request for get number of entries...");
retval = send_request(fs_phone, FS_DSUM, params, share);
if (retval < 0) {
return -1;
}
printf("Total number of entries: %d\n", retval
);
entries_num = retval;
/* File list in working directory. */
for (entry = 0; entry < entries_num; entry++) {
params->entry_number = entry;
retval = send_request(fs_phone, FS_READENTRY, params, share);
if (retval < 0) {
return -1;
}
/*
if (retval < sizeof(unsigned short))
continue;
*/
memcpy(&inode_num
, share
, sizeof(unsigned short));
memcpy(fname
, (void *)(share
+sizeof(unsigned short)), retval
-sizeof(unsigned short));
/* Do not show empty entries. */
if (!inode_num)
continue;
printf("Inode number: %u\t\t", inode_num
);
printf("File name: %s\n", fname
);
}
/* We want to change working directory */
memcpy(fname
, "uspace/fs", 10);
memcpy(params
->fname
, fname
, 10);
printf("Request for changing actual directory to %s...", fname
);
retval = send_request(fs_phone, FS_CHDIR, params, share);
if (retval < 0) {
return -1;
}
/* We want to work with specified file. */
memcpy(params
->fname
, fname
, 10);
printf("Request for opening file %s...", fname
);
retval = send_request(fs_phone, FS_OPEN, params, share);
if (retval < 0) {
return -1;
}
file_handle = retval;
printf("Returned file handle...%d\n", file_handle
);
memcpy(params
->fname
, fname
, 10);
params->fd = file_handle;
printf("Request for getting info about file %s[FSTAT called]...", fname
);
retval = send_request(fs_phone, FS_FSTAT, params, share);
if (retval < 0) {
return -1;
}
params->fd = file_handle;
params->offset = 100;
params->whence = 1; /* from actual position in the file */
printf("Request for seeking after %d bytes inside file %s...", params
->offset
, fname
);
retval = send_request(fs_phone, FS_SEEK, params, share);
if (retval < 0) {
return -1;
}
printf("New file position is: %d\n", retval
);
params->nbytes = 100;
printf("Request for reading %d bytes from file %s...", params
->nbytes
, fname
);
retval = send_request(fs_phone, FS_READ, params, share);
if (retval < 0) {
return -1;
}
printf("%d bytes was read into buffer\n", retval
);
printf("\nContent of the buffer:\n");
params->offset = 0;
params->whence = 0; /* from beginning of the file */
printf("Request for seeking after %d bytes inside file %s...", params
->offset
, fname
);
retval = send_request(fs_phone, FS_SEEK, params, share);
if (retval < 0) {
return -1;
}
printf("New file position is: %d\n", retval
);
params->fd = file_handle;
params->nbytes = 50;
printf("Another request for reading %d bytes from file %s...", params
->nbytes
, fname
);
retval = send_request(fs_phone, FS_READ, params, share);
if (retval < 0) {
return -1;
}
printf("%d bytes was read into buffer\n", retval
);
printf("\nContent of the buffer:\n");
printf("Request for closing file %s...", fname
);
retval = send_request(fs_phone, FS_CLOSE, params, share);
if (retval < 0) {
return -1;
}
printf("Request for closing file %s once more...", fname
);
retval = send_request(fs_phone, FS_CLOSE, params, share);
if (retval < 0) {
return -1;
}
return 0;
}