16
#include <mem/objalloc.h>
18
#include <fs/file_desc.h>
19
#include <kernel/task.h>
20
#include <kernel/task/resource/idesc_table.h>
21
#include <embox/unit.h>
25
extern const struct idesc_xattrops file_idesc_xattrops;
27
OBJALLOC_DEF(desc_pool, struct file_desc, OPTION_GET(NUMBER,fdesc_quantity));
29
static struct file_desc *file_desc_alloc(void) {
30
return objalloc(&desc_pool);
33
static void file_desc_free(struct file_desc *desc) {
34
objfree(&desc_pool, desc);
37
extern const struct idesc_ops idesc_file_ops;
39
struct file_desc *file_desc_create(struct inode *node, int flag) {
40
struct file_desc *desc;
45
if (NULL == (desc = file_desc_alloc())) {
46
return err2ptr(ENOMEM);
50
if ((flag & O_WRONLY) || (flag & O_RDWR)) {
51
perm_flags |= S_IWOTH;
53
if (!(flag & O_WRONLY)) {
54
perm_flags |= S_IROTH;
57
if (0 > (ret = fs_perm_check(node, perm_flags))) {
59
return err2ptr(EACCES);
65
idesc_init(&desc->f_idesc, &idesc_file_ops, flag);
66
desc->f_idesc.idesc_xattrops = &file_idesc_xattrops;
71
int file_desc_destroy(struct file_desc *fdesc) {
74
inode_del(fdesc->f_inode);
76
file_desc_free(fdesc);
80
struct file_desc *file_desc_get(int idx) {
82
struct idesc_table *it;
84
assert(idesc_index_valid(idx));
86
it = task_resource_idesc_table(task_self());
89
idesc = idesc_table_get(it, idx);
92
if (idesc->idesc_ops != &idesc_file_ops) {
96
return (struct file_desc *) idesc;
99
off_t file_get_pos(struct file_desc * file) {
103
off_t file_set_pos(struct file_desc *file, off_t off) {
109
size_t file_get_size(struct file_desc *file) {
110
return file->f_inode->i_size;
113
void file_set_size(struct file_desc *file, size_t size) {
114
file->f_inode->i_size = size;
117
void *file_get_inode_data(struct file_desc *file) {
118
assert(file->f_inode);
120
return file->f_inode->i_privdata;
123
struct file_desc *file_desc_from_idesc(struct idesc *idesc) {
124
return (struct file_desc *)idesc;
127
void file_desc_set_file_info(struct file_desc *file, void *fi) {
128
file->file_info = fi;