Added VFS_ReadDir and SYS_READDIR, Implemented basic directory listing in CLIShell
authorJohn Hodge <[email protected]>
Fri, 25 Sep 2009 05:31:41 +0000 (13:31 +0800)
committerJohn Hodge <[email protected]>
Fri, 25 Sep 2009 05:31:41 +0000 (13:31 +0800)
Kernel/include/vfs_ext.h
Kernel/syscalls.c
Kernel/vfs/dir.c
Usermode/Applications/CLIShell_src/main.c
Usermode/Libraries/libacess.so_src/vfs.asm

index 53bcc05..f5219c0 100644 (file)
@@ -42,5 +42,6 @@ extern char   *VFS_GetTruePath(char *Path);
 extern int     VFS_Mount(char *Device, char *MountPoint, char *Filesystem, char *Options);
 extern int     VFS_MkDir(char *Path);
 extern int     VFS_Symlink(char *Link, char *Dest);
+extern int     VFS_ReadDir(int FD, char *Dest);
 
 #endif
index ebfdbed..8790be7 100644 (file)
@@ -143,6 +143,10 @@ void SyscallHandler(tSyscallRegs *Regs)
        case SYS_FINFO:
                ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
                break;
+               
+       case SYS_READDIR:
+               ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
+               break;
        
        // -- Debug
        case SYS_DEBUG:
index aeda00e..b4001dd 100644 (file)
@@ -131,3 +131,29 @@ int VFS_Symlink(char *Name, char *Link)
        
        return 1;
 }
+
+/**
+ * \fn int VFS_ReadDir(int FD, char *Dest)
+ * \brief Read from a directory
+ */
+int VFS_ReadDir(int FD, char *Dest)
+{
+       tVFS_Handle     *h = VFS_GetHandle(FD);
+       char    *tmp;
+       
+       if(h)   return -1;
+       
+       if(h->Node->ReadDir == NULL)    return 0;
+       
+       tmp = h->Node->ReadDir(h->Node, h->Position);
+       
+       if(!tmp)        return 0;
+       
+       h->Position ++;
+       
+       strcpy(Dest, tmp);
+       
+       if(IsHeap(tmp)) free(tmp);
+       
+       return 1;
+}
index 2d05bdc..cb76be1 100644 (file)
@@ -15,9 +15,8 @@ char  *ReadCommandLine(int *Length);
 void   Parse_Args(char *str, char **dest);\r
 void   Command_Colour(int argc, char **argv);\r
 void   Command_Clear(int argc, char **argv);\r
-//void Command_Ls(int argc, char **argv);\r
 void   Command_Cd(int argc, char **argv);\r
-//void Command_Cat(int argc, char **argv);\r
+void   Command_Dir(int argc, char **argv);\r
 \r
 // ==== CONSTANT GLOBALS ====\r
 char   *cCOLOUR_NAMES[8] = {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"};\r
@@ -25,7 +24,8 @@ struct        {
        char    *name;\r
        void    (*fcn)(int argc, char **argv);\r
 }      cBUILTINS[] = {\r
-       {"colour", Command_Colour}, {"clear", Command_Clear}, {"cd", Command_Cd}\r
+       {"colour", Command_Colour}, {"clear", Command_Clear},\r
+       {"cd", Command_Cd}, {"dir", Command_Dir}\r
 };\r
 #define        BUILTIN_COUNT   (sizeof(cBUILTINS)/sizeof(cBUILTINS[0]))\r
 \r
@@ -111,22 +111,23 @@ int main(int argc, char *argv[], char *envp[])
                        continue;\r
                }\r
                // Check if the file is a directory\r
-               finfo( length, &info );
+               finfo( length, &info, 0 );
                close( length );\r
                if(info.flags & FILEFLAG_DIRECTORY) {\r
                        Print("`");Print(saArgs[1]);    // Error Message\r
                        Print("' is a directory.\n");\r
                        continue;\r
                }\r
+               // Load new executable\r
                pid = clone(CLONE_VM, 0);\r
-               if(pid == 0)    execve(gsTmpBuffer, &saArgs[1], NULL);\r
+               if(pid == 0)    execve(gsTmpBuffer, &saArgs[1], envp);\r
                if(pid <= 0) {\r
                        Print("Unablt to create process: `");Print(gsTmpBuffer);Print("'\n");   // Error Message
                        //SysDebug("pid = %i\n", pid);\r
                }\r
                else {\r
-                        int    status\r
-                       waitpid(pid, &status);\r
+                        int    status;\r
+                       waittid(pid, &status);\r
                }\r
        }\r
 }\r
@@ -316,7 +317,7 @@ void Command_Cd(int argc, char **argv)
                write(_stdout, 26, "Directory does not exist\n");\r
                return;\r
        }\r
-       finfo(fp, &stats);\r
+       finfo(fp, &stats, 0);\r
        close(fp);\r
        \r
        if( !(stats.flags & FILEFLAG_DIRECTORY) ) {\r
@@ -327,3 +328,106 @@ void Command_Cd(int argc, char **argv)
        strcpy(gsCurrentDirectory, tmpPath);\r
 }\r
 \r
+/**\r
+ */\r
+void Command_Dir(int argc, char **argv)\r
+{\r
+        int    dp, fp, dirLen;\r
+       //char  modeStr[11] = "RWXrwxRWX ";\r
+       char    tmpPath[1024];\r
+       char    fileName[256];\r
+       t_sysFInfo      info;\r
+       \r
+       // Generate Directory Path\r
+       if(argc > 1)\r
+               dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath);\r
+       else\r
+       {\r
+               strcpy(tmpPath, gsCurrentDirectory);\r
+       }\r
+       dirLen = strlen(tmpPath);\r
+       \r
+       // Open Directory\r
+       dp = open(tmpPath, OPENFLAG_READ);\r
+       // Check if file opened\r
+       if(dp == -1)\r
+       {\r
+               //printf("Unable to open directory `%s', File cannot be found\n", tmpPath);\r
+               //write(_stdout, 27, "Unable to open directory `");\r
+               //write(_stdout, strlen(tmpPath)+1, tmpPath);\r
+               //write(_stdout, 25, "', File cannot be found\n");\r
+               return;\r
+       }\r
+       // Get File Stats\r
+       if( finfo(dp, &info, 0) == -1 )\r
+       {\r
+               close(dp);\r
+               write(_stdout, 34, "stat Failed, Bad File Descriptor\n");\r
+               return;\r
+       }\r
+       // Check if it's a directory\r
+       if(!(info.flags & FILEFLAG_DIRECTORY))\r
+       {\r
+               close(dp);\r
+               write(_stdout, 27, "Unable to open directory `");\r
+               write(_stdout, strlen(tmpPath)+1, tmpPath);\r
+               write(_stdout, 20, "', Not a directory\n");\r
+               return;\r
+       }\r
+       \r
+       // Append Shash for file paths\r
+       if(tmpPath[dirLen-1] != '/')\r
+       {\r
+               tmpPath[dirLen++] = '/';\r
+               tmpPath[dirLen] = '\0';\r
+       }\r
+       // Read Directory Content\r
+       while( (fp = readdir(dp, fileName)) )\r
+       {\r
+               if(fp < 0)\r
+               {\r
+                       if(fp == -3)\r
+                               write(_stdout, 42, "Invalid Permissions to traverse directory\n");\r
+                       break;\r
+               }\r
+               // Create File Path\r
+               strcpy((char*)(tmpPath+dirLen), fileName);\r
+               // Open File\r
+               fp = open(tmpPath, 0);\r
+               if(fp == -1)    continue;\r
+               // Get File Stats\r
+               finfo(fp, &info, 0);\r
+               close(fp);\r
+               \r
+               // Colour Code\r
+               write(_stdout, 6, "\x1B[37m");  //White\r
+               if(info.flags & FILEFLAG_DIRECTORY)     //Directory Green\r
+                       write(_stdout, 6, "\x1B[32m");\r
+               \r
+               //Print Mode\r
+               #if 0\r
+               if(stats.st_mode & 0400)        modeStr[0] = 'R';       else    modeStr[0] = '-';\r
+               if(stats.st_mode & 0200)        modeStr[1] = 'W';       else    modeStr[1] = '-';\r
+               if(stats.st_mode & 0100)        modeStr[2] = 'X';       else    modeStr[2] = '-';\r
+               if(stats.st_mode & 0040)        modeStr[3] = 'R';       else    modeStr[3] = '-';\r
+               if(stats.st_mode & 0020)        modeStr[4] = 'W';       else    modeStr[4] = '-';\r
+               if(stats.st_mode & 0010)        modeStr[5] = 'X';       else    modeStr[5] = '-';\r
+               if(stats.st_mode & 0004)        modeStr[6] = 'R';       else    modeStr[6] = '-';\r
+               if(stats.st_mode & 0002)        modeStr[7] = 'W';       else    modeStr[7] = '-';\r
+               if(stats.st_mode & 0001)        modeStr[8] = 'X';       else    modeStr[8] = '-';\r
+               write(_stdout, 10, modeStr);\r
+               #endif\r
+               \r
+               //Print Name\r
+               write(_stdout, strlen(fileName), fileName);\r
+               //Print slash if applicable\r
+               if(info.flags & FILEFLAG_DIRECTORY)\r
+                       write(_stdout, 1, "/");\r
+               \r
+               // Revert Colout and end line\r
+               write(_stdout, 6, "\x1B[37m");\r
+               write(_stdout, 2, "\n");\r
+       }\r
+       // Close Directory\r
+       close(dp);\r
+}\r
index 430a2f5..53c66c9 100644 (file)
@@ -14,3 +14,4 @@ SYSCALL4      read, SYS_READ  ; int, int64_t, void*
 SYSCALL4       write, SYS_WRITE        ; int, int64_t, void*
 SYSCALL4       seek, SYS_SEEK          ; int, int64_t, int
 SYSCALL3       finfo, SYS_FINFO        ; int, void*, int
+SYSCALL2       readdir, SYS_READDIR

UCC git Repository :: git.ucc.asn.au