From c52979f31aeb02f63ee71eaecc036316d08dc5e0 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sat, 30 Nov 2019 16:04:45 -0500 Subject: [PATCH] use optionals in more places, start implementing SEND --- adb.nim | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/adb.nim b/adb.nim index 967f8d7..e6e4b13 100644 --- a/adb.nim +++ b/adb.nim @@ -75,7 +75,6 @@ proc listDir(filename : string) : seq[FileStat] = let filenameLen : string = filename.len.uint32.unrollBytes var dirents : seq[FileStat] = @[] - var dirent : string var status : string @@ -145,7 +144,7 @@ proc recvFile(filename : string) : Option[string] = socket.close() return some(buf) -proc statFile(filename : string) : FileStat = +proc statFile(filename : string) : Option[FileStat] = # Enter sync mode let socket : Socket = syncMode() @@ -162,18 +161,41 @@ proc statFile(filename : string) : FileStat = socket.close() - FileStat(androidFileName: filename, - androidFileMode: fileMode, - androidFileSize: fileSize, - androidFileModified: fileCreated) + if (fileMode != 0 and fileSize != 0): + some(FileStat(androidFileName: filename, + androidFileMode: fileMode, + androidFileSize: fileSize, + androidFileModified: fileCreated)) + else: + none(FileStat) + +proc sendFile(buf : string, filename : string) : bool = + let stat = filename.statFile + + if stat.isSome: + # never overwrite files + # TODO add optional parameter to disable this + return false + + let fileMode = fromOct[int]("0771") -proc adbPull(filename : string) : AndroidFile = + let remoteFileName = fmt"{filename},{fileMode}" + + echo remoteFileName + + return true + + +proc adbPull(filename : string) : Option[AndroidFile] = let stat = filename.statFile + if stat.isNone: + return none(AndroidFile) + let fileBlob = filename.recvFile.get("") - AndroidFile(androidFileName: filename, - androidFileStat: stat, - androidFile: fileBlob) + some(AndroidFile(androidFileName: filename, + androidFileStat: stat.get, + androidFile: fileBlob)) proc sendAdb(payload : string) : string = var socket = adbConnect() @@ -209,7 +231,9 @@ discard execCmd("adb start-server") #stdout.write adbPull("/etc/hosts").repr -echo listDir("/etc").map(proc(f: FileStat) : string = f.androidFileName) +#echo listDir("/etc").map(proc(f: FileStat) : string = f.androidFileName) + +echo sendFile("", "/storage/7AFD-17E3/test2.opus") #discard rebootPhone()