Ejemplo n.º 1
0
Archivo: file.c Proyecto: ralt/tffs-lib
static int32
_initialize_file(
	IN	tffs_t * ptffs,
	IN	tdir_t * pdir,
	IN	tdir_entry_t * pdir_entry,
	OUT	tfile_t * pfile)
{
	int32 ret;

	pfile->ptffs = ptffs;
	pfile->pdir = pdir;
	pfile->pdir_entry = pdir_entry;
	ret = FILE_OK;

	pfile->start_clus = dirent_get_clus(pdir_entry);
	pfile->file_size = dirent_get_file_size(pdir_entry);
	pfile->cur_clus = pfile->start_clus;
	pfile->cur_sec = 0;
	pfile->cur_sec_offset = 0;
	pfile->cur_fp_offset = 0;

	if (pfile->open_mode == OPENMODE_APPEND) {
		pfile->cur_fp_offset = dirent_get_file_size(pdir_entry);

		_file_seek(pfile, pfile->cur_fp_offset);
		if (pfile->cur_clus != 0) {
			if (cache_readsector(ptffs->pcache, clus2sec(ptffs, pfile->cur_clus) + pfile->cur_sec, 
					pfile->secbuf) != CACHE_OK) {
				ret = ERR_TFFS_DEVICE_FAIL;
			}
		}
	}
	else if (pfile->open_mode == OPENMODE_WRITE) {
		if (pfile->start_clus != 0) {
			if (fat_free_clus(ptffs->pfat, pfile->start_clus) != FAT_OK) {
				ERR("%s(): %d fat_free_clus failed.\n", __FUNCTION__, __LINE__);
				ret = ERR_TFFS_FAT;
			}
		}

		dirent_set_file_size(pdir_entry , 0);
		dirent_set_clus(pdir_entry, 0);

		pfile->file_size = 0;
		pfile->cur_clus = 0;
		pfile->cur_sec = 0;
		pfile->cur_sec_offset = 0;
		pfile->cur_fp_offset = 0;
	}
	else {
		if (pfile->cur_clus != 0) {
			if (cache_readsector(ptffs->pcache, clus2sec(ptffs, pfile->cur_clus) + pfile->cur_sec, 
					pfile->secbuf) != CACHE_OK) {
				ret = ERR_TFFS_DEVICE_FAIL;
			}
		}
	}

	return ret;
}
Ejemplo n.º 2
0
static int64_t _file_tell(BD_FILE_H *file)
{
    return _file_seek(file, 0, SEEK_CUR);
}