12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382 |
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020/08/21 ShaoJinchun first version
- */
- #include <rtthread.h>
- #include <dfs.h>
- #include <dfs_fs.h>
- #include <dfs_file.h>
- #include <dfs_dentry.h>
- #include <dfs_mnt.h>
- #include "dfs_cromfs.h"
- #include <stdint.h>
- #include "zlib.h"
- #ifdef RT_USING_PAGECACHE
- #include "dfs_pcache.h"
- #endif
- /**********************************/
- #define CROMFS_PATITION_HEAD_SIZE 256
- #define CROMFS_DIRENT_CACHE_SIZE 8
- #define CROMFS_MAGIC "CROMFSMG"
- #define CROMFS_CT_ASSERT(name, x) \
- struct assert_##name {char ary[2 * (x) - 1];}
- #define CROMFS_POS_ROOT (0x0UL)
- #define CROMFS_POS_ERROR (0x1UL)
- typedef struct
- {
- uint8_t magic[8]; /* CROMFS_MAGIC */
- uint32_t version;
- uint32_t partition_attr; /* expand, now reserved 0 */
- uint32_t partition_size; /* with partition head */
- uint32_t root_dir_pos; /* root dir pos */
- uint32_t root_dir_size;
- } partition_head_data;
- typedef struct
- {
- partition_head_data head;
- uint8_t padding[CROMFS_PATITION_HEAD_SIZE - sizeof(partition_head_data)];
- } partition_head;
- enum
- {
- CROMFS_DIRENT_ATTR_FILE = 0x0UL,
- CROMFS_DIRENT_ATTR_DIR = 0x1UL,
- CROMFS_DIRENT_ATTR_SYMLINK = 0x2UL,
- };
- typedef struct
- {
- uint16_t attr; /* dir or file add other */
- uint16_t name_size; /* name real size */
- uint32_t file_size; /* file data size */
- uint32_t file_origin_size; /* file size before compress */
- uint32_t parition_pos; /* offset of data */
- uint8_t name[0]; /* name data */
- } cromfs_dirent;
- #define CROMFS_ALIGN_SIZE_BIT 4
- #define CROMFS_ALIGN_SIZE (1UL << CROMFS_ALIGN_SIZE_BIT) /* must be same as sizeof cromfs_dirent */
- #define CROMFS_ALIGN_SIZE_MASK (CROMFS_ALIGN_SIZE - 1)
- CROMFS_CT_ASSERT(align_size, CROMFS_ALIGN_SIZE == sizeof(cromfs_dirent));
- typedef union
- {
- cromfs_dirent dirent;
- uint8_t name[CROMFS_ALIGN_SIZE];
- } cromfs_dirent_item;
- /**********************************/
- typedef struct
- {
- rt_list_t list;
- uint32_t partition_pos;
- uint32_t size;
- uint8_t *buff;
- } cromfs_dirent_cache;
- typedef struct st_cromfs_info
- {
- rt_device_t device;
- uint32_t partition_size;
- uint32_t bytes_per_sector;
- uint32_t (*read_bytes)(struct st_cromfs_info *ci, uint32_t pos, void *buf, uint32_t size);
- partition_head_data part_info;
- struct rt_mutex lock;
- struct cromfs_avl_struct *cromfs_avl_root;
- rt_list_t cromfs_dirent_cache_head;
- int cromfs_dirent_cache_nr;
- const void *data;
- } cromfs_info;
- typedef struct
- {
- uint32_t ref;
- uint32_t partition_pos;
- cromfs_info *ci;
- uint32_t size;
- uint8_t *buff;
- uint32_t partition_size;
- int data_valid;
- } file_info;
- /**********************************/
- #define avl_key_t uint32_t
- #define AVL_EMPTY (struct cromfs_avl_struct *)0
- #define avl_maxheight 32
- #define heightof(tree) ((tree) == AVL_EMPTY ? 0 : (tree)->avl_height)
- struct cromfs_avl_struct
- {
- struct cromfs_avl_struct *avl_left;
- struct cromfs_avl_struct *avl_right;
- int avl_height;
- avl_key_t avl_key;
- file_info *fi;
- };
- static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree);
- static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree);
- static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct *ptree);
- static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int count)
- {
- for (;count > 0; count--)
- {
- struct cromfs_avl_struct **nodeplace = *--nodeplaces_ptr;
- struct cromfs_avl_struct *node = *nodeplace;
- struct cromfs_avl_struct *nodeleft = node->avl_left;
- struct cromfs_avl_struct *noderight = node->avl_right;
- int heightleft = heightof(nodeleft);
- int heightright = heightof(noderight);
- if (heightright + 1 < heightleft)
- {
- struct cromfs_avl_struct * nodeleftleft = nodeleft->avl_left;
- struct cromfs_avl_struct * nodeleftright = nodeleft->avl_right;
- int heightleftright = heightof(nodeleftright);
- if (heightof(nodeleftleft) >= heightleftright)
- {
- node->avl_left = nodeleftright;
- nodeleft->avl_right = node;
- nodeleft->avl_height = 1 + (node->avl_height = 1 + heightleftright);
- *nodeplace = nodeleft;
- }
- else
- {
- nodeleft->avl_right = nodeleftright->avl_left;
- node->avl_left = nodeleftright->avl_right;
- nodeleftright->avl_left = nodeleft;
- nodeleftright->avl_right = node;
- nodeleft->avl_height = node->avl_height = heightleftright;
- nodeleftright->avl_height = heightleft;
- *nodeplace = nodeleftright;
- }
- }
- else if (heightleft + 1 < heightright)
- {
- struct cromfs_avl_struct *noderightright = noderight->avl_right;
- struct cromfs_avl_struct *noderightleft = noderight->avl_left;
- int heightrightleft = heightof(noderightleft);
- if (heightof(noderightright) >= heightrightleft)
- {
- node->avl_right = noderightleft;
- noderight->avl_left = node;
- noderight->avl_height = 1 + (node->avl_height = 1 + heightrightleft);
- *nodeplace = noderight;
- }
- else
- {
- noderight->avl_left = noderightleft->avl_right;
- node->avl_right = noderightleft->avl_left;
- noderightleft->avl_right = noderight;
- noderightleft->avl_left = node;
- noderight->avl_height = node->avl_height = heightrightleft;
- noderightleft->avl_height = heightright;
- *nodeplace = noderightleft;
- }
- }
- else {
- int height = (heightleft<heightright ? heightright : heightleft) + 1;
- if (height == node->avl_height)
- {
- break;
- }
- node->avl_height = height;
- }
- }
- }
- static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree)
- {
- avl_key_t key = node_to_delete->avl_key;
- struct cromfs_avl_struct **nodeplace = ptree;
- struct cromfs_avl_struct **stack[avl_maxheight];
- uint32_t stack_count = 0;
- struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
- struct cromfs_avl_struct **nodeplace_to_delete;
- for (;;)
- {
- struct cromfs_avl_struct *node = *nodeplace;
- if (node == AVL_EMPTY)
- {
- return;
- }
- *stack_ptr++ = nodeplace;
- stack_count++;
- if (key == node->avl_key)
- {
- break;
- }
- if (key < node->avl_key)
- {
- nodeplace = &node->avl_left;
- }
- else
- {
- nodeplace = &node->avl_right;
- }
- }
- nodeplace_to_delete = nodeplace;
- if (node_to_delete->avl_left == AVL_EMPTY)
- {
- *nodeplace_to_delete = node_to_delete->avl_right;
- stack_ptr--;
- stack_count--;
- }
- else
- {
- struct cromfs_avl_struct *** stack_ptr_to_delete = stack_ptr;
- struct cromfs_avl_struct ** nodeplace = &node_to_delete->avl_left;
- struct cromfs_avl_struct * node;
- for (;;)
- {
- node = *nodeplace;
- if (node->avl_right == AVL_EMPTY)
- {
- break;
- }
- *stack_ptr++ = nodeplace;
- stack_count++;
- nodeplace = &node->avl_right;
- }
- *nodeplace = node->avl_left;
- node->avl_left = node_to_delete->avl_left;
- node->avl_right = node_to_delete->avl_right;
- node->avl_height = node_to_delete->avl_height;
- *nodeplace_to_delete = node;
- *stack_ptr_to_delete = &node->avl_left;
- }
- cromfs_avl_rebalance(stack_ptr,stack_count);
- }
- static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree)
- {
- avl_key_t key = new_node->avl_key;
- struct cromfs_avl_struct **nodeplace = ptree;
- struct cromfs_avl_struct **stack[avl_maxheight];
- int stack_count = 0;
- struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
- for (;;)
- {
- struct cromfs_avl_struct * node = *nodeplace;
- if (node == AVL_EMPTY)
- {
- break;
- }
- *stack_ptr++ = nodeplace;
- stack_count++;
- if (key < node->avl_key)
- {
- nodeplace = &node->avl_left;
- }
- else
- {
- nodeplace = &node->avl_right;
- }
- }
- new_node->avl_left = AVL_EMPTY;
- new_node->avl_right = AVL_EMPTY;
- new_node->avl_height = 1;
- *nodeplace = new_node;
- cromfs_avl_rebalance(stack_ptr,stack_count);
- }
- static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct* ptree)
- {
- for (;;)
- {
- if (ptree == AVL_EMPTY)
- {
- return (struct cromfs_avl_struct *)0;
- }
- if (key == ptree->avl_key)
- {
- break;
- }
- if (key < ptree->avl_key)
- {
- ptree = ptree->avl_left;
- }
- else
- {
- ptree = ptree->avl_right;
- }
- }
- return ptree;
- }
- /**********************************/
- static uint32_t cromfs_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
- {
- if (pos >= ci->partition_size || pos + size > ci->partition_size)
- {
- return 0;
- }
- return ci->read_bytes(ci, pos, buf, size);
- }
- static uint32_t cromfs_noblk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
- {
- uint32_t ret = 0;
- ret = rt_device_read(ci->device, pos, buf, size);
- if (ret != size)
- {
- return 0;
- }
- else
- {
- return ret;
- }
- }
- static uint32_t cromfs_data_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
- {
- uint32_t ret = 0;
- uint8_t *data = (uint8_t *)ci->data;
- if (data)
- {
- memcpy(buf, data + pos, size);
- ret = size;
- }
- return ret;
- }
- static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
- {
- uint32_t ret = 0;
- uint32_t size_bak = size;
- uint32_t start_blk = 0;
- uint32_t end_blk = 0;
- uint32_t off_s = 0;
- uint32_t sector_nr = 0;
- uint8_t *block_buff = NULL;
- uint32_t ret_len = 0;
- if (!size || !buf)
- {
- return 0;
- }
- block_buff = (uint8_t *)malloc(2 * ci->bytes_per_sector);
- if (!block_buff)
- {
- return 0;
- }
- start_blk = pos / ci->bytes_per_sector;
- off_s = pos % ci->bytes_per_sector;
- end_blk = (pos + size - 1) / ci->bytes_per_sector;
- sector_nr = end_blk - start_blk;
- if (sector_nr < 2)
- {
- ret_len = rt_device_read(ci->device, start_blk, block_buff, sector_nr + 1);
- if (ret_len != sector_nr + 1)
- {
- goto end;
- }
- memcpy(buf, block_buff + off_s, size);
- }
- else
- {
- ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
- if (ret_len != 1)
- {
- goto end;
- }
- memcpy(buf, block_buff + off_s, ci->bytes_per_sector - off_s);
- off_s = (ci->bytes_per_sector - off_s);
- size -= off_s;
- sector_nr--;
- start_blk++;
- if (sector_nr)
- {
- ret_len = rt_device_read(ci->device, start_blk, (char*)buf + off_s, sector_nr);
- if (ret_len != sector_nr)
- {
- goto end;
- }
- start_blk += sector_nr;
- off_s += (sector_nr * ci->bytes_per_sector);
- size -= (sector_nr * ci->bytes_per_sector);
- }
- ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
- if (ret_len != 1)
- {
- goto end;
- }
- memcpy((char*)buf + off_s, block_buff, size);
- }
- ret = size_bak;
- end:
- free(block_buff);
- return ret;
- }
- /**********************************/
- static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t size)
- {
- rt_list_t *l = NULL;
- cromfs_dirent_cache *dir = NULL;
- uint32_t len = 0;
- /* find */
- for (l = ci->cromfs_dirent_cache_head.next; l != &ci->cromfs_dirent_cache_head; l = l->next)
- {
- dir = (cromfs_dirent_cache *)l;
- if (dir->partition_pos == pos)
- {
- RT_ASSERT(dir->size == size);
- rt_list_remove(l);
- rt_list_insert_after(&ci->cromfs_dirent_cache_head, l);
- return dir->buff;
- }
- }
- /* not found */
- if (ci->cromfs_dirent_cache_nr >= CROMFS_DIRENT_CACHE_SIZE)
- {
- l = ci->cromfs_dirent_cache_head.prev;
- dir = (cromfs_dirent_cache *)l;
- rt_list_remove(l);
- free(dir->buff);
- free(dir);
- ci->cromfs_dirent_cache_nr--;
- }
- dir = (cromfs_dirent_cache *)malloc(sizeof *dir);
- if (!dir)
- {
- return NULL;
- }
- dir->buff = (uint8_t *)malloc(size);
- if (!dir->buff)
- {
- free(dir);
- return NULL;
- }
- len = cromfs_read_bytes(ci, pos, dir->buff, size);
- if (len != size)
- {
- free(dir->buff);
- free(dir);
- return NULL;
- }
- rt_list_insert_after(&ci->cromfs_dirent_cache_head, (rt_list_t *)dir);
- ci->cromfs_dirent_cache_nr++;
- dir->partition_pos = pos;
- dir->size = size;
- return dir->buff;
- }
- static void cromfs_dirent_cache_destroy(cromfs_info *ci)
- {
- rt_list_t *l = NULL;
- cromfs_dirent_cache *dir = NULL;
- while ((l = ci->cromfs_dirent_cache_head.next) != &ci->cromfs_dirent_cache_head)
- {
- rt_list_remove(l);
- dir = (cromfs_dirent_cache *)l;
- free(dir->buff);
- free(dir);
- ci->cromfs_dirent_cache_nr--;
- }
- }
- /**********************************/
- #ifdef RT_USING_PAGECACHE
- static ssize_t dfs_cromfs_page_read(struct dfs_file *file, struct dfs_page *page);
- static struct dfs_aspace_ops dfs_cromfs_aspace_ops =
- {
- .read = dfs_cromfs_page_read
- };
- #endif
- static int dfs_cromfs_mount(struct dfs_mnt *mnt, unsigned long rwflag, const void *data)
- {
- struct rt_device_blk_geometry geometry;
- uint32_t len = 0;
- cromfs_info *ci = NULL;
- ci = (cromfs_info *)malloc(sizeof *ci);
- if (!ci)
- {
- return -ENOMEM;
- }
- memset(ci, 0, sizeof *ci);
- ci->device = mnt->dev_id;
- ci->partition_size = UINT32_MAX;
- if (ci->device)
- {
- rt_err_t ret = rt_device_open(ci->device, RT_DEVICE_OFLAG_RDONLY);
- if (ret != RT_EOK)
- {
- free(ci);
- return ret;
- }
- if (ci->device->type == RT_Device_Class_Block)
- {
- rt_device_control(ci->device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
- ci->bytes_per_sector = geometry.bytes_per_sector;
- ci->read_bytes = cromfs_blk_read_bytes;
- }
- else
- {
- ci->read_bytes = cromfs_noblk_read_bytes;
- }
- }
- else if (data)
- {
- ci->data = data;
- ci->read_bytes = cromfs_data_read_bytes;
- }
- else
- {
- free(ci);
- return -RT_EIO;
- }
- len = cromfs_read_bytes(ci, 0, &ci->part_info, sizeof ci->part_info);
- if (len != sizeof ci->part_info ||
- memcmp(ci->part_info.magic, CROMFS_MAGIC, sizeof ci->part_info.magic) != 0)
- {
- free(ci);
- return -RT_ERROR;
- }
- ci->partition_size = ci->part_info.partition_size;
- mnt->data = ci;
- rt_mutex_init(&ci->lock, "crom", RT_IPC_FLAG_FIFO);
- ci->cromfs_avl_root = NULL;
- rt_list_init(&ci->cromfs_dirent_cache_head);
- ci->cromfs_dirent_cache_nr = 0;
- return RT_EOK;
- }
- static int dfs_cromfs_unmount(struct dfs_mnt *mnt)
- {
- rt_err_t result = RT_EOK;
- cromfs_info *ci = NULL;
- ci = (cromfs_info *)mnt->data;
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- return -RT_ERROR;
- }
- cromfs_dirent_cache_destroy(ci);
- while (ci->cromfs_avl_root)
- {
- struct cromfs_avl_struct *node;
- file_info *fi = NULL;
- node = ci->cromfs_avl_root;
- fi = node->fi;
- cromfs_avl_remove(node, &ci->cromfs_avl_root);
- free(node);
- if (fi->buff)
- {
- free(fi->buff);
- }
- free(fi);
- }
- if (ci->device)
- {
- rt_device_close(ci->device);
- }
- rt_mutex_detach(&ci->lock);
- free(ci);
- return RT_EOK;
- }
- static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* file_type, uint32_t *size, uint32_t *osize)
- {
- uint32_t cur_size = 0, cur_pos = 0, cur_osize = 0;
- const char *subpath = NULL, *subpath_end = NULL;
- void *di_mem = NULL;
- int _file_type = 0;
- if (path[0] == '\0')
- {
- return CROMFS_POS_ERROR;
- }
- cur_size = ci->part_info.root_dir_size;
- cur_osize = 0;
- cur_pos = ci->part_info.root_dir_pos;
- _file_type = CROMFS_DIRENT_ATTR_DIR;
- subpath_end = path;
- while (1)
- {
- cromfs_dirent_item *di_iter = NULL;
- int found = 0;
- /* skip /// */
- while (*subpath_end && *subpath_end == '/')
- {
- subpath_end++;
- }
- subpath = subpath_end;
- while ((*subpath_end != '/') && *subpath_end)
- {
- subpath_end++;
- }
- if (*subpath == '\0')
- {
- break;
- }
- /* if not dir or empty dir, error */
- if (_file_type != CROMFS_DIRENT_ATTR_DIR || !cur_size)
- {
- return CROMFS_POS_ERROR;
- }
- /* find subpath */
- di_mem = cromfs_dirent_cache_get(ci, cur_pos, cur_size);
- if (!di_mem)
- {
- return CROMFS_POS_ERROR;
- }
- found = 0;
- di_iter = (cromfs_dirent_item *)di_mem;
- while (1)
- {
- uint32_t name_len = subpath_end - subpath;
- uint32_t name_block = 0;
- if (di_iter->dirent.name_size == name_len)
- {
- if (memcmp(di_iter->dirent.name, subpath, name_len) == 0)
- {
- found = 1;
- cur_size = di_iter->dirent.file_size;
- cur_osize = di_iter->dirent.file_origin_size;
- cur_pos = di_iter->dirent.parition_pos;
- if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_FILE)
- {
- _file_type = CROMFS_DIRENT_ATTR_FILE;
- }
- else if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
- {
- _file_type = CROMFS_DIRENT_ATTR_DIR;
- }
- else if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_SYMLINK)
- {
- _file_type = CROMFS_DIRENT_ATTR_SYMLINK;
- }
- else
- {
- RT_ASSERT(0);
- }
- break;
- }
- }
- name_block = (di_iter->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) >> CROMFS_ALIGN_SIZE_BIT;
- di_iter += (1 + name_block);
- if ((uint32_t)(intptr_t)di_iter - (uint32_t)(intptr_t)di_mem >= cur_size)
- {
- break;
- }
- }
- if (!found)
- {
- return CROMFS_POS_ERROR;
- }
- }
- *size = cur_size;
- *osize = cur_osize;
- *file_type = _file_type;
- return cur_pos;
- }
- static uint32_t __dfs_cromfs_lookup(cromfs_info *ci, const char *path, int* file_type, uint32_t *size, uint32_t *osize)
- {
- rt_err_t result = RT_EOK;
- uint32_t ret = 0;
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- return CROMFS_POS_ERROR;
- }
- ret = cromfs_lookup(ci, path, file_type, size, osize);
- rt_mutex_release(&ci->lock);
- return ret;
- }
- static int fill_file_data(file_info *fi)
- {
- int ret = -1;
- cromfs_info *ci = NULL;
- void *compressed_file_buff = NULL;
- uLongf size = 0, osize = 0;
- if (!fi->data_valid)
- {
- RT_ASSERT(fi->buff != NULL);
- ci = fi->ci;
- osize = fi->size;
- size = fi->partition_size;
- compressed_file_buff = (void *)malloc(size);
- if (!compressed_file_buff)
- {
- goto end;
- }
- if (cromfs_read_bytes(ci, fi->partition_pos, compressed_file_buff, size) != size)
- {
- goto end;
- }
- if (uncompress((uint8_t *)fi->buff, (uLongf *)&osize, (uint8_t *)compressed_file_buff, size) != Z_OK)
- {
- goto end;
- }
- fi->data_valid = 1;
- }
- ret = 0;
- end:
- if (compressed_file_buff)
- {
- free(compressed_file_buff);
- }
- return ret;
- }
- static ssize_t dfs_cromfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
- {
- rt_err_t result = RT_EOK;
- file_info *fi = NULL;
- cromfs_info *ci = NULL;
- ssize_t length = 0;
- ci = (cromfs_info *)file->dentry->mnt->data;
- fi = (file_info *)file->vnode->data;
- if ((off_t)count < (off_t)file->vnode->size - *pos)
- {
- length = count;
- }
- else
- {
- length = (off_t)file->vnode->size - *pos;
- }
- if (length > 0)
- {
- RT_ASSERT(fi->size != 0);
- if (fi->buff)
- {
- int fill_ret = 0;
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- return 0;
- }
- fill_ret = fill_file_data(fi);
- rt_mutex_release(&ci->lock);
- if (fill_ret < 0)
- {
- return 0;
- }
- memcpy(buf, fi->buff + *pos, length);
- }
- else
- {
- void *di_mem = NULL;
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- return 0;
- }
- di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
- if (di_mem)
- {
- memcpy(buf, (char*)di_mem + *pos, length);
- }
- rt_mutex_release(&ci->lock);
- if (!di_mem)
- {
- return 0;
- }
- }
- /* update file current position */
- *pos += length;
- }
- return length;
- }
- static file_info *get_file_info(cromfs_info *ci, uint32_t partition_pos, int inc_ref)
- {
- struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root);
- if (node)
- {
- if (inc_ref)
- {
- node->fi->ref++;
- }
- return node->fi;
- }
- return NULL;
- }
- static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int file_type, uint32_t size, uint32_t osize)
- {
- file_info *fi = NULL;
- void *file_buff = NULL;
- struct cromfs_avl_struct *node = NULL;
- fi = (file_info *)malloc(sizeof *fi);
- if (!fi)
- {
- goto err;
- }
- fi->partition_pos = partition_pos;
- fi->ci = ci;
- if (file_type == CROMFS_DIRENT_ATTR_DIR)
- {
- fi->size = size;
- }
- else
- {
- fi->size = osize;
- fi->partition_size = size;
- fi->data_valid = 0;
- if (osize)
- {
- file_buff = (void *)malloc(osize);
- if (!file_buff)
- {
- goto err;
- }
- }
- }
- fi->buff = file_buff;
- fi->ref = 1;
- node = (struct cromfs_avl_struct *)malloc(sizeof *node);
- if (!node)
- {
- goto err;
- }
- node->avl_key = partition_pos;
- node->fi = fi;
- cromfs_avl_insert(node, &ci->cromfs_avl_root);
- return fi;
- err:
- if (file_buff)
- {
- free(file_buff);
- }
- if (fi)
- {
- free(fi);
- }
- return NULL;
- }
- static void deref_file_info(cromfs_info *ci, uint32_t partition_pos)
- {
- struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root);
- file_info *fi = NULL;
- if (node)
- {
- node->fi->ref--;
- if (node->fi->ref == 0)
- {
- fi = node->fi;
- cromfs_avl_remove(node, &ci->cromfs_avl_root);
- free(node);
- if (fi->buff)
- {
- free(fi->buff);
- }
- free(fi);
- }
- }
- }
- static int dfs_cromfs_close(struct dfs_file *file)
- {
- file_info *fi = NULL;
- cromfs_info *ci = NULL;
- rt_err_t result = 0;
- RT_ASSERT(file->vnode->ref_count > 0);
- if (file->vnode->ref_count > 1)
- {
- return 0;
- }
- fi = (file_info *)file->vnode->data;
- ci = (cromfs_info *)file->dentry->mnt->data;
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- return -RT_ERROR;
- }
- deref_file_info(ci, fi->partition_pos);
- rt_mutex_release(&ci->lock);
- file->vnode->data = NULL;
- return RT_EOK;
- }
- static int dfs_cromfs_open(struct dfs_file *file)
- {
- int ret = 0;
- file_info *fi = NULL;
- cromfs_info *ci = NULL;
- uint32_t file_pos = 0;
- uint32_t size = 0, osize = 0;
- int file_type = 0;
- rt_err_t result = RT_EOK;
- if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
- {
- return -EINVAL;
- }
- RT_ASSERT(file->vnode->ref_count > 0);
- if (file->vnode->ref_count > 1)
- {
- if (file->vnode->type == FT_DIRECTORY
- && !(file->flags & O_DIRECTORY))
- {
- return -ENOENT;
- }
- file->fpos = 0;
- return 0;
- }
- ci = (cromfs_info *)file->dentry->mnt->data;
- file_pos = __dfs_cromfs_lookup(ci, file->dentry->pathname, &file_type, &size, &osize);
- if (file_pos == CROMFS_POS_ERROR)
- {
- ret = -ENOENT;
- goto end;
- }
- /* entry is a directory file type */
- if (file_type == CROMFS_DIRENT_ATTR_DIR)
- {
- if (!(file->flags & O_DIRECTORY))
- {
- ret = -ENOENT;
- goto end;
- }
- file->vnode->type = FT_DIRECTORY;
- }
- else if (file_type == CROMFS_DIRENT_ATTR_SYMLINK)
- {
- file->vnode->type = FT_SYMLINK;
- }
- else
- {
- /* entry is a file, but open it as a directory */
- if (file->flags & O_DIRECTORY)
- {
- ret = -ENOENT;
- goto end;
- }
- file->vnode->type = FT_REGULAR;
- }
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- ret = -EINTR;
- goto end;
- }
- fi = get_file_info(ci, file_pos, 1);
- if (!fi)
- {
- fi = inset_file_info(ci, file_pos, file_type, size, osize);
- }
- rt_mutex_release(&ci->lock);
- if (!fi)
- {
- ret = -ENOENT;
- goto end;
- }
- file->vnode->data = fi;
- if (file_type)
- {
- file->vnode->size = size;
- }
- else
- {
- file->vnode->size = osize;
- }
- file->fpos = 0;
- ret = RT_EOK;
- end:
- return ret;
- }
- static int dfs_cromfs_stat(struct dfs_dentry *dentry, struct stat *st)
- {
- uint32_t size = 0, osize = 0;
- int file_type = 0;
- cromfs_info *ci = NULL;
- uint32_t file_pos = 0;
- ci = (cromfs_info *)dentry->mnt->data;
- file_pos = __dfs_cromfs_lookup(ci, dentry->pathname, &file_type, &size, &osize);
- if (file_pos == CROMFS_POS_ERROR)
- {
- return -ENOENT;
- }
- st->st_dev = 0;
- st->st_mode = S_IFREG | (0777);
- if (file_type == CROMFS_DIRENT_ATTR_DIR)
- {
- st->st_mode &= ~S_IFREG;
- st->st_mode |= S_IFDIR;
- st->st_size = size;
- }
- else if(file_type == CROMFS_DIRENT_ATTR_SYMLINK)
- {
- st->st_mode &= ~S_IFREG;
- st->st_mode |= S_IFLNK;
- st->st_size = osize;
- }
- else
- {
- #ifdef RT_USING_PAGECACHE
- st->st_size = (dentry->vnode && dentry->vnode->aspace) ? dentry->vnode->size : osize;
- #else
- st->st_size = osize;
- #endif
- }
- st->st_mtime = 0;
- return RT_EOK;
- }
- static int dfs_cromfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
- {
- uint32_t index = 0;
- uint8_t *name = NULL;
- struct dirent *d = NULL;
- file_info *fi = NULL;
- cromfs_info *ci = NULL;
- cromfs_dirent_item *dirent = NULL, *sub_dirent = NULL;
- void *di_mem = NULL;
- rt_err_t result = RT_EOK;
- fi = (file_info *)file->vnode->data;
- ci = fi->ci;
- RT_ASSERT(fi->buff == NULL);
- if (!fi->size)
- {
- return -EINVAL;
- }
- dirent = (cromfs_dirent_item *)malloc(fi->size);
- if (!dirent)
- {
- return -ENOMEM;
- }
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- free(dirent);
- return -EINTR;
- }
- di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
- if (di_mem)
- {
- memcpy(dirent, di_mem, fi->size);
- }
- rt_mutex_release(&ci->lock);
- if (!di_mem)
- {
- free(dirent);
- return -ENOMEM;
- }
- /* make integer count */
- count = (count / sizeof(struct dirent));
- if (count == 0)
- {
- free(dirent);
- return -EINVAL;
- }
- for (index = 0; index < count && file->fpos < file->vnode->size; index++)
- {
- uint32_t name_size = 0;
- d = dirp + index;
- sub_dirent = &dirent[file->fpos >> CROMFS_ALIGN_SIZE_BIT];
- name = sub_dirent->dirent.name;
- /* fill dirent */
- if (sub_dirent->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
- {
- d->d_type = DT_DIR;
- }
- else
- {
- d->d_type = DT_REG;
- }
- d->d_namlen = sub_dirent->dirent.name_size;
- d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
- memcpy(d->d_name, (char *)name, sub_dirent->dirent.name_size);
- d->d_name[sub_dirent->dirent.name_size] = '\0';
- name_size = (sub_dirent->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) & ~CROMFS_ALIGN_SIZE_MASK;
- /* move to next position */
- file->fpos += (name_size + sizeof *sub_dirent);
- }
- free(dirent);
- return index * sizeof(struct dirent);
- }
- static struct dfs_vnode *dfs_cromfs_lookup (struct dfs_dentry *dentry)
- {
- struct dfs_vnode *vnode = RT_NULL;
- cromfs_info *ci = NULL;
- RT_ASSERT(dentry != RT_NULL);
- RT_ASSERT(dentry->mnt != RT_NULL);
- ci = (cromfs_info *)dentry->mnt->data;
- if (ci)
- {
- uint32_t size = 0, osize = 0;
- int file_type = 0;
- uint32_t file_pos = __dfs_cromfs_lookup(ci, dentry->pathname, &file_type, &size, &osize);
- if (file_pos != CROMFS_POS_ERROR)
- {
- vnode = dfs_vnode_create();
- if (vnode)
- {
- vnode->nlink = 1;
- if (file_type == CROMFS_DIRENT_ATTR_DIR)
- {
- vnode->mode = S_IFDIR | (0777);
- vnode->type = FT_DIRECTORY;
- vnode->size = size;
- }
- else if (file_type == CROMFS_DIRENT_ATTR_SYMLINK)
- {
- vnode->mode = S_IFLNK | (0777);
- vnode->type = FT_SYMLINK;
- vnode->size = osize;
- }
- else
- {
- vnode->mode = S_IFREG | (0777);
- vnode->type = FT_REGULAR;
- vnode->size = osize;
- #ifdef RT_USING_PAGECACHE
- vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_cromfs_aspace_ops);
- #endif
- }
- vnode->mnt = dentry->mnt;
- }
- }
- }
- return vnode;
- }
- static int dfs_cromfs_free_vnode(struct dfs_vnode *vnode)
- {
- return 0;
- }
- static int cromfs_readlink(cromfs_info *ci, char *path, char *buf, int len)
- {
- int ret = 0;
- file_info *fi = NULL;
- uint32_t file_pos = 0;
- int file_type = 0;
- uint32_t size = 0, osize = 0;
- rt_err_t result = RT_EOK;
- file_pos = __dfs_cromfs_lookup(ci, path, &file_type, &size, &osize);
- if (file_pos == CROMFS_POS_ERROR)
- {
- ret = -ENOENT;
- goto end1;
- }
- result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
- if (result != RT_EOK)
- {
- ret = -EINTR;
- goto end;
- }
- fi = get_file_info(ci, file_pos, 1);
- if (!fi)
- {
- fi = inset_file_info(ci, file_pos, file_type, size, osize);
- }
- rt_mutex_release(&ci->lock);
- if (!fi)
- {
- ret = -ENOENT;
- goto end;
- }
- if (len > 0)
- {
- RT_ASSERT(fi->size != 0);
- RT_ASSERT(fi->buff);
- int fill_ret = 0;
- fill_ret = fill_file_data(fi);
- if (fill_ret < 0)
- {
- ret = -ENOENT;
- deref_file_info(ci, fi->partition_pos);
- goto end;
- }
- len = len - 1;
- osize = osize < len ? osize : len;
- memcpy(buf, fi->buff, osize);
- }
- if (ret == 0)
- {
- buf[osize] = '\0';
- ret = osize;
- }
- deref_file_info(ci, fi->partition_pos);
- end:
- rt_mutex_release(&ci->lock);
- end1:
- return ret;
- }
- #ifdef RT_USING_PAGECACHE
- static ssize_t dfs_cromfs_page_read(struct dfs_file *file, struct dfs_page *page)
- {
- int ret = -EINVAL;
- if (page->page)
- {
- off_t fpos = page->fpos;
- ret = dfs_cromfs_read(file, page->page, page->size, &fpos);
- }
- return ret;
- }
- #endif
- static int dfs_cromfs_readlink(struct dfs_dentry *dentry, char *buf, int len)
- {
- cromfs_info *ci = NULL;
- if (dentry && buf)
- {
- ci = (cromfs_info *)dentry->mnt->data;
- return cromfs_readlink(ci, dentry->pathname, buf, len);
- }
- return -EBADF;
- }
- static const struct dfs_file_ops _crom_fops =
- {
- .open = dfs_cromfs_open,
- .close = dfs_cromfs_close,
- .lseek = generic_dfs_lseek,
- .read = dfs_cromfs_read,
- .getdents = dfs_cromfs_getdents,
- };
- static const struct dfs_filesystem_ops _cromfs_ops =
- {
- .name = "crom",
- .flags = 0,
- .default_fops = &_crom_fops,
- .mount = dfs_cromfs_mount,
- .umount = dfs_cromfs_unmount,
- .readlink = dfs_cromfs_readlink,
- .stat = dfs_cromfs_stat,
- .lookup = dfs_cromfs_lookup,
- .free_vnode = dfs_cromfs_free_vnode
- };
- static struct dfs_filesystem_type _cromfs =
- {
- .fs_ops = &_cromfs_ops,
- };
- int dfs_cromfs_init(void)
- {
- /* register crom file system */
- dfs_register(&_cromfs);
- return 0;
- }
- INIT_COMPONENT_EXPORT(dfs_cromfs_init);
|