int main (int argc, char *argv[]) { DIRST dir; char *directory, *filename, *slash; if (argc == 1 || streq (argv [1], "/?")) { puts ("dos2unix - part of the iMatix RealiBase Gaggle"); puts ("Strip CR characters from text file.\n"); puts ("syntax: dos2unix [/?] [filename]"); puts (" /? - show help on command"); puts (" filename - file to expand"); exit (0); } filename = argv [1]; if (strchr (filename, '*') || strchr (filename, '?')) { directory = mem_strdup (filename); slash = strrchr (directory, '\\'); if (!slash) slash = strrchr (directory, '/'); if (!slash) directory = "."; else { *slash = '\0'; filename = slash + 1; } if (open_dir (&dir, directory)) do { if ((dir.file_attrs & ATTR_HIDDEN) == 0 && (dir.file_attrs & ATTR_SUBDIR) == 0) if (file_matches (dir.file_name, filename)) convert (directory, dir.file_name); } while (read_dir (&dir)); close_dir (&dir); } else convert (".", filename); exit (0); }
int fchdir(int fd) { struct DIR_FILE * dirf = &dir_file_list[fd]; int olddir_handle; if (dirf->flags & FTABLE_FLAGS_DIR) { if (dirf->device == fileinf->current_drive) { olddir_handle = get_curdir_handle(fileinf->current_drive); increment_ref_count(fd); set_drive_curdir(dirf->device, fd); close_dir(olddir_handle); return 0; } else return EINVALID_ARGUMENT; } else return EINVALID_DIR_INDEX; }
int processing(char *path1, char *path2) { DIR *dir1, *dir2; DIRENT *curPath1, *curPath2; STAT curInfo1, curInfo2; char pathName1[PATH_MAX], pathName2[PATH_MAX]; int cmpFlag = 0, N = 0; pid_t pid; dir1 = dir2 = NULL; curPath1 = curPath2 = NULL; open_dir(&dir1, path1); open_dir(&dir2, path2); if (dir1 == NULL) { return 0; } errno = 0; while ((curPath1 = readdir(dir1)) != NULL) { if ((!strcmp((*curPath1).d_name, ".")) || (!strcmp((*curPath1).d_name, ".."))) { continue; } // full PATH sprintf(pathName1, "%s/%s", path1, (*curPath1).d_name); if (stat(pathName1, &curInfo1) == 0) { if (S_ISDIR(curInfo1.st_mode)) { continue; } else { ///////////////////////////////////////////////////////////////////////////////////// cmpFlag = 0; rewinddir(dir2); errno = 0; while ((curPath2 = readdir(dir2)) != NULL) { if ((!strcmp((*curPath2).d_name, ".")) || (!strcmp((*curPath2).d_name, ".."))) { continue; } // full PATH sprintf(pathName2, "%s/%s", path2, (*curPath2).d_name); if (stat(pathName2, &curInfo2) == 0) { if (S_ISDIR(curInfo2.st_mode)) { continue; } else { ///////////////////////////////////////////////////////////////////////////////////// if (strcmp((*curPath1).d_name, (*curPath2).d_name) == 0) { cmpFlag = 1; break; } ////////////////////////////////////////////////////////////////////////////////////// } } else { fprintf(stderr, "%s: %s: %s\n", program, strerror(errno), pathName2); } errno = 0; } if (errno) { fprintf(stderr, "%s: %s: %s\n", program, strerror(errno), pathName2); } ////////////////////////////////////////////////////////////////////////////////////// } } else { fprintf(stderr, "%s: %s: %s\n", program, strerror(errno), pathName2); } ////////////////////COPYYYYYYYYYYYYYYYYY///////////////////////// if (!cmpFlag) { if ((pid = fork()) == 0) { printf("%d: %s: %ldb\n", getpid(), pathName1, curInfo1.st_size); execlp("cp", program, pathName1, path2, NULL); break; } else { //printf("COUNT FORKS: %d\n", N); if (pid > 0) { //waitpid(pid, NULL, 0); if (N >= maxN) { wait(NULL); N--; } else { N++; } } else { if (pid == -1) { fprintf(stderr, "%s: %s: %s\n", program, strerror(errno), pathName2); } } } } errno = 0; } if (errno) { fprintf(stderr, "%s: %s: %s\n", program, strerror(errno), pathName1); } close_dir(&dir1, path1); close_dir(&dir2, path2); return 0; }
TA_RetCode TA_DirectoryAlloc( const char *path, TA_Directory **directory ) { #if defined( USE_WIN32_API ) HANDLE handle; WIN32_FIND_DATA data; DWORD win32Error; #endif #if defined( USE_OSLAYER ) DIRST dirHandle; const char *filePattern; char *basePath; #endif unsigned pathLength; int findNextRetCode; TA_Directory *dir; TA_String *string; TA_RetCode retCode; TA_SystemGlobal *global; const char *entryName; unsigned int entryIsDirectory; *directory = NULL; if( (path == NULL) || (directory == NULL) ) return TA_BAD_PARAM; retCode = TA_GetGlobal( &TA_SystemGlobalControl, (void **)&global ); if( retCode != TA_SUCCESS ) return retCode; dir = (TA_Directory *)TA_Malloc( sizeof( TA_Directory ) ); if( dir == NULL ) return TA_ALLOC_ERR; dir->nbFile = 0; dir->nbDirectory = 0; dir->listOfFile = TA_ListAlloc(); dir->listOfDirectory = TA_ListAlloc(); if( (dir->listOfFile == NULL) || (dir->listOfDirectory == NULL) ) { TA_DirectoryFree( dir ); return TA_ALLOC_ERR; } /* Verify that the path is valid. */ pathLength = strlen( path ); if( (pathLength == 0) || (pathLength >= MAX_PATH) ) { TA_DirectoryFree( dir ); return TA_BAD_PARAM; } /* Now get the directory from the operating system. */ #if defined( USE_WIN32_API ) handle = FindFirstFile( path, &data ); if( handle == INVALID_HANDLE_VALUE ) { win32Error = GetLastError(); global->lastError = win32Error; if( (win32Error != ERROR_FILE_NOT_FOUND) && (win32Error != ERROR_PATH_NOT_FOUND) ) { TA_DirectoryFree( dir ); return TA_ACCESS_FAILED; } /* No files or directory... but still have to pass the result * to the caller. */ *directory = dir; return TA_SUCCESS; } entryName = data.cFileName; entryIsDirectory = data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; #endif #if defined( USE_OSLAYER ) /* Split the path into the basePath and the filePattern. */ basePath = TA_Malloc( pathLength+1 ); memcpy( basePath, path, pathLength+1 ); filePattern = split_path_and_file( basePath ); if( !filePattern ) { /* With no filePattern, no file can be found... * so return an empty directory to the caller. */ *directory = dir; TA_Free( basePath ); return TA_SUCCESS; } /* Look for last separetor. */ if( !open_dir(&dirHandle, basePath ) ) { /* Errors, or no files or no directory... but * still have to pass the result to the caller. */ TA_Free( basePath ); *directory = dir; return TA_SUCCESS; } entryName = dirHandle.file_name; entryIsDirectory = dirHandle.file_attrs & ATTR_SUBDIR; #endif do { #if defined( USE_OSLAYER ) if( file_matches( entryName, filePattern ) ) { #endif if( entryIsDirectory ) { if( entryName[0] != '.' ) { string = TA_StringAlloc( global->dirnameCache, entryName ); if( string == NULL ) { #if defined( USE_OSLAYER ) close_dir(&dirHandle); TA_Free( basePath ); #endif TA_DirectoryFree( dir ); return TA_ALLOC_ERR; } retCode = TA_ListAddTail( dir->listOfDirectory, (void *)string ); if( retCode != TA_SUCCESS ) { #if defined( USE_OSLAYER ) close_dir(&dirHandle); TA_Free( basePath ); #endif TA_DirectoryFree( dir ); return retCode; } dir->nbDirectory++; } } else { string = TA_StringAlloc( global->filenameCache, entryName ); if( string == NULL ) { #if defined( USE_OSLAYER ) close_dir(&dirHandle); TA_Free( basePath ); #endif TA_DirectoryFree( dir ); return TA_ALLOC_ERR; } retCode = TA_ListAddTail( dir->listOfFile, (void *)string ); if( retCode != TA_SUCCESS ) { #if defined( USE_OSLAYER ) close_dir(&dirHandle); TA_Free( basePath ); #endif TA_DirectoryFree( dir ); return retCode; } dir->nbFile++; } #if defined( USE_OSLAYER ) } #endif #if defined( USE_WIN32_API ) findNextRetCode = FindNextFile( handle, &data ); entryName = data.cFileName; entryIsDirectory = data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; #endif #if defined( USE_OSLAYER ) findNextRetCode = read_dir( &dirHandle ); entryName = dirHandle.file_name; entryIsDirectory = dirHandle.file_attrs & ATTR_SUBDIR; #endif } while( findNextRetCode == TRUE ); #if defined( USE_OSLAYER ) TA_Free( basePath ); if( !close_dir(&dirHandle) ) { TA_DirectoryFree( dir ); return TA_INTERNAL_ERROR(11); } #endif #if defined( USE_WIN32_API ) if( FindClose( handle ) != TRUE ) { global->lastError = GetLastError(); TA_DirectoryFree( dir ); return TA_INTERNAL_ERROR(12); } #endif /* Pass the result to the caller. */ *directory = dir; return TA_SUCCESS; }
void dir_cnt_clean(dir_cnt *d) { dir_cnt_free_cnt(d); close_dir(d); }
FBCALL FBSTRING *fb_Dir( FBSTRING *filespec, int attrib, int *out_attrib ) { FB_DIRCTX *ctx; FBSTRING *res; ssize_t len; int tmp_attrib; char *name, *p; struct stat info; if( out_attrib == NULL ) out_attrib = &tmp_attrib; len = FB_STRSIZE( filespec ); name = NULL; ctx = FB_TLSGETCTX( DIR ); if( len > 0 ) { /* findfirst */ if( ctx->in_use ) close_dir( ); if( strchr( filespec->data, '*' ) || strchr( filespec->data, '?' ) ) { /* we have a pattern */ p = strrchr( filespec->data, '/' ); if( p ) { strncpy( ctx->filespec, p + 1, MAX_PATH ); ctx->filespec[MAX_PATH-1] = '\0'; len = (p - filespec->data) + 1; if( len > MAX_PATH - 1 ) len = MAX_PATH - 1; memcpy( ctx->dirname, filespec->data, len ); ctx->dirname[len] = '\0'; } else { strncpy( ctx->filespec, filespec->data, MAX_PATH ); ctx->filespec[MAX_PATH-1] = '\0'; strcpy( ctx->dirname, "./"); } /* Make sure these patterns work just like on Win32/DOS */ if( (!strcmp( ctx->filespec, "*.*" )) || (!strcmp( ctx->filespec, "*." )) ) strcpy( ctx->filespec, "*" ); if( (attrib & 0x10) == 0 ) attrib |= 0x20; ctx->attrib = attrib; ctx->dir = opendir( ctx->dirname ); if( ctx->dir ) { name = find_next( out_attrib ); if( name ) ctx->in_use = TRUE; } } else { /* no pattern, use stat on single file */ if( !stat( filespec->data, &info ) ) { tmp_attrib = get_attrib( filespec->data, &info ); if( (tmp_attrib & ~attrib ) == 0 ) { name = strrchr( filespec->data, '/' ); if( !name ) name = filespec->data; else name++; *out_attrib = tmp_attrib; } } } } else { /* findnext */ if( ctx->in_use ) name = find_next( out_attrib ); } FB_STRLOCK(); /* store filename if found */ if( name ) { len = strlen( name ); res = fb_hStrAllocTemp_NoLock( NULL, len ); if( res ) fb_hStrCopy( res->data, name, len ); else res = &__fb_ctx.null_desc; } else { res = &__fb_ctx.null_desc; *out_attrib = 0; } fb_hStrDelTemp_NoLock( filespec ); FB_STRUNLOCK(); return res; }
/*创建新的目录项*/ int make_file(int inode, char* name, int type) { char original_name_path[30]; int original_inode = inode_num;//记录当前的inode strcpy(original_name_path, name); if (eat_path(name) == -1) { if (type == File) printf("touch: cannot touch‘%s’: No such file or directory\n", original_name_path); if (type == Directory) printf("mkdir: cannot create directory ‘%s’: No such file or directory\n", original_name_path); return -1; } int new_node; int blk_need = 1;//本目录需要增加磁盘块则blk_need=2 int t; Inode temp; /*读取当前目录的Inode*/ fseek(Disk, InodeBeg + sizeof(Inode)*inode, SEEK_SET); fread(&temp, sizeof(Inode), 1, Disk); if (temp.access[1] == 0) { //当前目录不允许写 if (type == Directory) printf("mkdir: cannot create directory ‘%s’: Permission denied\n", original_name_path); if (type == File) printf("touch: cannot touch ‘%s’: Permission denied\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (dir_num>MaxDirNum) {//超过了目录文件能包含的最大目录项 if (type == Directory) printf("mkdir: cannot create directory '%s' : Directory full\n", original_name_path); if (type == File) printf("touch: cannot create file '%s' : Directory full\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (check_name(inode, name) != -1) {//防止重命名 if (type == Directory) printf("mkdir: cannnot create directory '%s' : Directory exist\n", original_name_path); if (type == File) printf("touch: cannot create file '%s' : File exist\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (dir_num / DirPerBlk != (dir_num + 1) / DirPerBlk) {//本目录也要增加磁盘块 blk_need = 2; } // printf("blk_used:%d\n",super_blk.blk_used); if (super_blk.blk_used + blk_need>BlkNum) { if (type == Directory) printf("mkdir: cannot create directory '%s' :Block used up\n", original_name_path); if (type == File) printf("touch: cannot create file '%s' : Block used up\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (blk_need == 2) {//本目录需要增加磁盘块 t = curr_inode.blk_num++; curr_inode.blk_identifier[t] = get_blk(); } /*申请inode*/ new_node = apply_inode(); if (new_node == -1) { if (type == Directory) printf("mkdir: cannot create directory '%s' :Inode used up\n", original_name_path); if (type == File) printf("touch: cannot create file '%s' : Inode used up\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (type == Directory) { /*初始化新建目录的inode*/ init_dir_inode(new_node, inode); } else if (type == File) { /*初始化新建文件的inode*/ init_file_inode(new_node); } strcpy(dir_table[dir_num].name, name); dir_table[dir_num++].inode_num = new_node; close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return 0; }
int remove_file(int inode, char* name, int deepth, int type) { char original_name_path[30]; int original_inode = inode_num;//记录当前的inode strcpy(original_name_path, name); if (eat_path(name) == -1) { if (type == Directory) printf("rmdir: failed to remove‘%s’: No such file or directory\n", original_name_path); if (type == File) printf("rm: cannot remove‘%s’: No such file or directory\n", original_name_path); return -1; } int check_type_result = type_check(name); if (check_type_result == -1) {//要删除的文件不存在 if (type == Directory) printf("rmdir: failed to remove '%s': No such file or directory\n", original_name_path); if (type == File) printf("rm: cannot remove '%s': No such file or directory\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } Inode father_inode; /*读取要删除的目录或文件的父目录的Inode*/ fseek(Disk, InodeBeg + sizeof(Inode)*inode_num, SEEK_SET); fread(&father_inode, sizeof(father_inode), 1, Disk); if (father_inode.access[1] == 0) { //要删除的目录或文件的父目录不允许写 if (type == Directory) printf("rmdir: failed to remove ‘%s’: Permission denied\n", original_name_path); if (type == File) printf("rm: cannot remove‘%s’: Permission denied\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (check_type_result == Directory && type == File) {//删除的文件类型与对应指令不符,如:尝试用rm删除目录 printf("rm: cannot remove '%s': Not a file\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (check_type_result == File && type == Directory) {//删除的文件类型与对应指令不符,如:用rmdir删除文件 printf("rmdir: failed to remove '%s': Not a directory\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } del_file(inode_num, name, 0); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return 0; }
int open_path(int drive, const char *path) // Opening the directory(s)... { int i, j, curdir_handle, new_handle; char name_comp[13], conv_name[11]; // Open the directory component i =0; if (path[0] == '/' || path[0] == '\\') // Absolute path { curdir_handle = root_handle(drive); i = 1; } else curdir_handle = get_curdir_handle(drive); // Increment the reference count increment_ref_count(curdir_handle); while (path[i] != 0) { j = 0; while (j < 12 && path[i] != 0 && path[i] != '/' && path[i] != '\\') { name_comp[j] = path[i]; j++; i++; } if (path[i] != 0) i++; name_comp[j] = 0; if (strcmp(name_comp,".") == 0) continue; else if (strcmp(name_comp,"..") == 0) { new_handle = get_parent_handle(curdir_handle); if (new_handle >= 0) increment_ref_count(new_handle); } else { // Open that directory component if (convert_name(name_comp, conv_name) < 0) { close_dir(curdir_handle); return EINVALIDNAME; // Error } new_handle = open_dir(curdir_handle, conv_name); } if (new_handle < 0) { close_dir(curdir_handle); return new_handle; // Error } close_dir(curdir_handle); // Effectively decrease the // reference count to 1 less. curdir_handle = new_handle; } // Successfully opened the path return curdir_handle; }
int syscall_rename(const char *a_oldpath, const char *a_newpath) { int ohandle, nhandle; int drive1, drive2; char dir_path1[512], dir_path2[512]; char name_comp1[13], name_comp2[13], conv_name1[11], conv_name2[11]; char oldpath[512], newpath[512]; struct dir_entry dent1, dent2; int exist1, exist2; struct DIR_FILE *dirf; int len1, len2; int i,t; len1 = strlen(a_oldpath); len2 = strlen(a_newpath); if (len1 > 512 || len2 > 512) return ELONGPATH; strcpy(oldpath,a_oldpath); strcpy(newpath,a_newpath); if (oldpath[len1-1] == '/' || oldpath[len1-1] == '\\') oldpath[len1-1] = '\0'; if (newpath[len2-1] == '/' || newpath[len2-1] == '\\') newpath[len2-1] = '\0'; parse_path(oldpath, &drive1, dir_path1, name_comp1); parse_path(newpath, &drive2, dir_path2, name_comp2); if (drive1 != drive2) return EDEVICE_DIFFERENT; nhandle = open_path(drive2, dir_path2); if (nhandle < 0) return nhandle; if (name_comp2[0] !='\0') { if (convert_name(name_comp2, conv_name2) < 0) { close_dir(nhandle); return EINVALIDNAME; // Error } exist2 = find_entry(nhandle, conv_name2, &dent2); } ohandle = open_path(drive1, dir_path1); if (ohandle < 0) { close_dir(nhandle); return ohandle; } if (name_comp1[0] != '\0') { if (convert_name(name_comp1, conv_name1) < 0) { close_dir(nhandle); close_dir(ohandle); return EINVALIDNAME; // Error } exist1 = find_entry(ohandle, conv_name1, &dent1); } // Check whether new path exists and is removable if ((exist2 == 1) && ((dent2.attrib & FTYPE_READONLY) || ((dent2.attrib & FTYPE_DIR) && (empty_dir(nhandle, &dent2) != 1)))) { close_dir(nhandle); close_dir(ohandle); return ETARGET_EXISTS; } // Check if source exists and is movable if (exist1 != 1) { close_dir(nhandle); close_dir(ohandle); return EPATH_NOT_EXISTS; } if ((dent1.attrib & FTYPE_READONLY) != 0) { close_dir(nhandle); close_dir(ohandle); return EREADONLY; } // Check whether oldpath is not a subpath of newpath if ((dent1.attrib & FTYPE_DIR) && (ohandle != nhandle)) { t = nhandle; dirf = &dir_file_list[t]; while (dirf->parent_index >= 0 && dirf->parent_index != ohandle) { t = dirf->parent_index; dirf = &dir_file_list[t]; } if (dirf->parent_index == ohandle) { close_dir(nhandle); close_dir(ohandle); return EOLDPATH_PARENT_OF_NEWPATH; } } // Check if newpath already exists whether it is compatible or not if ((exist2 == 1) && (((dent1.attrib & FTYPE_DIR) != 0 && (dent2.attrib & FTYPE_DIR) == 0) || ((dent1.attrib & FTYPE_DIR) == 0 && (dent2.attrib & FTYPE_DIR) != 0))) { close_dir(nhandle); close_dir(ohandle); return ESRC_DEST_NOT_SAME_TYPE; } // Remove destination entry if exists if (exist2 == 1) { if (dent2.attrib & FTYPE_DIR) syscall_rmdir(newpath); else syscall_unlink(newpath); } // Add the source dir entry after changing the name // to destination directory bcopy( (char *)&dent1, (char *)&dent2, sizeof(struct dir_entry)); for (i=0; i<11; i++) // Both name and extension dent2.name[i] = conv_name2[i]; t = add_dir_entry(nhandle, &dent2); if (t == 1) { delete_dir_entry(ohandle, dent1.name); } // Close the handles of parent directories close_dir(ohandle); close_dir(nhandle); if (t == 1) return 0; else return t; }
int file_edit(char* name) { char original_name_path[30]; int original_inode = inode_num;//记录当前的inode strcpy(original_name_path, name); if (eat_path(name) == -1) { printf("错误:文件路径出错!‘%s’\n", original_name_path); return -1; } if (type_check(name) == Directory) { printf("错误:'%s'文件不存在!\n", name); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (type_check(name) == -1) { printf("错误:'%s'文件不存在!\n", name); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (file_read(name) == -1) {//文件读取失败 printf("错误:无读写‘%s’文件权限!\n", name); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } BuffModifyTimeBeforeEdit = getBuffModifyTime();//文件信息载入到buff.txt后,获取buff.txt的修改时间,用来判断载入的内容在记事本中是否被修改 /* SYSTEMTIME *STime = new SYSTEMTIME; FileTimeToSystemTime(&BuffModifyTimeBeforeEdit, STime); printf("%d-%d-%d-%d-%d-%d\n", STime->wYear, STime->wMonth, STime->wDay, STime->wHour, STime->wMinute, STime->wSecond); */ STARTUPINFO si; PROCESS_INFORMATION pi; LPTSTR szCmdline = _tcsdup(TEXT("notepad.exe buff.txt")); ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); // 打开子进程 if (!CreateProcess(NULL, // No module name (use command line) szCmdline, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi) // Pointer to PROCESS_INFORMATION structure ) { printf("CreateProcess failed (%d).\n", GetLastError()); return 0; } // Wait until child process exits. WaitForSingleObject(pi.hProcess, INFINITE); // Close process and thread handles. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); Sleep(10); BuffModifyTimeAfterEdit = getBuffModifyTime();//buff.txt关闭后,获取buff.txt的修改时间,用来判断载入的内容在记事本中是否被修改 /* SYSTEMTIME *STime = new SYSTEMTIME; FileTimeToSystemTime(&BuffModifyTimeAfterEdit, STime); printf("%d-%d-%d-%d-%d-%d\n", STime->wYear, STime->wMonth, STime->wDay, STime->wHour, STime->wMinute, STime->wSecond); */ if (BuffModifyTimeBeforeEdit.dwLowDateTime == BuffModifyTimeAfterEdit.dwLowDateTime && BuffModifyTimeBeforeEdit.dwHighDateTime == BuffModifyTimeAfterEdit.dwHighDateTime) { //若buff.txt的修改时间没有发生变化。则载入记事本的内容没有发生改变,则不必重新写入 close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } if (file_write(name) == -1) { //将数据从BUFF写入文件 printf("错误:文件类型为只读,无法修改!\n"); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return 0; }
/*修改文件读写权限*/ int change_mode(char* parameter, char* name) { int inode; Inode temp; char original_name_path[30]; int original_inode = inode_num;//记录当前的inode strcpy(original_name_path, name); if (eat_path(name) == -1) { printf("chmod: cannot access‘%s’: No such file or directory\n", original_name_path); return -1; } inode = check_name(inode_num, name); if (inode == -1) { printf("stat: cannot stat '%s': No such file or directory\n", original_name_path); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return -1; } fseek(Disk, InodeBeg + sizeof(Inode)*inode, SEEK_SET); fread(&temp, sizeof(Inode), 1, Disk); if (strcmp(parameter, "+r") == 0) { temp.access[0][user_num] = 1; temp.i_ctime = time(NULL); } else if (strcmp(parameter, "-r") == 0) { temp.access[0][user_num] = 0; temp.i_ctime = time(NULL); } else if (strcmp(parameter, "+w") == 0) { temp.access[1][user_num] = 1; temp.i_ctime = time(NULL); } else if (strcmp(parameter, "-w") == 0) { temp.access[1][user_num] = 0; temp.i_ctime = time(NULL); } else if (strcmp(parameter, "+x") == 0) { temp.access[2][user_num] = 1; temp.i_ctime = time(NULL); } else if (strcmp(parameter, "-x") == 0) { temp.access[2][user_num] = 0; temp.i_ctime = time(NULL); } else { printf("chmod: invalid option -- '%s'\n", parameter); } /*将修改后的Inode写回*/ fseek(Disk, InodeBeg + sizeof(Inode)*inode, SEEK_SET); fwrite(&temp, sizeof(Inode), 1, Disk); close_dir(inode_num); inode_num = original_inode; open_dir(inode_num); return 0; }
int file_move(char* name, char* mvname) { int originalInode = inode_num;//记录当前的inode int source_inode_num, dest_inode_num; int inode;//原文件的inode char originalNamePath[30]; char originalMvNamePath[30]; strcpy(originalNamePath, name); strcpy(originalMvNamePath, mvname); if (eat_path(name) == -1) { printf("mv: cannot stat ‘%s’: No such file or directory\n", originalNamePath); return -1; } if (type_check(name) != File) { printf("mv: cannot move '%s': Not a file or no exist\n", originalNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } source_inode_num = inode_num;//记录原文件的父目录节点 close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 if (eat_path(mvname) == -1) { printf("mv: cannot stat ‘%s’: No such file or directory\n", originalMvNamePath); return -1; } dest_inode_num = inode_num;//记录目标目录的父目录节点 if (source_inode_num == dest_inode_num && strcmp(name, mvname) == 0) { printf("mv: '%s' and '%s' are the same file\n", originalNamePath, originalMvNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } Inode temp; /*读取原文件目录的inode节点,判断该目录是否可写*/ fseek(Disk, InodeBeg + sizeof(Inode)*source_inode_num, SEEK_SET); fread(&temp, sizeof(Inode), 1, Disk); if (temp.access[1][user_num] == 0) { //原文件目录不可写 if (type_check(mvname) == Directory) printf("mv: cannot move ‘%s’ to ‘%s/%s’: Permission denied\n", originalNamePath, originalMvNamePath, name); else printf("mv: cannot move ‘%s’ to ‘%s’: Permission denied\n", originalNamePath, originalMvNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } /*形如:mv a.txt b.txt*/ if (type_check(mvname) == -1) { //如果b.txt不存在 /*读取b.txt的父目录的inode,判断是否可写*/ fseek(Disk, InodeBeg + sizeof(Inode)*dest_inode_num, SEEK_SET); fread(&temp, sizeof(temp), 1, Disk); if (temp.access[1][user_num] == 0) { //b.txt的父目录不可写 printf("mv: cannot move ‘%s’ to ‘%s’: Permission denied\n", originalNamePath, originalMvNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } if (source_inode_num == dest_inode_num) {//如果b.txt不存在,且a.txt 与 b.txt 在同一父目录下,则相当于a.txt重命名 for (int pos = 0; pos < dir_num; ++pos) { if (strcmp(dir_table[pos].name, name) == 0) strcpy(dir_table[pos].name, mvname); } } else {//如果b.txt不存在,且a.txt 与 b.txt 不在同一父目录下,则将a.txt的目录项删除,创建b.txt的目录项指向a.txt的inode inode_num = source_inode_num; open_dir(source_inode_num);//返回a.txt父目录位置 inode = check_name(inode_num, name); adjust_dir(name);//删除a.txt的目录项 close_dir(inode_num); inode_num = dest_inode_num; open_dir(dest_inode_num);//返回b.txt父目录位置 strcpy(dir_table[dir_num].name, mvname); dir_table[dir_num++].inode_num = inode; } } else if (type_check(mvname) == File) {//如果b.txt为文件 if (source_inode_num == dest_inode_num) {//若b.txt 与 a.txt在同一父目录下,则删除b.txt的目录项,同时将a.txt的目录项重命名 adjust_dir(mvname); //b.txt 与 a.txt在同一父目录下,则b.txt的父目录权限在前面已经判断,是可写的 for (int pos = 0; pos < dir_num; ++pos) { if (strcmp(dir_table[pos].name, name) == 0) strcpy(dir_table[pos].name, mvname); } } else {//b.txt 已经存在 且 a.txt与b.txt不在同一目录,则将a.txt的目录项删除,修改b.txt的目录项指向a.txt的inode inode_num = source_inode_num; open_dir(source_inode_num);//返回a.txt父目录位置 inode = check_name(inode_num, name); adjust_dir(name);//删除a.txt的目录项 close_dir(inode_num); inode_num = dest_inode_num; open_dir(dest_inode_num);//返回b.txt父目录位置 for (int pos = 0; pos < dir_num; ++pos) { if (strcmp(dir_table[pos].name, mvname) == 0) dir_table[pos].inode_num = inode; } } } else { //如果b.txt为目录 if (source_inode_num == dest_inode_num && strcmp(mvname, ".") == 0) { printf("mv: '%s' and '%s/%s' are the same file\n", originalNamePath, originalMvNamePath, name); return -1;//移动到本目录下,即不需要移动 } //如果b.txt是目录项,进入b.txt目录中,创建一个目录项指向a.txt, 再将a.txt的目录项删除, inode_num = source_inode_num; open_dir(source_inode_num);//返回a.txt父目录位置 inode = check_name(inode_num, name);//记录a.txt的inode close_dir(inode_num); inode_num = dest_inode_num; open_dir(dest_inode_num);//返回b.txt父目录位置 enter_child_dir(inode_num, mvname); if (check_name(inode_num, mvname) == -1) { //b.txt目录下不存在与a.txt重名的项目,则创建一个目录项指向a.txt /*读取b.txt的inode,判断是否可写*/ fseek(Disk, InodeBeg + sizeof(Inode)*inode_num, SEEK_SET); fread(&temp, sizeof(Inode), 1, Disk); if (temp.access[1][user_num] == 0) { //b.txt不可写 printf("mv: cannot move ‘%s’ to ‘%s/%s’: Permission denied\n", originalNamePath, originalMvNamePath, name); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } strcpy(dir_table[dir_num].name, name); dir_table[dir_num++].inode_num = inode; } else { //b.txt目录下存在与a.txt重名的项目,修改该项目的目录项指向a.txt的inode dir_table[check_name(inode, mvname)].inode_num = inode; } close_dir(inode_num); inode_num = source_inode_num; open_dir(source_inode_num);//返回a.txt父目录位置 inode = check_name(inode_num, name); adjust_dir(name);//删除a.txt的目录项 } close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return 0; }
int file_copy(char* name, char* cpname) { int originalInode = inode_num;//记录当前的inode int source_inode_num, dest_inode_num; char originalNamePath[30]; char originalCpNamePath[30]; strcpy(originalNamePath, name); strcpy(originalCpNamePath, cpname); if (eat_path(name) == -1) { printf("cp: cannot stat ‘%s’: No such file or directory\n", originalNamePath); return -1; } if (type_check(name) != File) { printf("cp: cannot copy '%s': Not a file\n", originalNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } if (file_read(name) == -1) {//若原文件不可读 printf("cp: cannot open '%s' for reading: Permission denied\n", originalNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } source_inode_num = inode_num;//记录原文件的父目录节点 close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 if (eat_path(cpname) == -1) { printf("cp: cannot stat ‘%s’: No such file or directory\n", originalCpNamePath); return -1; } dest_inode_num = inode_num;//记录目标目录的父目录节点 if (source_inode_num == dest_inode_num && strcmp(name, cpname) == 0) { printf("cp: '%s' and '%s' are the same file\n", originalNamePath, originalCpNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } Inode temp; /*将原文件拷贝到另一文件中*/ if (type_check(cpname) != Directory) { if (check_name(inode_num, cpname) != -1) {//目标文件已经存在,则将数据拷贝进去 if (file_write(cpname) == -1) { //若目标文件不可写 printf("cp: cannot create regular file‘%s’: Permission denied\n", originalCpNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } } else { //目标文件不存在,创建文件,并将数据拷贝进去 /*读取目标目录的inode节点,判断该目录是否可写*/ fseek(Disk, InodeBeg + sizeof(Inode)*dest_inode_num, SEEK_SET); fread(&temp, sizeof(Inode), 1, Disk); if (temp.access[1][user_num] == 0) { //目标目录不可写 printf("cp: cannot create regular file ‘%s’: Permission denied\n", originalCpNamePath); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } make_file(inode_num, cpname, File); file_write(cpname);//将数据从BUFF写入文件 } } /*将原文件拷贝到某个子目录下,包括当前目录(.), 原目录的父目录(..)*/ else { if (source_inode_num == dest_inode_num && strcmp(cpname, ".") == 0) {//将当前目录下的某个文件复制到当前目录,提示已经存在 printf("cp: '%s' and '%s/%s' are the same file\n", originalNamePath, originalCpNamePath, name); return -1; } enter_child_dir(inode_num, cpname);//进入子目录或父目录 /*记录原目录名*/ int pos = strlen(path) - 1; for (; pos >= 0; --pos) { if (path[pos] == '/') { break; } } char curDirName[30]; int i = 0; for (pos = pos + 1; pos <= strlen(path); ++pos) curDirName[i++] = path[pos]; //printf("%s\n", curDirName); if (check_name(inode_num, name) != -1) {//是否已有同名文件 file_write(name);//将数据从BUFF写入文件 } else { /*读取目标目录的inode节点,判断该目录是否可写*/ fseek(Disk, InodeBeg + sizeof(Inode)*inode_num, SEEK_SET); fread(&temp, sizeof(Inode), 1, Disk); if (temp.access[1][user_num] == 0) { //目标目录不可写 printf("cp: cannot create regular file ‘./%s’: Permission denied\n", name); close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return -1; } make_file(inode_num, name, File); file_write(name);//将数据从BUFF写入文件 } /*返回原目录*/ if (strcmp(cpname, "..") == 0) enter_child_dir(inode_num, curDirName);//如果复制到父目录,则复制完返回原目录 else enter_child_dir(inode_num, "..");//如果复制到子目录,则复制完返回到子目录的".."目录,即原目录 } close_dir(inode_num); inode_num = originalInode; open_dir(inode_num);//返回操作前的目录位置 return 0; }