/* Detect FS type on device and returt pointer to static structure from fstype.c */ const char *detect_fstype(char *device, struct charlist *fl) { int fd; const char *fstype; fd = open(device, O_RDONLY); if (fd < 0) { log_msg(lg, "+ can't open device: %s", ERRMSG); return NULL; } if ( 0 != identify_fs(fd, &fstype, NULL, 0) ) { close(fd); log_msg(lg, "+ can't identify FS type"); return NULL; } close(fd); log_msg(lg, "+ FS type '%s' detected", fstype); /* Check that FS is known */ if (in_charlist(fl, fstype) < 0) { /* whitelist 'ubi', we assume it is ubifs */ if (!strncmp(fstype, "ubi",3)) { log_msg(lg, "+ found %s container: assume ubifs", fstype); } else { log_msg(lg, "+ FS %s is not supported by kernel", fstype); return NULL; } } return(fstype); }
/* * Interpret filesystem auto type if necessary */ static void interpret_type(struct fs_info *fs) { const char *type; if (strcmp(fs->type, "auto") == 0) { type = identify_fs(fs->device); if (type) { free(fs->type); fs->type = string_copy(type); } else fprintf(stderr, _("Could not determine " "filesystem type for %s\n"), fs->device); } }
/*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; } } }