/** * Usage: -f file -i - create a filesystem with name 'file' * Usage: -f file -o - open a filesystem 'file' * Usage: -f file -c path - create a file with path 'path' in a filesystem 'file' */ int main(int argc, char ** argv) { char *value = 0; int option; while ((option = getopt (argc, argv, "f:ioc:")) != -1) { switch(option) { case 'f': value = optarg; break; case 'i': if(value != 0) { init_file_system(value); } break; case 'o': open_file_system(value); break; case 'c': creat_file(value, optarg); break; } } int fdfs = simplefs_openfs("filesystem"); simplefs_creat("/a.txt", fdfs); int fd = simplefs_open("/a.txt", READ_AND_WRITE, fdfs); printf("Result of simplefs_open %d\n", fd); printf("Wrint to file result %d", simplefs_write(fd, "adam to glupi programista", 15, fdfs)); char result[20]; simplefs_lseek(fd, SEEK_SET,0,fdfs); printf("Readig from file %d", simplefs_read(fd, result, 10, fdfs)); result[10] = '\0'; printf("================Read================\n %s\n", result); return 0; }
static struct inode *alloc_inode(struct initfs_inode *ii, struct inode *dir) { struct inode *inode; /* initfs supports regular files only */ inode = creat_file(dir, ii->file_name); inode->i_fop = &initfs_fops; inode->i_size = ii->file_size; inode->i_private = (char *)ii + align_next(2 + strlen(ii->file_name) + 1, 4); return inode; }
int main(int argc, const char *argv[]) { stu_t *p, *head_new; int i = 0; //char str[10] = "111"; //creat_file(2, str); //p = creat_link(NULL, str, 2); //char str[10] = "111"; creat_file(2, argv[1]); p = creat_link(NULL, argv[1], 2); head_new = sort(p); for (i = 0; i < 2; i++) { printf("%d\n", head_new->id); head_new = head_new->next; } return 0; }
//update time if it exists, if it doesn't exist, it creates it void touch_file(char path[124]) { int ino; int newmode = 0; MINODE *mip = running->cwd; MINODE *touchmip = NULL; INODE *ip = NULL; char fullpath[128]; strcpy(fullpath, path); if (!strcmp(path, "")) { printf("No pathname given!\n"); return; } printf("path = %s\n", path); ino = getino(running->cwd, path); if (ino != 0) //The target exists, touch it { printf("The target exists, touching...\n"); touchmip = iget(dev, ino); ip = &touchmip->INODE; ip->i_mtime = time(0L);//update time touchmip->dirty = 1; iput(touchmip); return; } else //The target doesnt exist and we must make a file { printf("The target does not exists, creating file...\n"); creat_file(fullpath); } return; }
PUBLIC int do_open() { int i = 0; int fd = -1; // return value char pathname[MAX_PATH]; int flags = fs_msg.FLAGS; int name_len = fs_msg.NAME_LEN; int src = fs_msg.source; assert(name_len < MAX_PATH); phys_copy( (void *)va2la(TASK_FS, pathname), (void *)va2la(src, fs_msg.PATHNAME), name_len ); pathname[name_len] = 0; /* 先在调用者进程中查找空闲文件描述符 */ for (i = 0; i < NR_FILES; i++) if (pcaller->filp[i] == 0) { fd = i; break; } if ((fd < 0) || (fd >= NR_FILES)) panic("filp[] is full (PID:%d)", proc2pid(pcaller)); /* 然后在 f_desc_table 中查找空闲位置 */ for (i = 0; i < NR_FILE_DESC; i++) if (f_desc_table[i].fd_inode == 0) break; if (i >= NR_FILE_DESC) panic("f_desc_table[] is full (PID:%d)", proc2pid(pcaller)); /* 在磁盘中查找文件 */ int inode_nr = search_file(pathname); /* 准备创建或打开文件 */ struct inode *pin = 0; if (flags & O_CREAT) { if (inode_nr) { printl("file exists.\n"); return -1; } else { // 文件不存在且标志位 O_CREAT pin = creat_file(pathname, flags); } } else { assert(flags & O_RDWR); char filename[MAX_PATH]; struct inode * dir_inode; if (strip_path(filename, pathname, &dir_inode) != 0) return -1; pin = get_inode(dir_inode->i_dev, inode_nr); } /* 关联文件描述符 */ if (pin) { /* proc <- fd (connects proc with file_descriptor) */ pcaller->filp[fd] = &f_desc_table[i]; /* fd <- inode (connects file_descriptor with inode) */ f_desc_table[i].fd_mode = flags; f_desc_table[i].fd_pos = 0; f_desc_table[i].fd_inode = pin; int imode = pin->i_mode & I_TYPE_MASK; if (imode == I_CHAR_SPECIAL) { MESSAGE driver_msg; driver_msg.type = DEV_OPEN; int dev = pin->i_start_sect; driver_msg.DEVICE = MINOR(dev); assert(MAJOR(dev) == 4); assert(dd_map[MAJOR(dev)].driver_nr != INVALID_DRIVER); /* 如果是字符设备则交给该设备的驱动处理 */ send_recv(BOTH, dd_map[MAJOR(dev)].driver_nr, &driver_msg); } else if (imode == I_DIRECTORY) assert(pin->i_num == ROOT_INODE); else assert(pin->i_mode == I_REGULAR); } else return -1; // open file failed return fd; }
void main(int argc, char *argv[]) { int i = 0, cmd = -1, fd = 0; char line[128], cname[64], temp_pathname[124], pathname[124] = "NULL", basename[124] = "NULL", dirname[124] = "NULL", parameter[124] = "NULL", *device; //GETS DISK NAME TO MOUNT /* printf("Enter the name of your disk image\n"); fgets(device, 128, stdin); device[strlen(device)-1] = 0; // kill the \n char at end */ device = "mydisk"; mount_root(device, &fd); while(1) { //printf("P%d running: ", running.uid); printf("nick's@linux:"); pwd(P0.cwd); printf("$ "); fgets(line, 128, stdin); line[strlen(line)-1] = 0; // kill the \n char at end if (line[0]==0) continue; sscanf(line, "%s %s %s", cname, pathname, parameter); if(strcmp(pathname,"NULL") != 0) strcpy(PATHNAME, pathname); if(strcmp(parameter,"NULL") != 0) strcpy(PARAMETER, parameter); cmd = findCmd(cname); // map cname to an index switch(cmd) { case 0 : menu(); break; case 1 : pwd(P0.cwd);printf("\n"); break; case 2 : ls(); break; case 3 : cd(); break; case 4 : make_dir(); break; case 5 : rmdir(); break; case 6 : creat_file(); break; case 7 : link(); break; case 8 : unlink(); break; case 9 : symlink(); break; case 10: rm_file(); break; case 11: chmod_file(); break; case 12: chown_file(); break; case 13: stat_file(); break; case 14: touch_file(); break; case 20: open_file(); break; //LEVEL 2 case 21: close_file((int)PATHNAME); break; case 22: pfd(); break; case 23: lseek_file(); break; case 24: //access_file(); break; break; case 25: //read_file(); break; case 26: write_file(); break; case 27: cat_file(); break; case 28: cp_file(); break; case 29: mv_file(); break; case 30: mount(); break; //LEVLEL 3 case 31: //umount(); break; case 32: //cs(); break; case 33: //do_fork(); break; case 34: //do_ps(); break; case 40: //sync(); break; case 41: quit(); break; case 42 : system("clear"); break; default: printf("invalid command\n"); break; } //RESET NAMES strcpy(parameter, "NULL"); strcpy(pathname, "NULL"); strcpy(PATHNAME, "NULL"); } }
void copy_file() { //cp src dest //pathname = src //parameter = dest //make a local copy char source[128]; char destination[128]; int byte_count; int nBytes; char buf[1024]; strcpy(source, pathname); strcpy(destination, parameter); //open src for read strcpy(pathname, source); strcpy(parameter, "0"); //Read mode int fd = open_file(); //open dst for WR|CREAT strcpy(pathname, destination); strcpy(parameter, "1"); //Write mode int gd = open_file(); //if gd fails to open, mean the location is not there. if (gd == -1) { //create that file. //Assuming that the directory containing the file is actually exists //Need syntax check. (what creat_file use) creat_file(); //try to open the file again. gd = open_file(); } //the file at both src and dest is opened for business, OFT* oftp_src = running->fd[fd]; OFT* oftp_dest = running->fd[gd]; MINODE* mip_src = oftp_src->minodeptr; MINODE* mip_dest = oftp_dest->minodeptr; //get the total number of bytes that we have to copy. nBytes = mip_src->INODE.i_size; while(nBytes > 0) { //try to get more data from the src. byte_count = myread(fd, buf, 1024); //write the amount of bytes that was read into the destination file mywrite(gd, buf, byte_count); //decrease the number of bytes that we need to read nBytes -= byte_count; } //done with the file, close it. close_file(fd); close_file(gd); }