static void romident(const char *filename, romident_status *status) { osd_directory *directory; /* reset the status */ memset(status, 0, sizeof(*status)); /* first try to open as a directory */ directory = osd_opendir(filename); if (directory != NULL) { const osd_directory_entry *entry; /* iterate over all files in the directory */ while ((entry = osd_readdir(directory)) != NULL) if (entry->type == ENTTYPE_FILE) { astring *curfile = astring_assemble_3(astring_alloc(), filename, PATH_SEPARATOR, entry->name); identify_file(astring_c(curfile), status); astring_free(curfile); } osd_closedir(directory); } /* if that failed, and the filename ends with .zip, identify as a ZIP file */ else if (core_filename_ends_with(filename, ".zip")) { /* first attempt to examine it as a valid ZIP file */ zip_file *zip = NULL; zip_error ziperr = zip_file_open(filename, &zip); if (ziperr == ZIPERR_NONE && zip != NULL) { const zip_file_header *entry; /* loop over entries in the ZIP, skipping empty files and directories */ for (entry = zip_file_first_file(zip); entry; entry = zip_file_next_file(zip)) if (entry->uncompressed_length != 0) { UINT8 *data = (UINT8 *)malloc(entry->uncompressed_length); if (data != NULL) { /* decompress data into RAM and identify it */ ziperr = zip_file_decompress(zip, data, entry->uncompressed_length); if (ziperr == ZIPERR_NONE) identify_data(entry->filename, data, entry->uncompressed_length, status); free(data); } } /* close up */ zip_file_close(zip); } } /* otherwise, identify as a raw file */ else identify_file(filename, status); }
struct image *gfx_load( char *filename, int *error, Tag tags, ... ) { FILE *fp=NULL; struct image *image=NULL; struct imageio *io=NULL; int err; if( !ios ) { if( error ) *error=ERR_UNKNOWNFORMAT; return( NULL ); } if( !( fp=fopen( filename, "rb" ) ) ) { if( error ) *error=ERR_FILE; return( NULL ); } io=identify_file( fp, filename, error ); /* We have a possitive ID on the file */ if( io ) { if( !io->io_load ) { fclose( fp ); if( error ) *error=ERR_LOADNOTPOSSIBLE; return( NULL ); } if( !( image=gfx_new0( struct image, 1 ) ) ) { fclose( fp ); if( error ) *error=ERR_MEM; return( NULL ); } err=io->io_load( fp, image, (struct TagItem *)&tags ); if( err ) { free( image ); fclose( fp ); if( error ) *error=err; return( FALSE ); } } else { if( error ) *error=ERR_UNKNOWNFORMAT;
/*main*/ void HariMain(void){ fs_pid=get_pid(); while(identify_hd()==FALSE); while(identify_fs()==FALSE);//读取superBlock /*如果硬盘上不存在文件系统,则新建一个。注:约定tinyOS文件系统的superBlock前5字节为字符串“TINY”*/ if(strcmp(superBlock.sign,"TINY")==FALSE) { create_fs(); identify_fs();/*因为之前读取的可能无效,当文件系统不存在时*/ } read_hd_bmp (); read_fat(); while(1) { receive(&msg_recv,STATUS_RECV_ANY,0,fs_pid); u_int32 recv_type=msg_recv.type; u_int32 send_pid =msg_recv.send_pid; switch(recv_type) { case FILE_MSG_TYPE: { struct FILE_MSG *msg =&msg_recv.u.msg_file; u_int32 file_msg_type=msg->type ; int8*file_name =msg->file_name ; void*buf =msg->buf ; u_int32 buf_len =msg->buf_len ; u_int32 handle =msg->handle ; u_int32 result ; struct I_NODE*inode =msg->inode ; switch(file_msg_type) { case FILE_IDENTIFY: if(identify_file(handle,inode,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE ); break; case FILE_OPEN : handle=open_file(file_name,send_pid); awake(send_pid,handle); break; case FILE_READ : if((result=read_file(handle,buf_len,buf,send_pid))!=FALSE) awake(send_pid,result ); else awake(send_pid,FALSE ); break; case FILE_WRITE : if(write_file(handle,buf_len,buf,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE); break; case FILE_CREATE : if(create_file(file_name,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE); break; case FILE_DELETE : if(delete_file(handle,send_pid)==TRUE ) awake(send_pid,TRUE ); else awake(send_pid,FALSE ); break; case POWER_OFF : write_back( ); awake(send_pid,TRUE ); break; default: awake(send_pid,FALSE ); break; } break; } default: break; } } }