int main(int argc, char *argv[]) { make_fs("test"); mount_fs("test"); fs_create("myfile.txt"); fs_list_files(); fs_create("junk.txt"); fs_list_files(); int fd = fs_open("myfile.txt"); char *buf = "testing this code"; fs_write(fd,buf,strlen(buf)); //fs_lseek(fd,100); buf = "it! Blahc lksjdfkj ihwtw lekjth lksajdf 123413459827 i sadfkjhq3oipu4tryheiq8u934yt5981 3yq4t97ughergo9vyq398 4y5t 98qgrebgfvkje"; fs_write(fd,buf,strlen(buf)+1); //fs_truncate(fd,2); //fs_write(fd,"",1); fs_close(fd); fd = fs_open("myfile.txt"); char word[1000]; fs_read(fd,word,10000); fs_close(fd); printf("\"%s\" was read from myfile.txt\n",word); printf("size of myfile.txt - %d\n", fs_get_filesize("myfile.txt")); unmount_fs("test"); mount_fs("test"); fs_list_files(); printf("size of myfile.txt - %d\n", fs_get_filesize("myfile.txt")); printf("size of junk.txt - %d\n", fs_get_filesize("junk.txt")); fd = fs_open("myfile.txt"); fs_read(fd,word,10000); fs_close(fd); printf("\"%s\" was read from myfile.txt\n",word); fs_delete("myfile.txt"); fs_list_files(); unmount_fs("test"); return 0; }
int fs_open(char *file_name, int mode) { //printf("Função não implementada: fs_open\n"); int flag = 0, i; if(formatado == 0) { printf("Disco não formatado\n"); return -1; } for (i = 0; i < 128; i++) { if(dir[i].used != '0' && strcmp(dir[i].name,file_name) == 0) { flag = 1; break; } } if ((flag = 0) && (mode == FS_R)) { printf("Arquivo inexistente\n"); return -1; } else { if((flag = 0) && (mode == FS_W)) { i = fs_create(file_name); arquivo[i].aberto = 1; arquivo[i].bytes_lidos = 0; arquivo[i].byte_bloco = 0; arquivo[i].bloco = dir[i].first_block; } else { if((flag = 1) && (mode == FS_R)) { arquivo[i].aberto = 0; 0 arquivo[i].bytes_lidos = 0; arquivo[i].byte_bloco = 0; arquivo[i].bloco = dir[i].first_block; } else { if((flag = 1) && (mode == FS_W)) { fs_remove(file_name); i = fs_create(file_name); arquivo[i].aberto = 1; arquivo[i].bytes_lidos = 0; arquivo[i].byte_bloco = 0; arquivo[i].bloco = dir[i].first_block; } } } } return i; }
void proc_init_ap(void) { int cpuid = myid(); struct proc_struct *idle; idle = alloc_proc(); if (idle == NULL) { panic("cannot alloc idleproc.\n"); } idle->pid = cpuid; idle->state = PROC_RUNNABLE; // XXX // idle->kstack = (uintptr_t)bootstack; idle->need_resched = 1; idle->tf = NULL; if ((idle->fs_struct = fs_create()) == NULL) { panic("create fs_struct (idleproc) failed.\n"); } fs_count_inc(idle->fs_struct); char namebuf[32]; snprintf(namebuf, 32, "idle/%d", cpuid); set_proc_name(idle, namebuf); nr_process++; idleproc = idle; current = idle; assert(idleproc != NULL && idleproc->pid == cpuid); }
void cmd_create( void ) { S_FSTREAM fs; S_FINFO fi; if( !t0_testP3( sizeof(S_FINFO) ) ) return; if( !t0_testP1P2( 0x0000 ) ) return; /* Get DF data */ if( !fs_getData( &selected, &fs, &fi ) ) return; /* Correct DF type? */ if( fi.type!=FS_TYPE_DF ) { sw_set( SW_NOT_ALLOWED ); return; } /* Correct permissions? */ if( !auth_checkAc( (fi.ac[0]>>4)&0x0F ) ) return; /* ACK */ t0_sendAck(); /* Receive */ t0_recBlock( (iu8 *)&fi, sizeof(S_FINFO) ); hton_us( &fi.size, 1 ); hton_us( &fi.fid, 1 ); if( !fs_create( &fi ) ) return; sw_set( SW_OK ); }
void proc_init_ap(void) { int lcpu_idx = pls_read(lcpu_idx); int lapic_id = pls_read(lapic_id); pls_write(idleproc, alloc_proc()); if (idleproc == NULL) { panic("cannot alloc idleproc.\n"); } idleproc->pid = lcpu_idx; idleproc->state = PROC_RUNNABLE; // XXX // idleproc->kstack = (uintptr_t)bootstack; idleproc->need_resched = 1; idleproc->tf = NULL; if ((idleproc->fs_struct = fs_create()) == NULL) { panic("create fs_struct (idleproc) failed.\n"); } fs_count_inc(idleproc->fs_struct); char namebuf[32]; snprintf(namebuf, 32, "idle/%d", lapic_id); set_proc_name(idleproc, namebuf); nr_process ++; pls_write(current, idleproc); assert(idleproc != NULL && idleproc->pid == lcpu_idx); }
static int copy_fs(uint32_t clone_flags, struct proc_struct *proc) { struct fs_struct *fs_struct, *old_fs_struct = current->fs_struct; assert(old_fs_struct != NULL); if (clone_flags & CLONE_FS) { fs_struct = old_fs_struct; goto good_fs_struct; } int ret = -E_NO_MEM; if ((fs_struct = fs_create()) == NULL) { goto bad_fs_struct; } if ((ret = dup_fs(fs_struct, old_fs_struct)) != 0) { goto bad_dup_cleanup_fs; } good_fs_struct: fs_count_inc(fs_struct); proc->fs_struct = fs_struct; return 0; bad_dup_cleanup_fs: fs_destroy(fs_struct); bad_fs_struct: return ret; }
int chips_mkdir(const char *path, mode_t mode) { if(!fs_create((char *)path, POTATOES_DIRECTORY)) return -ENOENT; return 0; }
/** * addr_offset */ void fs_reset_specific(u32_t addr_offset, u32_t phys_addr, u32_t phys_size, u32_t phys_sector_size, u32_t log_block_size, u32_t log_page_size) { fs_create(phys_size + phys_addr - addr_offset, phys_sector_size, log_page_size, DEFAULT_NUM_FD, DEFAULT_NUM_CACHE_PAGES); fs_set_addr_offset(addr_offset); memset(&AREA(addr_offset), 0xcc, _area_sz); memset(&AREA(phys_addr), 0xff, phys_size); memset(&__fs, 0, sizeof(__fs)); s32_t res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size); #if SPIFFS_USE_MAGIC if (res == SPIFFS_OK) { SPIFFS_unmount(&__fs); } res = SPIFFS_format(&__fs); if (res != SPIFFS_OK) { printf("format failed, %i\n", SPIFFS_errno(&__fs)); } res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size); if (res != SPIFFS_OK) { printf("mount failed, %i\n", SPIFFS_errno(&__fs)); } #endif clear_flash_ops_log(); log_flash_ops = 1; fs_check_fixes = 0; }
void oc_bpt_test_fs_create( char *str_desc_p, int num_blocks, bool verbose) { ctx_p = fs_create(str_desc_p, num_blocks, verbose); }
int chips_create(const char *path, mode_t mode, struct fuse_file_info *fi) { if(chips_open(path, fi) == 0) return 0; if(!fs_create((char *)path, POTATOES_DATA_FILE)) return -ENOENT; //chips_truncate(path, 0); return chips_open(path, fi); }
int fs_open(char *file_name, int mode) { /*variavel primeiro contem o primeiro bloco do arquivo contido no diretorio*/ int i, primeiro; /*flag que é 1 se arq existe ou 0 caso contrário*/ char arq_existe=0; /*laço que procura no diretório o arquivo pelo seu nome*/ for(i=0; i<128; i++) if(!strcmp(dir[i].name, file_name)){ primeiro=dir[i].first_block; arq_existe=1; } /*id, global começa com 0, vai incrementando conforme abrimos arquivos*/ id++; /*se existe o arquivo no diretorio*/ if(arq_existe == 1){ /*procura primeira posição livre na lista de arquivos abertos*/ for(i=0; i<128; i++){ if(file_list[i].id == -1){ /*le bloco do arquivo e guarda em seu conteudo na lista*/ if(!le_bloco(file_list[i].conteudo, primeiro)) return -1; /*atribui um id pro arquivo*/ file_list[i].id = id; /*dependendo do modo, guarda isso no arquivo*/ if(mode == FS_R) file_list[i].mode = FS_R; if(mode == FS_W) file_list[i].mode = FS_W; /*retorna id do arquivo aberto*/ return file_list[i].id; } } /*caso contrário, se modo é de leitura, arquivo não existe*/ }else{ if(mode == FS_R){ printf("O arquivo não existe"); return -1; } /*se é modo de escrita, então temos que escrever um novo arquivo com tamanho 0*/ for(i=0; i<128; i++){ if(file_list[i].id == -1){ file_list[i].id = id; if(!fs_create(file_name)){ return 0; } file_list[i].mode = FS_W; return file_list[i].id; } } } return -1; }
static int chips_mkdir(int argc, char **argv) { if(argc != 2) { fprintf(stderr, "Usage: %s <path>\n", argv[0]); return -1; } if(!fs_create(argv[1], POTATOES_DIRECTORY)) { fprintf(stderr, "Failed to create directory %s\n", argv[1]); return 1; } return 0; }
// proc_init - set up the first kernel thread idleproc "idle" by itself and // - create the second kernel thread init_main void proc_init(void) { int i; int cpuid = myid(); struct proc_struct *idle; spinlock_init(&proc_lock); list_init(&proc_list); list_init(&proc_mm_list); for (i = 0; i < HASH_LIST_SIZE; i++) { list_init(hash_list + i); } idle = alloc_proc(); if (idle == NULL) { panic("cannot alloc idleproc.\n"); } idle->pid = cpuid; idle->state = PROC_RUNNABLE; // No need to be set for kthread (no privilege switch) // idleproc->kstack = (uintptr_t)bootstack; idle->need_resched = 1; idle->tf = NULL; if ((idle->fs_struct = fs_create()) == NULL) { panic("create fs_struct (idleproc) failed.\n"); } fs_count_inc(idle->fs_struct); char namebuf[32]; snprintf(namebuf, 32, "idle/%d", cpuid); set_proc_name(idle, namebuf); nr_process++; idleproc = idle; current = idle; int pid = ucore_kernel_thread(init_main, NULL, 0); if (pid <= 0) { panic("create init_main failed.\n"); } initproc = find_proc(pid); set_proc_name(initproc, "kinit"); char *proc_init="Proc init OK"; assert(idleproc != NULL && idleproc->pid == cpuid); assert(initproc != NULL && initproc->pid == sysconf.lcpu_count); }
int16_t parse_cmd_fs_mkfile (char *cmd, char *output, uint16_t len) { /* ignore leading spaces */ while (*cmd == ' ') cmd ++; fs_status_t ret = fs_create (&fs, cmd); if (ret != FS_OK) return ECMD_FINAL(snprintf_P(output, len, PSTR("fs_create: returned 0x%02x"), ret)); fs_inode_t i = fs_get_inode (&fs, cmd); return ECMD_FINAL(snprintf_P(output, len, PSTR("fs_create: inode 0x%04x"), i)); }
// proc_init - set up the first kernel thread idleproc "idle" by itself and // - create the second kernel thread init_main void proc_init(void) { int i; int lcpu_idx = pls_read(lcpu_idx); int lapic_id = pls_read(lapic_id); int lcpu_count = pls_read(lcpu_count); list_init(&proc_list); list_init(&proc_mm_list); for (i = 0; i < HASH_LIST_SIZE; i ++) { list_init(hash_list + i); } pls_write(idleproc, alloc_proc()); if (idleproc == NULL) { panic("cannot alloc idleproc.\n"); } idleproc->pid = lcpu_idx; idleproc->state = PROC_RUNNABLE; // XXX // idleproc->kstack = (uintptr_t)bootstack; idleproc->need_resched = 1; idleproc->tf = NULL; if ((idleproc->fs_struct = fs_create()) == NULL) { panic("create fs_struct (idleproc) failed.\n"); } fs_count_inc(idleproc->fs_struct); char namebuf[32]; snprintf(namebuf, 32, "idle/%d", lapic_id); set_proc_name(idleproc, namebuf); nr_process ++; pls_write(current, idleproc); int pid = kernel_thread(init_main, NULL, 0); if (pid <= 0) { panic("create init_main failed.\n"); } initproc = find_proc(pid); set_proc_name(initproc, "init"); assert(idleproc != NULL && idleproc->pid == lcpu_idx); assert(initproc != NULL && initproc->pid == lcpu_count); }
void proc_init_ap(void) { int cpuid = myid(); struct proc_struct *idle; idle = alloc_proc(); if (idle == NULL) { panic("cannot alloc idleproc.\n"); } idle->pid = cpuid; idle->state = PROC_RUNNABLE; // No need to be set for kthread (no privilege switch) // idle->kstack = (uintptr_t)bootstack; idle->need_resched = 1; idle->tf = NULL; if ((idle->fs_struct = fs_create()) == NULL) { panic("create fs_struct (idleproc) failed.\n"); } fs_count_inc(idle->fs_struct); char namebuf[32]; snprintf(namebuf, 32, "idle/%d", cpuid); set_proc_name(idle, namebuf); nr_process++; idleproc = idle; current = idle; #if 1 int pid; char proc_name[32]; if((pid = ucore_kernel_thread(krefcache_cleaner, NULL, 0)) <= 0){ panic("krefcache_cleaner init failed.\n"); } struct proc_struct* cleaner = find_proc(pid); snprintf(proc_name, 32, "krefcache/%d", myid()); set_proc_name(cleaner, proc_name); set_proc_cpu_affinity(cleaner, myid()); nr_process++; #endif assert(idleproc != NULL && idleproc->pid == cpuid); }
void fs_copy_file(fs_t *fs, inodeid_t dir2, inodeid_t file1id, char* file2) { inodeid_t file2id; fs_inode_t ifile1 = fs->inode_tab[file1id]; fs_create(fs, dir2, file2, &file2id); fs_inode_t ifile2 = fs->inode_tab[file2id]; for (int i = 0; i < INODE_NUM_BLKS && ifile1.blocks[i] != 0; i++) { char new_block[BLOCK_SIZE]; unsigned blockid = 0; block_read(fs->blocks,ifile1.blocks[i], new_block); fsi_bmap_find_free(fs->blk_bmap, BLOCK_SIZE, &blockid); block_write(fs->blocks, blockid, new_block); ifile2.blocks[i] = blockid; //ifile2.blocks[i]=ifile1.blocks[i]; } }
static int chips_append(int argc, char **argv) { if(argc != 2) { fprintf(stderr, "Usage: %s <file>\n", argv[0]); return -1; } int fd = do_open(argv[1]); if(fd < 0) { if(!fs_create(argv[1], POTATOES_DATA_FILE)) { fprintf(stderr, "Failed to create %s\n", argv[1]); return 1; } if((fd = do_open(argv[1])) < 0) { fprintf(stderr, "Failed to open %s\n", argv[1]); return 1; } } potatoes_file_info_t info; get_file_info(fd, &info); if(info.mode != POTATOES_DATA_FILE) { fprintf(stderr, "Not a file: %s\n", argv[1]); return 1; } char buf[512]; int count; int off = info.size; while((count = read(0, buf, 512)) > 0) { do_write(fd, buf, count, off); off += count; } return 0; }
int do_create(const char *filename,uint64_t lv,uint32_t ts,char *ptr) { uint32_t parent,mode,uid,gid,rdev,inode; uint8_t type,name[256]; EAT(ptr,filename,lv,'('); GETU32(parent,ptr); EAT(ptr,filename,lv,','); GETNAME(name,ptr,filename,lv,','); EAT(ptr,filename,lv,','); type = *ptr; ptr++; EAT(ptr,filename,lv,','); GETU32(mode,ptr); EAT(ptr,filename,lv,','); GETU32(uid,ptr); EAT(ptr,filename,lv,','); GETU32(gid,ptr); EAT(ptr,filename,lv,','); GETU32(rdev,ptr); EAT(ptr,filename,lv,')'); EAT(ptr,filename,lv,':'); GETU32(inode,ptr); return fs_create(ts,parent,strlen((char*)name),name,type,mode,uid,gid,rdev,inode); }
void fs_mount_dump(char *fname, u32_t addr_offset, u32_t phys_addr, u32_t phys_size, u32_t phys_sector_size, u32_t log_block_size, u32_t log_page_size) { fs_create(phys_size + phys_addr - addr_offset, phys_sector_size, log_page_size, DEFAULT_NUM_FD, DEFAULT_NUM_CACHE_PAGES); fs_set_addr_offset(addr_offset); memset(&AREA(addr_offset), 0xcc, _area_sz); memset(&AREA(phys_addr), 0xff, phys_size); memset(&__fs, 0, sizeof(__fs)); fs_load_dump(fname); s32_t res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size); ASSERT(res == SPIFFS_OK, "failed mounting dump, check settings"); clear_flash_ops_log(); log_flash_ops = 1; fs_check_fixes = 0; }
/* pre: takes in int 'argc' and char** 'argv' command line arguments which * include: * -f <file_list> * -d <dir_list> * -s <disk size> * -b <block size> * post: runs the filesystem simulation program * return: 0 on sucessful exit, something else on error */ int main(int argc, char** argv) { int n; char* line; char** v; /* parse args and setup global environment */ parse_args(argc, argv); /* read input files and initialize filesystem */ init(); /* main command reading loop */ line = (char*)malloc(CMD_LEN * sizeof(char)); bzero((void*)line, CMD_LEN * sizeof(char)); while (1) { /* prompt */ printf("%s ", PROMPT); fflush(stdout); /* fd 0 is stdin */ if ((n = read(0, line, CMD_LEN)) == -1) { perror(argv[0]); fs_exit(); exit(-1); } else if (n == 0) { fs_exit(); break; } else { /* overwrite newline with NULL-byte */ line[n - 1] = '\0'; #ifdef DEBUG printf("[DEBUG]\tRead line: <%s>\n", line); fflush(stdout); #endif /* use str2vect to break 'line' into a list of whitespace separated * strings */ v = str2vect(line); /* check for commands and execute the proper function */ if (!strcmp(v[0], "append")) { if (v[1] != NULL && v[2] != NULL) fs_append(v[1], atoi(v[2])); else { printf("usage: append name size\n"); fflush(stdout); } } else if (!strcmp(v[0], "cd")) { if (v[1] != NULL) fs_cd(v[1]); else { printf("usage: cd directory\n"); fflush(stdout); } } else if (!strcmp(v[0], "cd..")) /* just in case this is the intended command */ { fs_cd(".."); } else if (!strcmp(v[0], "create")) { if (v[1] != NULL) fs_create(v[1]); else { printf("usage: create name\n"); fflush(stdout); } } else if (!strcmp(v[0], "delete")) { if (v[1] != NULL) fs_delete(v[1]); else { printf("usage: delete name\n"); fflush(stdout); } } else if (!strcmp(v[0], "dir")) { fs_dir(); } else if (!strcmp(v[0], "exit")) { /* free the vector immediately and break to exit */ free_vect(v); break; } else if (!strcmp(v[0], "ls")) { fs_ls(); } else if (!strcmp(v[0], "mkdir")) { if (v[1] != NULL) fs_mkdir(v[1]); else { printf("usage: mkdir directory\n"); fflush(stdout); } } else if (!strcmp(v[0], "prdisk")) { fs_prdisk(); } else if (!strcmp(v[0], "prfiles")) { fs_prfiles(); } else if (!strcmp(v[0], "remove")) { if (v[1] != NULL && v[2] != NULL) fs_remove(v[1], atoi(v[2])); else { printf("usage: remove name size\n"); fflush(stdout); } } else { printf("%s: command not found: %s\n", argv[0], v[0]); fflush(stdout); } /* free the vector to avoid memory leaks */ free_vect(v); } } free(line); fs_exit(); return 0; }
int main( int argc, char *argv[] ) { char line[1024]; char cmd[1024]; char arg1[1024]; char arg2[1024]; int inumber, result, args; if(argc!=3) { printf("use: %s <diskfile> <nblocks>\n",argv[0]); return 1; } if(!disk_init(argv[1],atoi(argv[2]))) { printf("couldn't initialize %s: %s\n",argv[1],strerror(errno)); return 1; } printf("opened emulated disk image %s with %d blocks\n",argv[1],disk_size()); while(1) { printf(" prompt> "); fflush(stdout); if(!fgets(line,sizeof(line),stdin)) break; if(line[0]=='\n') continue; line[strlen(line)-1] = 0; args = sscanf(line,"%s %s %s",cmd,arg1,arg2); if(args==0) continue; if(!strcmp(cmd,"format")) { if(args==1) { if(fs_format()) { printf("disk formatted.\n"); } else { printf("format failed!\n"); } } else { printf("use: format\n"); } } else if(!strcmp(cmd,"mount")) { if(args==1) { if(fs_mount()) { printf("disk mounted.\n"); } else { printf("mount failed!\n"); } } else { printf("use: mount\n"); } } else if(!strcmp(cmd,"umount")) { if(args==1) { if(fs_umount()) { printf("disk umounted.\n"); } else { printf("umount failed!\n"); } } else { printf("use: mount\n"); } } else if(!strcmp(cmd,"debug")) { if(args==1) { fs_debug(); } else { printf("use: debug\n"); } } else if(!strcmp(cmd,"getsize")) { if(args==2) { inumber = atoi(arg1); result = fs_getsize(inumber); if(result>=0) { printf("inode %d has size %d\n",inumber,result); } else { printf("getsize failed!\n"); } } else { printf("use: getsize <inumber>\n"); } } else if(!strcmp(cmd,"create")) { if(args==1) { inumber = fs_create(); if(inumber>=0) { printf("created inode %d\n",inumber); } else { printf("create failed!\n"); } } else { printf("use: create\n"); } } else if(!strcmp(cmd,"delete")) { if(args==2) { inumber = atoi(arg1); if(fs_delete(inumber)) { printf("inode %d deleted.\n",inumber); } else { printf("delete failed!\n"); } } else { printf("use: delete <inumber>\n"); } } else if(!strcmp(cmd,"cat")) { if(args==2) { inumber = atoi(arg1); if(!do_copyout(inumber,"/dev/stdout")) { printf("cat failed!\n"); } } else { printf("use: cat <inumber>\n"); } } else if(!strcmp(cmd,"copyin")) { if(args==3) { inumber = atoi(arg2); if(do_copyin(arg1,inumber)) { printf("copied file %s to inode %d\n",arg1,inumber); } else { printf("copy failed!\n"); } } else { printf("use: copyin <filename> <inumber>\n"); } } else if(!strcmp(cmd,"copyout")) { if(args==3) { inumber = atoi(arg1); if(do_copyout(inumber,arg2)) { printf("copied inode %d to file %s\n",inumber,arg2); } else { printf("copy failed!\n"); } } else { printf("use: copyout <inumber> <filename>\n"); } } else if(!strcmp(cmd,"help")) { printf("Commands are:\n"); printf(" format\n"); printf(" mount\n"); printf(" umount\n"); printf(" debug\n"); printf(" create\n"); printf(" delete <inode>\n"); printf(" getsize <inode>\n"); printf(" cat <inode>\n"); printf(" copyin <file> <inode>\n"); printf(" copyout <inode> <file>\n"); printf(" help\n"); printf(" quit\n"); printf(" exit\n"); } else if(!strcmp(cmd,"quit")) { break; } else if(!strcmp(cmd,"exit")) { break; } else { printf("unknown command: %s\n",cmd); printf("type 'help' for a list of commands.\n"); result = 1; } } printf("closing emulated disk.\n"); disk_close(); return 0; }
int process_request(const char *line, FILE *fp, FileSystem *fs) { char command[4096]; #ifdef DEBUG fprintf(stderr, "process request `%s`\n", line); #endif sscanf(line, "%s", command); #ifdef DEBUG fprintf(stderr, "command is `%s`\n", command); #endif if (0 == strcmp("f", command)) { fs_format(fs); return RESULT_DONE; } else if (0 == strcmp("mk", command)) { char f[4096]; char parent_path[4096]; sscanf(line + 2, "%s", f); #ifdef DEBUG fprintf(stderr, "> mk `%s`\n", f); #endif if (fs_exists(fs, f)) { #ifdef DEBUG fprintf(stderr, "> `%s` already exists\n", f); #endif return RESULT_NO; } else { #ifdef DEBUG fprintf(stderr, "> going to create `%s`\n", f); #endif } fs_split_path(f, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_create(fs, f)) { #ifdef DEBUG fprintf(stderr, "> failed to create `%s`\n", f); #endif return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "> parent path `%s` is not a directory\n", parent_path); #endif return RESULT_NO; } else if (0 == strcmp("mkdir", command)) { char d[4096]; char parent_path[4096]; sscanf(line + 5, "%s", d); if (fs_exists(fs, d)) { return RESULT_NO; } fs_split_path(d, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_mkdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("rm", command)) { char f[4096]; sscanf(line + 2, "%s", f); if (fs_isfile(fs, f)) { if (fs_unlink(fs, f)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("cd", command)) { char path[4096]; sscanf(line + 2, "%s", path); if (fs_isdir(fs, path)) { if (fs_chdir(fs, path)) { return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "`%s` is not a directory\n", path); #endif return RESULT_NO; } else if (0 == strcmp("rmdir", command)) { char d[4096]; sscanf(line + 5, "%s", d); if (fs_isdir(fs, d)) { if (fs_rmdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("ls", command)) { fs_ls(fs, fp); return RESULT_ELSE; } else if (0 == strcmp("cat", command)) { char f[4096]; sscanf(line + 3, "%s", f); if (fs_isfile(fs, f)) { fs_cat(fs, f, fp); return RESULT_ELSE; } return RESULT_NO; } else if (0 == strcmp("w", command)) { char f[4096]; int l; char data[4096]; sscanf(line + 1, "%s %d %[^\n]", f, &l, data); if (fs_isfile(fs, f)) { if (fs_write(fs, f, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("i", command)) { char f[4096]; int pos; int l; char data[4096]; sscanf(line + 1, "%s %d %d %[^\n]", f, &pos, &l, data); if (fs_isfile(fs, f)) { if (fs_insert(fs, f, pos, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("d", command)) { char f[4096]; int pos; int l; sscanf(line + 1, "%s %d %d", f, &pos, &l); if (fs_isfile(fs, f)) { if (fs_delete(fs, f, pos, l)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("e", command)) { return RESULT_EXIT; } return RESULT_ELSE; }
int main( int argc, char *argv[] ) { char line[1024]; char cmd[1024]; char arg1[1024]; char arg2[1024]; int result, args; if(argc!=3) { printf("use: %s <diskfile> <nblocks>\n",argv[0]); return 1; } if(!disk_init(argv[1],atoi(argv[2]))) { printf("couldn't initialize %s: %s\n",argv[1],strerror(errno)); return 1; } printf("opened emulated disk image %s with %d blocks\n",argv[1],disk_size()); while(1) { printf(" prompt> "); fflush(stdout); if(!fgets(line,sizeof(line),stdin)) break; if(line[0]=='\n') continue; line[strlen(line)-1] = 0; args = sscanf(line,"%s %s %s",cmd,arg1,arg2); if(args==0) continue; if(!strcmp(cmd,"format")) { if(args==1) { if(!fs_format()) { printf("disk formatted.\n"); } else { printf("format failed!\n"); } } else { printf("use: format\n"); } } else if(!strcmp(cmd,"mount")) { if(args==1) { if(!fs_mount()) { printf("disk mounted.\n"); } else { printf("mount failed!\n"); } } else { printf("use: mount\n"); } } else if(!strcmp(cmd,"debug")) { if(args==1) { fs_debug(); } else { printf("use: debug\n"); } } else if(!strcmp(cmd,"getsize")) { if(args==2) { result = fs_getsize(arg1); if(result>=0) { printf("file %s has size %d\n",arg1,result); } else { printf("getsize failed!\n"); } } else { printf("use: getsize <filename>\n"); } } else if(!strcmp(cmd,"create")) { if(args==2) { result = fs_create(arg1); if(result==0) { printf("created file %s\n",arg1); } else { printf("create failed!\n"); } } else { printf("use: create <filename>\n"); } } else if(!strcmp(cmd,"delete")) { if(args==2) { if(!fs_delete(arg1)) { printf("file %s deleted.\n",arg1); } else { printf("delete failed!\n"); } } else { printf("use: delete <filename>\n"); } } else if(!strcmp(cmd,"cat")) { if(args==2) { if(!do_copyout(arg1,"/dev/stdout")) { printf("cat failed!\n"); } } else { printf("use: cat <name>\n"); } } else if(!strcmp(cmd,"copyin")) { if(args==3) { if(do_copyin(arg1,arg2)) { printf("copied file %s to %s\n",arg1,arg2); } else { printf("copy failed!\n"); } } else { printf("use: copyin <filename in host system> <filename in fs-miei-02>\n"); } } else if(!strcmp(cmd,"copyout")) { if(args==3) { if(do_copyout(arg1,arg2)) { printf("copied myfs_file %s to file %s\n", arg1,arg2); } else { printf("copy failed!\n"); } } else { printf("use: copyout <inumber> <filename>\n"); } } else if(!strcmp(cmd,"help")) { printf("Commands are:\n"); printf(" format\n"); printf(" mount\n"); printf(" debug\n"); printf(" create\n"); printf(" delete <filename>\n"); printf(" cat <filename>\n"); printf(" getsize <filename>\n"); printf(" copyin <file name in host system> <miei02-filename>\n"); printf(" copyout <miei02-filename> <file name in host system>\n"); printf(" dump <number_of_block_with_text_contents>\n"); printf(" help\n"); printf(" quit\n"); printf(" exit\n"); } else if(!strcmp(cmd,"quit")) { break; } else if(!strcmp(cmd,"exit")) { break; } else if (!strcmp(cmd, "dump")){ if(args==2) { int blNo = atoi(arg1); printf("Dumping disk block %d\n", blNo); char b[4096]; disk_read( blNo, b); printf("------------------------------\n"); printf("%s", b); printf("\n------------------------------\n"); } else { printf("use: dump <block_number>\n"); } } else { printf("unknown command: %s\n",cmd); printf("type 'help' for a list of commands.\n"); result = 1; } } printf("closing emulated disk.\n"); disk_close(); return 0; }
// Initialize the alternate block-map void oc_bpt_test_fs_alt_create(void) { oc_utl_assert(ctx_p); oc_utl_assert(NULL == alt_p); alt_p = fs_create("alternate", ctx_p->num_blocks, FALSE); }
/** * @ingroup shell * * Shell command fstest. * @param nargs number of arguments in args array * @param args array of arguments * @return OK for success, SYSERR for syntax error */ shellcmd xsh_fstest(int nargs, char *args[]) { int rval; int fd, i, j; char *buf1, *buf2; /* Output help, if '--help' argument was supplied */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s\n\n", args[0]); printf("Description:\n"); printf("\tFilesystem Test\n"); printf("Options:\n"); printf("\t--help\tdisplay this help and exit\n"); return OK; } /* Check for correct number of arguments */ if (nargs > 1) { fprintf(stderr, "%s: too many arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } if (nargs < 1) { fprintf(stderr, "%s: too few arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } #ifdef FS bs_mkdev(0, MDEV_BLOCK_SIZE, MDEV_NUM_BLOCKS); /* device "0" and default blocksize (=0) and count */ fs_mkfs(0,DEFAULT_NUM_INODES); /* bsdev 0*/ testbitmask(); buf1 = getmem(SIZE*sizeof(char)); buf2 = getmem(SIZE*sizeof(char)); // Create test file fd = fs_create("Test_File", O_CREAT); // Fill buffer with random stuff for(i=0; i<SIZE; i++) { j = i%(127-33); j = j+33; buf1[i] = (char) j; } rval = fs_write(fd,buf1,SIZE); if(rval == 0 || rval != SIZE ) { printf("\n\r File write failed"); goto clean_up; } // Now my file offset is pointing at EOF file, i need to bring it back to start of file // Assuming here implementation of fs_seek is like "original_offset = original_offset + input_offset_from_fs_seek" fs_seek(fd,-rval); //read the file rval = fs_read(fd, buf2, rval); buf2[rval] = '\0'; if(rval == 0) { printf("\n\r File read failed"); goto clean_up; } printf("\n\rContent of file %s",buf2); rval = fs_close(fd); if(rval != OK) { printf("\n\rReturn val for fclose : %d",rval); } clean_up: freemem(buf1,SIZE); freemem(buf2,SIZE); #else printf("No filesystem support\n"); #endif return OK; }
uchar cmd_copyFile(char* from, char* to) { char buf[16]; uchar e, progress_i=0; ulong progress; ulong progress_x, progress_step; // Открываем исходный файл и получаем его длину if(e = fs_open(from)) return e; if(e = fs_getsize()) return e; // Расчет шага прогресса set32(&progress_step, &fs_result); div32_16(&progress_step, 40); // Интерфейс drawWindow(" kopirowanie "); drawWindowText(0, 0, "iz:"); drawWindowText(4, 0, from); drawWindowText(0, 1, "w:"); drawWindowText(4, 1, to); drawWindowText(0, 2, "skopirowano / bajt"); drawWindowProgress(0, 3, progress_width, '#'); i2s32(buf, &fs_result, 10, ' '); drawWindowText(23, 2, buf); drawEscButton(); // Создаем новый файл if(e = fs_swap()) return e; if(e = fs_create(to)) return e; // Копирование SET32IMM(&progress, 0); SET32IMM(&progress_x, 0); for(;;) { // Вывод прогресса i2s32(buf, &progress, 10, ' '); drawWindowText(12, 2, buf); // Копирование блока if(e = fs_swap()) return e; if(e = fs_read(panelB.files1, 1024) ) return e; if(fs_low == 0) return 0; // С перезагрузкой файлов if(e = fs_swap()) return e; if(e = fs_write(panelB.files1, fs_low)) return e; // Это недоработка компилятора, Он не поддерживает 32-х битной арифметиики add32_16(&progress, fs_low); // Прогресс add32_16(&progress_x, fs_low); while(progress_i < progress_width && cmp32_32(&progress_x, &progress_step) != 1) { sub32_32(&progress_x, &progress_step); drawWindowText(progress_i, 3, "\x17"); ++progress_i; } // Прерывание if(fs_bioskey(1) == KEY_ESC) { e = ERR_USER; break; } } // Удалить файл в случае ошибки. Ошибку удаления не обрабатываем. fs_delete(to); return e; }