From: John Hodge Date: Wed, 11 Jul 2012 15:41:00 +0000 (+0800) Subject: DiskTool - Cleanup, copy command X-Git-Tag: rel0.15~611^2~41^2~17 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=9d050efae63542b0d69377eec02367e178b88731;p=tpg%2Facess2.git DiskTool - Cleanup, copy command --- diff --git a/.gitignore b/.gitignore index c2b64113..fe240ee3 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,7 @@ obj-*/ Makefile.user.cfg QemuLog.txt Screenshots/ + +Tools/*/src/obj/ +Tools/*/src/Makefile.BuildNum +Tools/DiskTool/DiskTool diff --git a/Tools/DiskTool/src/Makefile b/Tools/DiskTool/src/Makefile index a85575c8..def66213 100644 --- a/Tools/DiskTool/src/Makefile +++ b/Tools/DiskTool/src/Makefile @@ -13,7 +13,8 @@ MODULE_SRC = ../../../KernelLand/Modules/ BIN = ../DiskTool # Kernel Sources (compiled with -ffreestanding) -K_OBJ = vfs/main.o vfs/open.o vfs/acls.o vfs/io.o vfs/dir.o +K_OBJ := lib.o +K_OBJ += vfs/main.o vfs/open.o vfs/acls.o vfs/io.o vfs/dir.o K_OBJ += vfs/nodecache.o vfs/mount.o vfs/memfile.o # vfs/select.o K_OBJ += vfs/fs/root.o vfs/fs/devfs.o K_OBJ += drv/proc.o @@ -22,10 +23,10 @@ MODULES := Filesystems/FAT Filesystems/Ext2 # Local kernel soruces (same as above, but located in same directory as Makefile) L_OBJ = vfs_handles.o threads.o nativefs.o time.o actions.o # Native Sources (compiled as usual) -N_OBJ = main.o script.o logging.o +N_OBJ = main.o script.o logging.o helpers.o # Compilation Options -CFLAGS := -Wall -std=gnu99 -g +CFLAGS := -Wall -std=gnu99 -g -Werror CPPFLAGS := -I include/ K_CPPFLAGS := -I $(KERNEL_SRC)include LDFLAGS += -Wl,--defsym,__buildnum=$(BUILD_NUM) -g diff --git a/Tools/DiskTool/src/actions.c b/Tools/DiskTool/src/actions.c index 417a86b9..51f5cd3f 100644 --- a/Tools/DiskTool/src/actions.c +++ b/Tools/DiskTool/src/actions.c @@ -8,14 +8,12 @@ */ #include #include -#include // === IMPORTS === extern int NativeFS_Install(char **Arguments); // === PROTOTYPES === void DiskTool_Initialise(void) __attribute__((constructor(101))); -size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path); int DiskTool_int_TranslateOpen(const char *File, int Mode); // === CODE === @@ -48,23 +46,46 @@ int DiskTool_MountImage(const char *Identifier, const char *Path) int DiskTool_Copy(const char *Source, const char *Destination) { - return -1; + int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ); + if( src == -1 ) { + Log_Error("DiskTool", "Unable to open %s for reading", Source); + return -1; + } + int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE); + if( dst == -1 ) { + Log_Error("DiskTool", "Unable to open %s for writing", Destination); + VFS_Close(src); + return -1; + } + + char buf[1024]; + size_t len, total = 0; + while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) + VFS_Write(dst, len, buf), total += len; + VFS_Write(dst, len, buf), total += len; + + Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination); + + VFS_Close(dst); + VFS_Close(src); + + return 0; } int DiskTool_ListDirectory(const char *Directory) { - int fd = DiskTool_int_TranslateOpen(Directory, 2); + int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY); if(fd == -1) { // fprintf(stderr, "Can't open '%s'\n", Directory); return -1; } - printf("Directory listing of '%s'\n", Directory); + Log("Directory listing of '%s'", Directory); char name[256]; while( VFS_ReadDir(fd, name) ) { - printf("%s\n", name); + Log("- %s", name); } VFS_Close(fd); @@ -73,43 +94,7 @@ int DiskTool_ListDirectory(const char *Directory) } // --- Internal helpers --- -size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path) -{ - int len; - const char *colon = strchr(Path, ':'); - if( colon ) - { - const char *pos; - for(pos = Path; pos < colon; pos ++) - { - if( !isalpha(*pos) ) - goto native_path; - } - - len = strlen("/Mount/"); - len += strlen(Path); - if( Buffer ) { - strcpy(Buffer, "/Mount/"); - strncat(Buffer+strlen("/Mount/"), Path, colon - Path); - strcat(Buffer, colon + 1); - } - return len; - } - -native_path: - len = strlen("/Native"); - len += strlen( getenv("PWD") ) + 1; - len += strlen(Path); - if( Buffer ) { - strcpy(Buffer, "/Native"); - strcat(Buffer, getenv("PWD")); - strcat(Buffer, "/"); - strcat(Buffer, Path); - } - return len; -} - -int DiskTool_int_TranslateOpen(const char *File, int Mode) +int DiskTool_int_TranslateOpen(const char *File, int Flags) { size_t tpath_len = DiskTool_int_TranslatePath(NULL, File); if(tpath_len == -1) @@ -117,18 +102,6 @@ int DiskTool_int_TranslateOpen(const char *File, int Mode) char tpath[tpath_len-1]; DiskTool_int_TranslatePath(tpath, File); -// printf("Opening '%s'\n", tpath); - - switch(Mode) - { - case 0: // Read - return VFS_Open(tpath, VFS_OPENFLAG_READ); - case 1: // Write - return VFS_Open(tpath, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE); - case 2: // Directory - return VFS_Open(tpath, VFS_OPENFLAG_READ|VFS_OPENFLAG_EXEC); - default: - return -1; - } + return VFS_Open(tpath, Flags); } diff --git a/Tools/DiskTool/src/helpers.c b/Tools/DiskTool/src/helpers.c new file mode 100644 index 00000000..ddfe0f06 --- /dev/null +++ b/Tools/DiskTool/src/helpers.c @@ -0,0 +1,58 @@ +/* + * Acess2 DiskTool + * - By John Hodge (thePowersGang) + * + * helpers.c + */ +#include +#include +#include +#include +#include + +// === GLOBALS === +char gsWorkingDirectory[1024]; + + +// === CODE === +size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path) +{ + int len; + const char *colon = strchr(Path, ':'); + + if( colon ) + { + const char *pos; + for(pos = Path; pos < colon; pos ++) + { + if( !isalpha(*pos) ) + goto native_path; + } + + len = strlen("/Mount/"); + len += strlen(Path); + if( Buffer ) { + strcpy(Buffer, "/Mount/"); + strncat(Buffer+strlen("/Mount/"), Path, colon - Path); + strcat(Buffer, colon + 1); + } + return len; + } + +native_path: + + if( !gsWorkingDirectory[0] ) { + getcwd(gsWorkingDirectory, 1024); + } + + len = strlen("/Native"); + len += strlen( gsWorkingDirectory ) + 1; + len += strlen(Path); + if( Buffer ) { + strcpy(Buffer, "/Native"); + strcat(Buffer, gsWorkingDirectory); + strcat(Buffer, "/"); + strcat(Buffer, Path); + } + return len; +} diff --git a/Tools/DiskTool/src/include/acess.h b/Tools/DiskTool/src/include/acess.h index deb18346..8493078c 100644 --- a/Tools/DiskTool/src/include/acess.h +++ b/Tools/DiskTool/src/include/acess.h @@ -13,6 +13,8 @@ #define STR(x) #x #define EXPAND_STR(x) STR(x) +#define ASSERT(x) do{}while(0) + extern char __buildnum[]; #define BUILD_NUM ((int)(Uint)&__buildnum) extern const char gsGitHash[]; @@ -22,7 +24,8 @@ extern const char gsGitHash[]; #include typedef uintptr_t Uint; -typedef unsigned int size_t; +//typedef unsigned int size_t; +#include typedef uint64_t off_t; typedef char BOOL; @@ -49,6 +52,8 @@ typedef char tMutex; typedef char tShortSpinlock; typedef int64_t tTime; +extern tTime now(void); +extern int64_t timestamp(int sec, int min, int hr, int day, int month, int year); #define PACKED __attribute__((packed)) #define DEPRECATED @@ -64,7 +69,6 @@ extern void *realloc(void *oldptr, size_t bytes); extern void free(void *buffer); #include - #include // Threads @@ -79,11 +83,19 @@ extern tGID Threads_GetGID(void); #define errno (*(Threads_GetErrno())) #include +extern int strucmp(const char *s1, const char *s2); extern int strpos(const char *Str, char Ch); extern void itoa(char *buf, uint64_t num, int base, int minLength, char pad); +extern int snprintf(char *buf, size_t len, const char *fmt, ...); +extern int sprintf(char *buf, const char *fmt, ...); +extern int ReadUTF8(const Uint8 *str, Uint32 *Val); +extern int WriteUTF8(Uint8 *str, Uint32 Val); +#define CheckString(str) (1) +#define CheckMem(mem,sz) (1) // TODO: Move out? -extern int64_t DivUp(int64_t value, int64_t divisor); +extern int DivUp(int value, int divisor); +extern uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem); #if DEBUG # define ENTER(str, v...) Log("%s:%i: ENTER "str, __func__, __LINE__) diff --git a/Tools/DiskTool/src/include/acess_logging.h b/Tools/DiskTool/src/include/acess_logging.h index b5d7af09..3be9e956 100644 --- a/Tools/DiskTool/src/include/acess_logging.h +++ b/Tools/DiskTool/src/include/acess_logging.h @@ -12,5 +12,6 @@ extern void Log_Debug(const char *Ident, const char *Message, ...); extern void Warning(const char *Message, ...); extern void Log(const char *Message, ...); +extern void Debug_HexDump(const char *Prefix, const void *Data, size_t Length); #endif diff --git a/Tools/DiskTool/src/include/disktool_common.h b/Tools/DiskTool/src/include/disktool_common.h index e36c3adf..f6092558 100644 --- a/Tools/DiskTool/src/include/disktool_common.h +++ b/Tools/DiskTool/src/include/disktool_common.h @@ -10,6 +10,9 @@ extern int DiskTool_MountImage(const char *Identifier, const char *Path); extern int DiskTool_Copy(const char *Source, const char *Destination); +extern int DiskTool_ListDirectory(const char *Directory); + +extern size_t DiskTool_int_TranslatePath(char *Buffer, const char *Path); #endif diff --git a/Tools/DiskTool/src/logging.c b/Tools/DiskTool/src/logging.c index 6cdb1c0b..a7a934e1 100644 --- a/Tools/DiskTool/src/logging.c +++ b/Tools/DiskTool/src/logging.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #define PUTERR(col,type) {\ @@ -33,16 +34,37 @@ void Log_Debug(const char *Ident, const char *Message, ...) PUTERR("37", "d") void Warning(const char *Message, ...) { - const char *Ident = "WARNING"; + const char *Ident = ""; PUTERR("33", "W") } void Log(const char *Message, ...) { - const char *Ident = "LOGLOG"; + const char *Ident = ""; PUTERR("37", "L") } -void Debug_HexDump(const char *Prefix, size_t Length, const void *Data) +void Debug_HexDump(const char *Prefix, const void *Data, size_t Length) { - + const uint8_t *data = Data; + size_t ofs; + fprintf(stderr, "[HexDump ]d %s: %i bytes\n", Prefix, (int)Length); + for( ofs = 0; ofs + 16 <= Length; ofs += 16 ) + { + fprintf(stderr, "[HexDump ]d %s:", Prefix); + fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", + data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + data += 8; + fprintf(stderr, " %02x %02x %02x %02x %02x %02x %02x %02x", + data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + data += 8; + fprintf(stderr, "\n"); + } + + fprintf(stderr, "[HexDump ]d %s:", Prefix); + for( ; ofs < Length; ofs ++ ) + { + if( ofs % 16 == 8 ) fprintf(stderr, " "); + fprintf(stderr, " %02x", data[ofs%16]); + } + fprintf(stderr, "\n"); } diff --git a/Tools/DiskTool/src/main.c b/Tools/DiskTool/src/main.c index 53a9598c..ff6f0ae1 100644 --- a/Tools/DiskTool/src/main.c +++ b/Tools/DiskTool/src/main.c @@ -36,6 +36,21 @@ int main(int argc, char *argv[]) } DiskTool_ListDirectory(argv[i+1]); + i += 1; + continue ; + } + + if( strcmp("cp", argv[i]) == 0 ) { + + if( argc - i < 3 ) { + fprintf(stderr, "cp takes 2 arguments (source and destination)\n"); + exit(-1); + } + + DiskTool_Copy(argv[i+1], argv[i+2]); + + i += 2; + continue ; } } return 0; @@ -78,13 +93,10 @@ int strucmp(const char *s1, const char *s2) return strcasecmp(s1, s2); } -int64_t DivUp(int64_t value, int64_t divisor) +uint64_t DivMod64U(uint64_t value, uint64_t divisor, uint64_t *remainder) { - return (value + divisor - 1) / divisor; -} - -int64_t timestamp(int sec, int min, int hr, int day, int month, int year) -{ - return 0; + if(remainder) + *remainder = value % divisor; + return value / divisor; }