int do_open(char * filename, int flags, int mode) { iNode * posible_file = search_directory(filename, current); int fd; if (posible_file != NULL) { if (posible_file->gid < currentUsr.group) return -2; if (posible_file->identifier == LINK) { posible_file = fs_get_inode(posible_file->link); } if ((fd = search_for_inode(posible_file->iNode_number)) != -1) { return fd; } else { return insert_fd(posible_file->iNode_number); } } else { if (flags == 0) { do_creat(filename, mode); } else { return -1; } } }
void create_n_bytes(char * name) { int size, fd, i, a; size = 10485760; //10mb int cant = size / (512); char * buffer = malloc(BLOCK_SIZE); fd = do_creat(name, 777); printf("SIZE:%d\n", size); for (i = 0; i < (512); i++) { buffer[i] = '2'; } buffer[i] = '\0'; printf("len:%d\n", str_len(buffer)); printf("cant:%d\n", cant); for (i = 0; i < cant; i++) { a = write(fd, buffer, str_len(buffer)); //printf("i:%d,a:%d\n",i,a); } printf("Se escrbieron:%d\n", getsize(fd)); close(fd); return; }
int creat_in_kernel(creat_param * param) { char * filename = malloc(str_len(param->filename)); int mode = param->mode; memcpy(filename, param->filename, str_len(filename)); return do_creat(filename, 777); }
void touch_in_kernel(char * filename) { int fd; if ((fd = do_creat(filename, 777)) == -1) { printf("\nCreat error"); } else { printf("\nFile created succesfully"); } return; }
void init_filesystem(char * filesystem_name, masterBootRecord * mbr) { /*mbr sector*/ int fd; user * users = calloc(sizeof(user), 100); memcpy(users[0].name, "chinux", str_len("chinux")); memcpy(users[0].password, "chinux", str_len("chinux")); users[0].usrID = 1; users[0].group = ADMIN; mbr->existFS = 1; write_disk(0, MBRSECTOR, mbr, BLOCK_SIZE, 0); //BLOCK_SIZE /* superBlock sector */ superblock->name = "Chinux"; superblock->blockSize = BLOCK_SIZE; superblock->freeBlocks = DISK_SIZE / BLOCK_SIZE; superblock->usedBlocks = 0; superblock->root = NULL; write_disk(0, SUPERBLOCKSECTOR, superblock, BLOCK_SIZE, 0); /* bitmap Sectors */ init_bitmap(); /* inodemap Sectors */ init_inodemap(); /* Root node & current node */ init_root(); write_disk(0, 1, superblock, BLOCK_SIZE, 0); current = superblock->root; makeDir("users"); makeDir("etc"); fd = do_creat("usersfile", 777); write(fd, (char *) users, sizeof(user) * 100); close(fd); return; }
void createusr(char * name, char * password, char * group) { int i, fd, length; user * usr; iNode * aux; aux = current; current = superblock->root; fd = do_open("usersfile", 777, 777); usr = malloc(sizeof(user) * 100); do_read(fd, (char *)usr, sizeof(user) * 100); for(i = 0; i < 100 && usr[i].usrID; i++) if(strcmp(usr[i].name, name)) { printf("Error: User already exists.\n"); return ; } if(i == 100) { printf("Error: Too many users created.\n"); return ; } length = str_len(name); name[length - 1] = 0; memcpy(usr[i].name, name, length - 1); length = str_len(password); password[length - 1] = 0; memcpy(usr[i].password, password, length - 1); usr[i].usrID = ++usrID; if(strcmp(group, "admin")) usr[i].group = ADMIN; else usr[i].group = USR; do_close(fd); rmDir("usersfile"); fd = do_creat("usersfile", 777); write(fd, (void *)usr, sizeof(user) * 100); do_close(fd); current = aux; //free(usr); return; }
int open_test() { int ret = 0; printf("open_test: Start\n"); /* * Create new file */ printf("\tbegin: Create new file(%s) \n", NEW_FILE_PATH_1); if((ret = do_creat(NEW_FILE_PATH_1, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) != 0){ goto out; } printf("\tend: Success\n"); /* * Open Existing file */ printf("\tbegin: Open existing file(%s) with O_RDONLY flag\n", NEW_FILE_PATH_1); if((ret = do_open(NEW_FILE_PATH_1, O_RDONLY, 0)) != 0){ goto out; } printf("\tend: Success\n"); /* * Open new file with O_CREAT */ printf("\tbegin: Open new file(%s) with O_CREAT flag\n", NEW_FILE_PATH_2); if((ret = do_open(NEW_FILE_PATH_2, O_WRONLY | O_CREAT | O_TRUNC| O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) != 0){ goto out; } printf("\tend: Success\n"); /* * Open Existing file with O_EXCL & O_CREATE flag */ printf("\tbegin: Open existing file(%s) with O_EXCL & O_CREAT flag\n", NEW_FILE_PATH_1); if((do_open(NEW_FILE_PATH_1, O_WRONLY | O_CREAT | O_TRUNC| O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) != EEXIST){ ret = 1; goto out; } printf("\tend: Success\n"); /* * Create new file on newly created dir */ printf("\tbegin: Open new file(%s) on new directory\n", NEW_FILE_PATH_5); if((ret = do_creat(NEW_FILE_PATH_5, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) != 0){ goto out; } printf("\tend: Success\n"); out: if (ret) { printf("\tend: Failed\n"); } printf("open_test: Finish\n"); return ret; }