static void lzmafs_fileop_close(void *ref) { lzmafs_file_t *file = (lzmafs_file_t *) ref; file->lzmafs_fsctx->lzmafsctx_refcnt--; BDCLOSE(file->lzmafs_fsctx->lzmafsctx_subops,file->lzmafs_subfile); if (file->lzmafs_inbuf) KFREE(file->lzmafs_inbuf); if (file->lzmafs_outbuf) KFREE(file->lzmafs_outbuf); if (file->lzmafs_state) { LzmaDec_Free(file->lzmafs_state, &g_Alloc); KFREE(file->lzmafs_state); } if (file) KFREE(file); }
/* ********************************************************************* * fs_close(fsctx,ref) * * Close a file on the file system * * Input parameters: * fsctx - filesystem context (from fs_init) * ref - file handle (from fs_open) * * Return value: * 0 if ok * else error code ********************************************************************* */ int fs_close(fileio_ctx_t *fsctx,void *ref) { BDCLOSE(fsctx->ops,ref); return 0; }
static int lzmafs_fileop_open(void **ref,void *fsctx_arg,char *filename,int mode) { lzmafs_fsctx_t *fsctx; lzmafs_file_t *file; int err = 0, i; /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ unsigned char header[LZMA_PROPS_SIZE + 8]; if (mode != FILE_MODE_READ) return CFE_ERR_UNSUPPORTED; fsctx = (lzmafs_fsctx_t *) fsctx_arg; file = KMALLOC(sizeof(lzmafs_file_t), 0); if (!file) { return CFE_ERR_NOMEM; } file->lzmafs_fileoffset = 0; file->lzmafs_fsctx = fsctx; file->lzmafs_inlen = 0; file->lzmafs_eofseen = 0; file->lzmafs_state = KMALLOC(sizeof(CLzmaDec), 0); if (!file->lzmafs_state) goto error2; file->lzmafs_outlen = 0; /* Open the raw file system */ err = BDOPEN(fsctx->lzmafsctx_subops, &(file->lzmafs_subfile), fsctx->lzmafsctx_subfsctx, filename); if (err != 0) goto error2; err = BDREAD(file->lzmafs_fsctx->lzmafsctx_subops, file->lzmafs_subfile, header, sizeof(header)); if (err < 0) goto error; file->lzmafs_unpackSize = 0; /* uncompressed size */ for (i = 0; i < 8; i++) file->lzmafs_unpackSize += header[LZMA_PROPS_SIZE + i] << (i * 8); LzmaDec_Construct(file->lzmafs_state); RINOK(LzmaDec_Allocate(file->lzmafs_state, header, LZMA_PROPS_SIZE, &g_Alloc)); file->lzmafs_inbuf = KMALLOC(LZMAFS_INBUFSIZE, 0); file->lzmafs_outbuf = KMALLOC(LZMAFS_OUTBUFSIZE, 0); if (!file->lzmafs_inbuf || !file->lzmafs_outbuf) { err = CFE_ERR_NOMEM; goto error; } file->lzmafs_outptr = file->lzmafs_outbuf; LzmaDec_Init(file->lzmafs_state); fsctx->lzmafsctx_refcnt++; *ref = file; return 0; error: BDCLOSE(file->lzmafs_fsctx->lzmafsctx_subops, file->lzmafs_subfile); error2: if (file->lzmafs_inbuf) KFREE(file->lzmafs_inbuf); if (file->lzmafs_outbuf) KFREE(file->lzmafs_outbuf); if (file->lzmafs_state) { LzmaDec_Free(file->lzmafs_state, &g_Alloc); KFREE(file->lzmafs_state); } if (file) KFREE(file); return err; }