예제 #1
0
void cmd_open(void) {
  uint8_t mode;
 
  /* Принимаем режим */
  mode = wrecv();    

  // Принимаем имя файла
  recvString();

  // Режим передачи и подтверждение
  sendStart(ERR_WAIT);

  // Открываем/создаем файл/папку
  if(mode == O_SWAP) {
    fs_swap();
  } else
  if(mode == O_DELETE) {
    fs_delete();
  } else
  if(mode == O_OPEN) {
    fs_open();
  } else 
  if(mode < 3) {
    fs_open0(mode);
  } else {
    lastError = ERR_INVALID_COMMAND;
  }

  // Ок
  if(!lastError) lastError = ERR_OK_CMD;
}
예제 #2
0
static int chips_rm(int argc, char **argv)
{
	if(argc != 2)
	{
		fprintf(stderr, "Usage: %s <path>\n", argv[0]);
		return -1;
	}
	int fd = do_open(argv[1]);
	if(fd < 0)
	{
		fprintf(stderr, "Failed to open %s\n", argv[1]);
		return 1;
	}

	potatoes_file_info_t info;
	get_file_info(fd, &info);
	do_close(fd);

	if(info.mode != POTATOES_DATA_FILE)
	{
		fprintf(stderr, "Not a file: %s\n", argv[1]);
		return 1;
	}

	if(!fs_delete(argv[1]))
	{
		fprintf(stderr, "Failed to delete %s\n", argv[1]);
		return 1;
	}
	return 0;
}
예제 #3
0
파일: fs.c 프로젝트: qinpan1003/linux_drv
//--------------------------------------------------------------------------------------------------*/
// 文件系统主循环任务
void task_fs(void * pdata)
{
	MSG 	msg;
	FS_MSG* 	p_fs_msg;
	pdata = pdata;
	int src;
	init_fs();
	while(1)
	{
//--------------------------------------------------------------------------------------------------*/
//
		assert(recv(ANY,&msg) == 0);
		assert(	msg.type == FS_MSG_UNION	);
		src	= msg.sender;
		p_fs_msg	= &msg.msg_union.fs_msg;
		switch(p_fs_msg->para)
		{
			case FS_OPEN:
				p_fs_msg->fd = fs_open(p_fs_msg->pathname,p_fs_msg->flags,task_table + msg.sender);
				break;
			case FS_WRITE:
				p_fs_msg->count = fs_write(p_fs_msg->fd,p_fs_msg->buf,p_fs_msg->offset,p_fs_msg->count,task_table + msg.sender);
  				break;
			case FS_READ:
				p_fs_msg->count = fs_read(p_fs_msg->fd,p_fs_msg->buf,p_fs_msg->offset,p_fs_msg->count,task_table + msg.sender);
           			break;
			case FS_CLOSE:
				p_fs_msg->para = fs_close(p_fs_msg->fd,task_table + msg.sender);
				break;
			case FS_DEL:
				p_fs_msg->para = fs_delete(va2pa((task_table + msg.sender)->pdb,p_fs_msg->pathname),p_fs_msg->flags);
				break;
			case FS_RESUME:
				src = p_fs_msg->flags;	// 待解除阻塞的进程ID(利用了flags这个成员返回PID)
				assert(0 <= src && src < NR_TOTAL);
				break;			// p_fs_msg->count:-1 请求失败(有其他进程正在请求输入),其他值表示输入字节数
			case FS_FORK:
				p_fs_msg->para	= fs_do_fork(p_fs_msg->flags);
				break;
			case FS_EXIT:
				fs_do_exit(p_fs_msg->flags);
				break;
			case FS_LSEEK:
				p_fs_msg->para	= fs_do_lseek(p_fs_msg->fd,p_fs_msg->offset,p_fs_msg->flags,task_table + msg.sender);
				break;
			default:	break;
		}
		if(no_answer == 1)
			no_answer = 0;			// 解除下一个tty请求的阻塞
		else
			send(src,&msg);
//--------------------------------------------------------------------------------------------------*/
	}
}
예제 #4
0
파일: main.c 프로젝트: parmaJon/SegFault
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;
	
}
예제 #5
0
int chips_unlink(const char *path)
{
	int fd = do_open((char *)path);
	if(fd < 0)
		return -ENOENT;

	potatoes_file_info_t info;
	get_file_info(fd, &info);

	if(info.mode != POTATOES_DATA_FILE)
		return -EPERM;
	do_close(fd);

	return fs_delete((char *)path) ? 0 : -1;
}
예제 #6
0
/*
 * os_auto_flush -- check if platform supports auto flush for all regions
 *
 * Traverse "/sys/bus/nd/devices" path to find all the nvdimm regions,
 * then for each region checks if "persistence_domain" file exists and
 * contains "cpu_cache" string.
 * If for any region "persistence_domain" entry does not exists, or its
 * context is not as expected, assume eADR is not available on this platform.
 */
int
os_auto_flush(void)
{
	LOG(15, NULL);

	char *device_path;
	int cpu_cache = 0;

	device_path = BUS_DEVICE_PATH;

	os_stat_t sdev;
	if (os_stat(device_path, &sdev) != 0 ||
		S_ISDIR(sdev.st_mode) == 0) {
		LOG(3, "eADR not supported");
		return cpu_cache;
	}

	struct fs *dev = fs_new(device_path);
	if (dev == NULL) {
		ERR("!fs_new: \"%s\"", device_path);
		return -1;
	}

	struct fs_entry *dev_entry;

	while ((dev_entry = fs_read(dev)) != NULL) {
		/*
		 * Skip if not a symlink, because we expect that
		 * region on sysfs path is a symlink.
		 * Skip if depth is different than 1, bacause region
		 * we are interested in should be the first level
		 * child for device.
		 */
		if ((dev_entry->type != FS_ENTRY_SYMLINK) ||
				!strstr(dev_entry->name, "region") ||
				dev_entry->level != 1)
			continue;

		LOG(15, "Start traversing region: %s", dev_entry->path);
		cpu_cache = check_domain_in_region(dev_entry->path);
		if (cpu_cache != 1)
			goto end;
	}

end:
	fs_delete(dev);
	return cpu_cache;
}
예제 #7
0
/*
 * check_domain_in_region -- (internal) check if region
 * contains persistence_domain file
 */
static int
check_domain_in_region(const char *region_path)
{
	LOG(3, "region_path: %s", region_path);

	struct fs *reg = NULL;
	struct fs_entry *reg_entry;
	char domain_path[PATH_MAX];
	int cpu_cache = 0;

	reg = fs_new(region_path);
	if (reg == NULL) {
		ERR("!fs_new: \"%s\"", region_path);
		cpu_cache = -1;
		goto end;
	}

	while ((reg_entry = fs_read(reg)) != NULL) {
		/*
		 * persistence_domain has to be a file type entry
		 * and it has to be first level child for region;
		 * there is no need to run into deeper levels
		 */
		if (reg_entry->type != FS_ENTRY_FILE ||
				strcmp(reg_entry->name,
				PERSISTENCE_DOMAIN) != 0 ||
				reg_entry->level != 1)
			continue;

		int ret = snprintf(domain_path, PATH_MAX,
			"%s/"PERSISTENCE_DOMAIN,
			region_path);
		if (ret < 0) {
			ERR("snprintf(%p, %d,"
				"%s/"PERSISTENCE_DOMAIN", %s): %d",
				domain_path, PATH_MAX,
				region_path, region_path, ret);
			cpu_cache = -1;
			goto end;
		}
		cpu_cache = check_cpu_cache(domain_path);
	}

end:
	if (reg)
		fs_delete(reg);
	return cpu_cache;
}
void fs_sis_try_unlink_hash_file(struct fs *sis_fs, struct fs_file *super_file)
{
	struct fs_file *hash_file;
	struct stat st1, st2;
	const char *dir, *hash, *hash_path;

	if (fs_sis_path_parse(sis_fs, super_file->path, &dir, &hash) == 0 &&
	    fs_stat(super_file, &st1) == 0 && st1.st_nlink == 2) {
		/* this may be the last link. if hashes/ file is the same,
		   delete it. */
		hash_path = t_strdup_printf("%s/"HASH_DIR_NAME"/%s", dir, hash);
		hash_file = fs_file_init(super_file->fs, hash_path,
					 FS_OPEN_MODE_READONLY);
		if (fs_stat(hash_file, &st2) == 0 &&
		    st1.st_ino == st2.st_ino &&
		    CMP_DEV_T(st1.st_dev, st2.st_dev)) {
			if (fs_delete(hash_file) < 0)
				i_error("%s", fs_last_error(hash_file->fs));
		}
		fs_file_deinit(&hash_file);
	}
}
예제 #9
0
파일: resp.c 프로젝트: edelkind/filthttp
void
resp_rmfile(struct reqinfo *req)
{
    lx_s data = {0};

    if (!OPT_WRITABL) {
        die_html(HTERR_FORBID, 0,
                "Write access forbidden by configuration",
                err_perm_denied);
    }

    if (lx_check0(&req->location)) die_nomem();
    fs_delete(req->location.s);

    if (lx_striset(&data, "0", 1)) die_nomem();
    header_setstr(blob_header_send, "Content-Length", data.s, data.len);

    resp_sendprefix(req, HTERR_OK);
    resp_sendheaders(req, blob_header_send);

    lx_free(&data);
}
예제 #10
0
파일: commands.c 프로젝트: 12019/SOSSE
void cmd_delete( void )
{
	S_FSTREAM fs;
	S_FINFO fi;
	iu16 fid;
	iu8 b;

	if( !t0_testP3( sizeof(iu16) ) ) 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]&0x0F ) ) return;

	/* ACK */
	t0_sendAck();

	/* Data */
	b = hal_io_recByteT0();
	fid=b<<8;
	b = hal_io_recByteT0();
	fid+=b;

	if( !fs_delete( fid ) ) return;

	sw_set( SW_OK );
}
예제 #11
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;
}
예제 #12
0
파일: fs.c 프로젝트: liwei606/OS_proj
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;
}
예제 #13
0
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;
}
예제 #14
0
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;
}
예제 #15
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;
}