Browse Source

implement usable copy function

master
Wesley Kerfoot 4 years ago
parent
commit
b74aa9a91a
  1. 4
      src/adbtool.nim
  2. 15
      src/adbtoolpkg/adb.nim

4
src/adbtool.nim

@ -1,5 +1,7 @@
import adbtoolpkg/adb import adbtoolpkg/adb
import os, strformat
when isMainModule: when isMainModule:
startServer() startServer()
echo listCerts() let fileSource = paramStr(1)
fileSource.androidCopyFile(fmt"/sdcard/Download/{fileSource}")

15
src/adbtoolpkg/adb.nim

@ -1,5 +1,6 @@
import net, strutils, parseutils, strformat, osproc, sequtils import net, strutils, parseutils, strformat, osproc, sequtils
import system, times, math, sugar, os, streams import system, times, math, sugar, os, streams
import posix except Time
import options except map import options except map
type FileStat = ref object of RootObj type FileStat = ref object of RootObj
@ -175,11 +176,11 @@ proc statFile(filename : string) : Option[FileStat] =
none(FileStat) none(FileStat)
proc adbSend(buf : string, proc adbSend(buf : string,
filename : string, destination : string,
permissions : string, permissions : string,
overwrite = false) : bool = overwrite = false) : bool =
let stat = filename.statFile let stat = destination.statFile
if stat.isSome and (not overwrite): if stat.isSome and (not overwrite):
# never overwrite files unless asked to # never overwrite files unless asked to
@ -189,7 +190,7 @@ proc adbSend(buf : string,
let socket : Socket = syncMode() let socket : Socket = syncMode()
let fileMode = fromOct[int](fmt"0{permissions}") let fileMode = fromOct[int](fmt"0{permissions}")
let lmtime = getTime().toUnix.uint32.unrollBytes let lmtime = getTime().toUnix.uint32.unrollBytes
let remoteFileName = fmt"{filename},{fileMode:04o}" let remoteFileName = fmt"{destination},{fileMode:04o}"
let chunks = buf.chunkString let chunks = buf.chunkString
if chunks.isNone: if chunks.isNone:
@ -217,7 +218,13 @@ proc adbSend(buf : string,
socket.close() socket.close()
true true
proc androidCopyFile*(filename : string, dest : string) =
var statResult : Stat
# See https://stackoverflow.com/a/14325854/903589 for an explanation of the bitmasks
let perms = statResult.st_mode.int and (S_IRWXU or S_IRWXG or S_IRWXO)
discard filename.readFile.adbSend(dest, fmt"0{perms}", overwrite = true)
proc adbPull(filename : string) : Option[AndroidFile] = proc adbPull(filename : string) : Option[AndroidFile] =
let stat = filename.statFile let stat = filename.statFile
if stat.isNone: if stat.isNone:

Loading…
Cancel
Save