2 * All functions related with file, such as open, close, lseek and so on.
4 * Copyright (C) 2009 Liu Aleaxander -- All rights reserved. This file
5 * may be redistributed under the terms of the GNU Public License.
16 #include "fstk_malloc.h"
19 struct file
*fstk_open(struct fstk
*fs
, const char *name
)
26 int symlink_count
= 64;
29 inode
= fs
->ops
->iget_root();
33 inode
= this_dir
->dd_dir
->inode
;
39 while(*name
&& *name
!= '/')
42 inode
= fs
->ops
->iget(part
, parent
);
45 if (inode
->mode
== SYMLINK_TYPE
) {
46 if (--symlink_count
== 0 || /* limit check */
47 (int)inode
->size
>= inode
->blksize
)
49 name
= fs
->ops
->follow_symlink(inode
, name
);
53 if (parent
!= this_dir
->dd_dir
->inode
)
62 file
= fstk_malloc(sizeof(*file
));
64 malloc_error("file strucutre");
74 #define MIN(a,b) ((a) < (b) ? (a) : (b))
76 int fstk_read(struct file
*file
, void *buf
, uint32_t len
)
78 struct inode
*inode
= file
->inode
;
79 struct fstk
*fs
= file
->fs
;
80 uint32_t bytes_need
= MIN(len
, inode
->size
- file
->offset
);
81 int blocks
= (bytes_need
+ inode
->blksize
- 1) >> fs
->block_shift
;
85 if ((fs
->ops
->read(file
, buf
, blocks
)) < 0)
87 memmove(buf
, buf
+ (file
->offset
& (inode
->blksize
-1)), bytes_need
);
88 file
->offset
+= bytes_need
;
94 * lseek function, use the macro from unistd.h. Well, it aslo
95 * would be easy if you want define your macros, like FSTK_SEEK_
97 int fstk_lseek(struct file
*file
, int off
, int mode
)
101 else if (mode
== SEEK_END
)
102 file
->offset
= file
->inode
->size
+ off
;
103 else if (mode
== SEEK_SET
)
110 void fstk_close(struct file
*file
)
113 if (file
->inode
->data
)
114 fstk_free(file
->inode
->data
);
115 fstk_free(file
->inode
);