2018-03-27 16:41:57 +00:00
|
|
|
{- Remote on Android device accessed using adb.
|
|
|
|
-
|
2019-04-09 21:52:41 +00:00
|
|
|
- Copyright 2018-2019 Joey Hess <id@joeyh.name>
|
2018-03-27 16:41:57 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Remote.Adb (remote) where
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Types.Remote
|
|
|
|
import Types.Creds
|
2018-03-27 20:10:28 +00:00
|
|
|
import Types.Export
|
2019-04-09 21:52:41 +00:00
|
|
|
import Types.Import
|
2018-03-27 16:41:57 +00:00
|
|
|
import qualified Git
|
|
|
|
import Config.Cost
|
|
|
|
import Remote.Helper.Special
|
|
|
|
import Remote.Helper.Messages
|
2019-02-20 19:55:01 +00:00
|
|
|
import Remote.Helper.ExportImport
|
2018-03-27 16:41:57 +00:00
|
|
|
import Annex.UUID
|
2018-03-27 20:10:28 +00:00
|
|
|
import Utility.Metered
|
2018-03-27 16:41:57 +00:00
|
|
|
|
2019-04-09 21:52:41 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified System.FilePath.Posix as Posix
|
|
|
|
|
2018-03-27 16:41:57 +00:00
|
|
|
-- | Each Android device has a serial number.
|
|
|
|
newtype AndroidSerial = AndroidSerial { fromAndroidSerial :: String }
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
-- | A location on an Android device.
|
|
|
|
newtype AndroidPath = AndroidPath { fromAndroidPath :: FilePath }
|
|
|
|
|
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType
|
|
|
|
{ typename = "adb"
|
|
|
|
, enumerate = const (findSpecialRemotes "adb")
|
|
|
|
, generate = gen
|
|
|
|
, setup = adbSetup
|
2018-03-27 20:10:28 +00:00
|
|
|
, exportSupported = exportIsSupported
|
2019-04-09 21:52:41 +00:00
|
|
|
, importSupported = importIsSupported
|
2018-03-27 16:41:57 +00:00
|
|
|
}
|
|
|
|
|
add RemoteStateHandle
This solves the problem of sameas remotes trampling over per-remote
state. Used for:
* per-remote state, of course
* per-remote metadata, also of course
* per-remote content identifiers, because two remote implementations
could in theory generate the same content identifier for two different
peices of content
While chunk logs are per-remote data, they don't use this, because the
number and size of chunks stored is a common property across sameas
remotes.
External special remote had a complication, where it was theoretically
possible for a remote to send SETSTATE or GETSTATE during INITREMOTE or
EXPORTSUPPORTED. Since the uuid of the remote is typically generate in
Remote.setup, it would only be possible to pass a Maybe
RemoteStateHandle into it, and it would otherwise have to construct its
own. Rather than go that route, I decided to send an ERROR in this case.
It seems unlikely that any existing external special remote will be
affected. They would have to make up a git-annex key, and set state for
some reason during INITREMOTE. I can imagine such a hack, but it doesn't
seem worth complicating the code in such an ugly way to support it.
Unfortunately, both TestRemote and Annex.Import needed the Remote
to have a new field added that holds its RemoteStateHandle.
2019-10-14 16:33:27 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> RemoteStateHandle -> Annex (Maybe Remote)
|
|
|
|
gen r u c gc rs = do
|
2018-03-27 16:41:57 +00:00
|
|
|
let this = Remote
|
|
|
|
{ uuid = u
|
|
|
|
-- adb operates over USB or wifi, so is not as cheap
|
|
|
|
-- as local, but not too expensive
|
|
|
|
, cost = semiExpensiveRemoteCost
|
|
|
|
, name = Git.repoDescribe r
|
|
|
|
, storeKey = storeKeyDummy
|
|
|
|
, retrieveKeyFile = retreiveKeyFileDummy
|
|
|
|
, retrieveKeyFileCheap = \_ _ _ -> return False
|
2018-06-21 15:35:27 +00:00
|
|
|
, retrievalSecurityPolicy = RetrievalAllKeysSecure
|
2018-03-27 16:41:57 +00:00
|
|
|
, removeKey = removeKeyDummy
|
|
|
|
, lockContent = Nothing
|
|
|
|
, checkPresent = checkPresentDummy
|
|
|
|
, checkPresentCheap = False
|
2019-01-30 18:55:28 +00:00
|
|
|
, exportActions = ExportActions
|
2018-03-27 20:10:28 +00:00
|
|
|
{ storeExport = storeExportM serial adir
|
|
|
|
, retrieveExport = retrieveExportM serial adir
|
|
|
|
, removeExport = removeExportM serial adir
|
|
|
|
, checkPresentExport = checkPresentExportM this serial adir
|
|
|
|
, removeExportDirectory = Just $ removeExportDirectoryM serial adir
|
|
|
|
, renameExport = renameExportM serial adir
|
|
|
|
}
|
2019-04-09 21:52:41 +00:00
|
|
|
, importActions = ImportActions
|
|
|
|
{ listImportableContents = listImportableContentsM serial adir
|
|
|
|
, retrieveExportWithContentIdentifier = retrieveExportWithContentIdentifierM serial adir
|
|
|
|
, storeExportWithContentIdentifier = storeExportWithContentIdentifierM serial adir
|
|
|
|
, removeExportWithContentIdentifier = removeExportWithContentIdentifierM serial adir
|
|
|
|
, removeExportDirectoryWhenEmpty = Nothing
|
|
|
|
, checkPresentExportWithContentIdentifier = checkPresentExportWithContentIdentifierM serial adir
|
|
|
|
}
|
2018-03-27 16:41:57 +00:00
|
|
|
, whereisKey = Nothing
|
|
|
|
, remoteFsck = Nothing
|
|
|
|
, repairRepo = Nothing
|
|
|
|
, config = c
|
2018-06-04 18:31:55 +00:00
|
|
|
, getRepo = return r
|
2018-03-27 16:41:57 +00:00
|
|
|
, gitconfig = gc
|
|
|
|
, localpath = Nothing
|
|
|
|
, remotetype = remote
|
|
|
|
, availability = LocallyAvailable
|
|
|
|
, readonly = False
|
2018-08-30 15:12:18 +00:00
|
|
|
, appendonly = False
|
2018-03-27 16:41:57 +00:00
|
|
|
, mkUnavailable = return Nothing
|
|
|
|
, getInfo = return
|
|
|
|
[ ("androidserial", fromAndroidSerial serial)
|
|
|
|
, ("androiddirectory", fromAndroidPath adir)
|
|
|
|
]
|
|
|
|
, claimUrl = Nothing
|
|
|
|
, checkUrl = Nothing
|
add RemoteStateHandle
This solves the problem of sameas remotes trampling over per-remote
state. Used for:
* per-remote state, of course
* per-remote metadata, also of course
* per-remote content identifiers, because two remote implementations
could in theory generate the same content identifier for two different
peices of content
While chunk logs are per-remote data, they don't use this, because the
number and size of chunks stored is a common property across sameas
remotes.
External special remote had a complication, where it was theoretically
possible for a remote to send SETSTATE or GETSTATE during INITREMOTE or
EXPORTSUPPORTED. Since the uuid of the remote is typically generate in
Remote.setup, it would only be possible to pass a Maybe
RemoteStateHandle into it, and it would otherwise have to construct its
own. Rather than go that route, I decided to send an ERROR in this case.
It seems unlikely that any existing external special remote will be
affected. They would have to make up a git-annex key, and set state for
some reason during INITREMOTE. I can imagine such a hack, but it doesn't
seem worth complicating the code in such an ugly way to support it.
Unfortunately, both TestRemote and Annex.Import needed the Remote
to have a new field added that holds its RemoteStateHandle.
2019-10-14 16:33:27 +00:00
|
|
|
, remoteStateHandle = rs
|
2018-03-27 16:41:57 +00:00
|
|
|
}
|
|
|
|
return $ Just $ specialRemote c
|
|
|
|
(simplyPrepare $ store serial adir)
|
|
|
|
(simplyPrepare $ retrieve serial adir)
|
|
|
|
(simplyPrepare $ remove serial adir)
|
|
|
|
(simplyPrepare $ checkKey this serial adir)
|
|
|
|
this
|
|
|
|
where
|
|
|
|
adir = maybe (giveup "missing androiddirectory") AndroidPath
|
|
|
|
(remoteAnnexAndroidDirectory gc)
|
|
|
|
serial = maybe (giveup "missing androidserial") AndroidSerial
|
|
|
|
(remoteAnnexAndroidSerial gc)
|
|
|
|
|
|
|
|
adbSetup :: SetupStage -> Maybe UUID -> Maybe CredPair -> RemoteConfig -> RemoteGitConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
adbSetup _ mu _ c gc = do
|
|
|
|
u <- maybe (liftIO genUUID) return mu
|
|
|
|
|
|
|
|
-- verify configuration
|
|
|
|
adir <- maybe (giveup "Specify androiddirectory=") (pure . AndroidPath)
|
|
|
|
(M.lookup "androiddirectory" c)
|
|
|
|
serial <- getserial =<< liftIO enumerateAdbConnected
|
2018-03-27 21:34:31 +00:00
|
|
|
let c' = M.insert "androidserial" (fromAndroidSerial serial) c
|
2018-03-27 16:41:57 +00:00
|
|
|
|
2018-03-27 21:34:31 +00:00
|
|
|
(c'', _encsetup) <- encryptionSetup c' gc
|
2018-03-27 16:41:57 +00:00
|
|
|
|
|
|
|
ok <- liftIO $ adbShellBool serial
|
|
|
|
[Param "mkdir", Param "-p", File (fromAndroidPath adir)]
|
|
|
|
unless ok $
|
|
|
|
giveup "Creating directory on Android device failed."
|
|
|
|
|
2018-03-27 21:34:31 +00:00
|
|
|
gitConfigSpecialRemote u c''
|
2018-03-27 16:41:57 +00:00
|
|
|
[ ("adb", "true")
|
|
|
|
, ("androiddirectory", fromAndroidPath adir)
|
|
|
|
, ("androidserial", fromAndroidSerial serial)
|
|
|
|
]
|
|
|
|
|
2018-03-27 21:34:31 +00:00
|
|
|
return (c'', u)
|
2018-03-27 16:41:57 +00:00
|
|
|
where
|
|
|
|
getserial [] = giveup "adb does not list any connected android devices. Plug in an Android device, or configure adb, and try again.."
|
|
|
|
getserial l = case M.lookup "androidserial" c of
|
2018-03-27 21:34:31 +00:00
|
|
|
Nothing -> case l of
|
|
|
|
(s:[]) -> return s
|
|
|
|
_ -> giveup $ unlines $
|
|
|
|
"There are multiple connected android devices, specify which to use with androidserial="
|
|
|
|
: map fromAndroidSerial l
|
2018-03-27 16:41:57 +00:00
|
|
|
Just cs
|
|
|
|
| AndroidSerial cs `elem` l -> return (AndroidSerial cs)
|
|
|
|
| otherwise -> giveup $ "The device with androidserial=" ++ cs ++ " is not connected."
|
|
|
|
|
|
|
|
store :: AndroidSerial -> AndroidPath -> Storer
|
2018-03-27 20:10:28 +00:00
|
|
|
store serial adir = fileStorer $ \k src _p ->
|
|
|
|
let dest = androidLocation adir k
|
|
|
|
in store' serial dest src
|
|
|
|
|
|
|
|
store' :: AndroidSerial -> AndroidPath -> FilePath -> Annex Bool
|
2019-04-09 23:11:38 +00:00
|
|
|
store' serial dest src = store'' serial dest src (return True)
|
2019-04-09 21:52:41 +00:00
|
|
|
|
2019-04-09 23:11:38 +00:00
|
|
|
store'' :: AndroidSerial -> AndroidPath -> FilePath -> Annex Bool -> Annex Bool
|
|
|
|
store'' serial dest src canoverwrite = do
|
2018-03-27 20:10:28 +00:00
|
|
|
let destdir = takeDirectory $ fromAndroidPath dest
|
|
|
|
liftIO $ void $ adbShell serial [Param "mkdir", Param "-p", File destdir]
|
2018-03-27 16:41:57 +00:00
|
|
|
showOutput -- make way for adb push output
|
2019-04-09 21:52:41 +00:00
|
|
|
let tmpdest = fromAndroidPath dest ++ ".annextmp"
|
2018-03-27 16:41:57 +00:00
|
|
|
ifM (liftIO $ boolSystem "adb" (mkAdbCommand serial [Param "push", File src, File tmpdest]))
|
2019-04-09 23:11:38 +00:00
|
|
|
( ifM canoverwrite
|
|
|
|
-- move into place atomically
|
|
|
|
( liftIO $ adbShellBool serial [Param "mv", File tmpdest, File (fromAndroidPath dest)]
|
|
|
|
, do
|
2019-04-09 21:52:41 +00:00
|
|
|
void $ remove' serial (AndroidPath tmpdest)
|
2019-04-09 23:11:38 +00:00
|
|
|
return False
|
|
|
|
)
|
|
|
|
, return False
|
2018-03-27 16:41:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
retrieve :: AndroidSerial -> AndroidPath -> Retriever
|
2018-03-27 20:10:28 +00:00
|
|
|
retrieve serial adir = fileRetriever $ \dest k _p ->
|
|
|
|
let src = androidLocation adir k
|
|
|
|
in unlessM (retrieve' serial src dest) $
|
|
|
|
giveup "adb pull failed"
|
|
|
|
|
|
|
|
retrieve' :: AndroidSerial -> AndroidPath -> FilePath -> Annex Bool
|
|
|
|
retrieve' serial src dest = do
|
2018-03-27 16:41:57 +00:00
|
|
|
showOutput -- make way for adb pull output
|
2018-03-27 20:10:28 +00:00
|
|
|
liftIO $ boolSystem "adb" $ mkAdbCommand serial
|
2018-03-27 16:41:57 +00:00
|
|
|
[ Param "pull"
|
2018-03-27 20:10:28 +00:00
|
|
|
, File $ fromAndroidPath src
|
|
|
|
, File dest
|
2018-03-27 16:41:57 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
remove :: AndroidSerial -> AndroidPath -> Remover
|
2018-03-27 20:10:28 +00:00
|
|
|
remove serial adir k = remove' serial (androidLocation adir k)
|
|
|
|
|
|
|
|
remove' :: AndroidSerial -> AndroidPath -> Annex Bool
|
|
|
|
remove' serial aloc = liftIO $ adbShellBool serial
|
|
|
|
[Param "rm", Param "-f", File (fromAndroidPath aloc)]
|
2018-03-27 16:41:57 +00:00
|
|
|
|
|
|
|
checkKey :: Remote -> AndroidSerial -> AndroidPath -> CheckPresent
|
2018-03-27 20:10:28 +00:00
|
|
|
checkKey r serial adir k = checkKey' r serial (androidLocation adir k)
|
|
|
|
|
|
|
|
checkKey' :: Remote -> AndroidSerial -> AndroidPath -> Annex Bool
|
|
|
|
checkKey' r serial aloc = do
|
2018-03-27 16:41:57 +00:00
|
|
|
showChecking r
|
2019-04-09 21:52:41 +00:00
|
|
|
out <- liftIO $ adbShellRaw serial $ unwords
|
2018-03-27 20:10:28 +00:00
|
|
|
[ "if test -e ", shellEscape (fromAndroidPath aloc)
|
2018-03-27 16:41:57 +00:00
|
|
|
, "; then echo y"
|
|
|
|
, "; else echo n"
|
|
|
|
, "; fi"
|
|
|
|
]
|
2019-04-09 21:52:41 +00:00
|
|
|
case out of
|
|
|
|
Just ["y"] -> return True
|
|
|
|
Just ["n"] -> return False
|
|
|
|
_ -> giveup "unable to access Android device"
|
2018-03-27 16:41:57 +00:00
|
|
|
|
|
|
|
androidLocation :: AndroidPath -> Key -> AndroidPath
|
|
|
|
androidLocation adir k = AndroidPath $
|
2019-01-14 17:03:35 +00:00
|
|
|
fromAndroidPath (androidHashDir adir k) ++ serializeKey k
|
2018-03-27 16:41:57 +00:00
|
|
|
|
|
|
|
androidHashDir :: AndroidPath -> Key -> AndroidPath
|
|
|
|
androidHashDir adir k = AndroidPath $
|
|
|
|
fromAndroidPath adir ++ "/" ++ hdir
|
|
|
|
where
|
2019-12-11 18:12:22 +00:00
|
|
|
hdir = replace [pathSeparator] "/" (fromRawFilePath (hashDirLower def k))
|
2018-03-27 16:41:57 +00:00
|
|
|
|
2018-03-27 20:10:28 +00:00
|
|
|
storeExportM :: AndroidSerial -> AndroidPath -> FilePath -> Key -> ExportLocation -> MeterUpdate -> Annex Bool
|
|
|
|
storeExportM serial adir src _k loc _p = store' serial dest src
|
|
|
|
where
|
|
|
|
dest = androidExportLocation adir loc
|
|
|
|
|
|
|
|
retrieveExportM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> FilePath -> MeterUpdate -> Annex Bool
|
|
|
|
retrieveExportM serial adir _k loc dest _p = retrieve' serial src dest
|
|
|
|
where
|
|
|
|
src = androidExportLocation adir loc
|
|
|
|
|
|
|
|
removeExportM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> Annex Bool
|
|
|
|
removeExportM serial adir _k loc = remove' serial aloc
|
|
|
|
where
|
|
|
|
aloc = androidExportLocation adir loc
|
|
|
|
|
|
|
|
removeExportDirectoryM :: AndroidSerial -> AndroidPath -> ExportDirectory -> Annex Bool
|
|
|
|
removeExportDirectoryM serial abase dir = liftIO $ adbShellBool serial
|
|
|
|
[Param "rm", Param "-rf", File (fromAndroidPath adir)]
|
|
|
|
where
|
|
|
|
adir = androidExportLocation abase (mkExportLocation (fromExportDirectory dir))
|
|
|
|
|
|
|
|
checkPresentExportM :: Remote -> AndroidSerial -> AndroidPath -> Key -> ExportLocation -> Annex Bool
|
|
|
|
checkPresentExportM r serial adir _k loc = checkKey' r serial aloc
|
|
|
|
where
|
|
|
|
aloc = androidExportLocation adir loc
|
|
|
|
|
2019-03-11 16:44:12 +00:00
|
|
|
renameExportM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> ExportLocation -> Annex (Maybe Bool)
|
|
|
|
renameExportM serial adir _k old new = liftIO $ Just <$>
|
|
|
|
adbShellBool serial [Param "mv", Param "-f", File oldloc, File newloc]
|
2018-03-27 20:10:28 +00:00
|
|
|
where
|
|
|
|
oldloc = fromAndroidPath $ androidExportLocation adir old
|
|
|
|
newloc = fromAndroidPath $ androidExportLocation adir new
|
|
|
|
|
2019-04-09 21:52:41 +00:00
|
|
|
listImportableContentsM :: AndroidSerial -> AndroidPath -> Annex (Maybe (ImportableContents (ContentIdentifier, ByteSize)))
|
|
|
|
listImportableContentsM serial adir = liftIO $
|
|
|
|
process <$> adbShell serial
|
|
|
|
[ Param "find"
|
|
|
|
-- trailing slash is needed, or android's find command
|
|
|
|
-- won't recurse into the directory
|
|
|
|
, File $ fromAndroidPath adir ++ "/"
|
|
|
|
, Param "-type", Param "f"
|
|
|
|
, Param "-exec", Param "stat"
|
|
|
|
, Param "-c", Param statformat
|
|
|
|
, Param "{}", Param "+"
|
|
|
|
]
|
|
|
|
where
|
|
|
|
process Nothing = Nothing
|
|
|
|
process (Just ls) = Just $ ImportableContents (mapMaybe mk ls) []
|
|
|
|
|
|
|
|
statformat = adbStatFormat ++ "\t%n"
|
|
|
|
|
|
|
|
mk ('S':'T':'\t':l) =
|
|
|
|
let (stat, fn) = separate (== '\t') l
|
|
|
|
sz = fromMaybe 0 (readish (takeWhile (/= ' ') stat))
|
|
|
|
cid = ContentIdentifier (encodeBS' stat)
|
2019-12-02 16:26:33 +00:00
|
|
|
loc = mkImportLocation $ toRawFilePath $
|
2019-04-09 21:52:41 +00:00
|
|
|
Posix.makeRelative (fromAndroidPath adir) fn
|
|
|
|
in Just (loc, (cid, sz))
|
|
|
|
mk _ = Nothing
|
|
|
|
|
|
|
|
-- This does not guard against every possible race. As long as the adb
|
|
|
|
-- connection is resonably fast, it's probably as good as
|
|
|
|
-- git's handling of similar situations with files being modified while
|
|
|
|
-- it's updating the working tree for a merge.
|
|
|
|
retrieveExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> ExportLocation -> ContentIdentifier -> FilePath -> Annex (Maybe Key) -> MeterUpdate -> Annex (Maybe Key)
|
|
|
|
retrieveExportWithContentIdentifierM serial adir loc cid dest mkkey _p = catchDefaultIO Nothing $
|
|
|
|
ifM (retrieve' serial src dest)
|
|
|
|
( do
|
|
|
|
k <- mkkey
|
|
|
|
currcid <- liftIO $ getExportContentIdentifier serial adir loc
|
|
|
|
return $ if currcid == Right (Just cid)
|
|
|
|
then k
|
|
|
|
else Nothing
|
|
|
|
, return Nothing
|
|
|
|
)
|
|
|
|
where
|
|
|
|
src = androidExportLocation adir loc
|
|
|
|
|
2019-08-13 16:05:00 +00:00
|
|
|
storeExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> FilePath -> Key -> ExportLocation -> [ContentIdentifier] -> MeterUpdate -> Annex (Either String ContentIdentifier)
|
|
|
|
storeExportWithContentIdentifierM serial adir src _k loc overwritablecids _p =
|
2019-04-09 21:52:41 +00:00
|
|
|
-- Check if overwrite is safe before sending, because sending the
|
|
|
|
-- file is expensive and don't want to do it unncessarily.
|
2019-04-09 23:11:38 +00:00
|
|
|
ifM checkcanoverwrite
|
|
|
|
( ifM (store'' serial dest src checkcanoverwrite)
|
2019-08-13 16:05:00 +00:00
|
|
|
( liftIO $ getExportContentIdentifier serial adir loc >>= return . \case
|
|
|
|
Right (Just cid) -> Right cid
|
|
|
|
Right Nothing -> Left "adb failed to store file"
|
|
|
|
Left _ -> Left "unable to get content identifier for file stored on adtb"
|
|
|
|
, return $ Left "adb failed to store file"
|
2019-04-09 23:11:38 +00:00
|
|
|
)
|
2019-08-13 16:05:00 +00:00
|
|
|
, return $ Left "unsafe to overwrite file"
|
2019-04-09 23:11:38 +00:00
|
|
|
)
|
2019-04-09 21:52:41 +00:00
|
|
|
where
|
|
|
|
dest = androidExportLocation adir loc
|
|
|
|
checkcanoverwrite = liftIO $
|
|
|
|
getExportContentIdentifier serial adir loc >>= return . \case
|
2019-04-09 23:11:38 +00:00
|
|
|
Right (Just cid) | cid `elem` overwritablecids -> True
|
|
|
|
Right Nothing -> True
|
|
|
|
_ -> False
|
2019-04-09 21:52:41 +00:00
|
|
|
|
|
|
|
removeExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> [ContentIdentifier] -> Annex Bool
|
|
|
|
removeExportWithContentIdentifierM serial adir k loc removeablecids = catchBoolIO $
|
|
|
|
liftIO (getExportContentIdentifier serial adir loc) >>= \case
|
|
|
|
Right Nothing -> return True
|
|
|
|
Right (Just cid) | cid `elem` removeablecids ->
|
|
|
|
removeExportM serial adir k loc
|
|
|
|
_ -> return False
|
|
|
|
|
|
|
|
checkPresentExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> [ContentIdentifier] -> Annex Bool
|
|
|
|
checkPresentExportWithContentIdentifierM serial adir _k loc knowncids =
|
|
|
|
liftIO $ getExportContentIdentifier serial adir loc >>= \case
|
|
|
|
Right (Just cid) | cid `elem` knowncids -> return True
|
|
|
|
Right _ -> return False
|
|
|
|
Left _ -> giveup "unable to access Android device"
|
|
|
|
|
2018-03-27 20:10:28 +00:00
|
|
|
androidExportLocation :: AndroidPath -> ExportLocation -> AndroidPath
|
|
|
|
androidExportLocation adir loc = AndroidPath $
|
2019-12-02 16:26:33 +00:00
|
|
|
fromAndroidPath adir ++ "/" ++ fromRawFilePath (fromExportLocation loc)
|
2018-03-27 20:10:28 +00:00
|
|
|
|
2018-03-27 16:41:57 +00:00
|
|
|
-- | List all connected Android devices.
|
|
|
|
enumerateAdbConnected :: IO [AndroidSerial]
|
|
|
|
enumerateAdbConnected =
|
|
|
|
mapMaybe parse . lines <$> readProcess "adb" ["devices"]
|
|
|
|
where
|
|
|
|
parse l =
|
|
|
|
let (serial, desc) = separate (== '\t') l
|
2018-06-12 17:56:01 +00:00
|
|
|
in if null desc || length serial < 4
|
2018-03-27 16:41:57 +00:00
|
|
|
then Nothing
|
|
|
|
else Just (AndroidSerial serial)
|
|
|
|
|
|
|
|
-- | Runs a command on the android device with the given serial number.
|
|
|
|
--
|
2019-04-09 21:52:41 +00:00
|
|
|
-- Any stdout from the command is returned, separated into lines.
|
|
|
|
adbShell :: AndroidSerial -> [CommandParam] -> IO (Maybe [String])
|
2018-03-27 16:41:57 +00:00
|
|
|
adbShell serial cmd = adbShellRaw serial $
|
|
|
|
unwords $ map shellEscape (toCommand cmd)
|
|
|
|
|
|
|
|
adbShellBool :: AndroidSerial -> [CommandParam] -> IO Bool
|
2019-04-09 21:52:41 +00:00
|
|
|
adbShellBool serial cmd =
|
|
|
|
adbShellRaw serial cmd' >>= return . \case
|
|
|
|
Just l -> end l == ["y"]
|
|
|
|
Nothing -> False
|
|
|
|
where
|
|
|
|
cmd' = "if " ++ unwords (map shellEscape (toCommand cmd))
|
|
|
|
++ "; then echo y; else echo n; fi"
|
2018-03-27 16:41:57 +00:00
|
|
|
|
|
|
|
-- | Runs a raw shell command on the android device.
|
|
|
|
-- Any necessary shellEscaping must be done by caller.
|
2019-04-09 21:52:41 +00:00
|
|
|
adbShellRaw :: AndroidSerial -> String -> IO (Maybe [String])
|
|
|
|
adbShellRaw serial cmd = catchMaybeIO $
|
|
|
|
processoutput <$> readProcess "adb"
|
|
|
|
[ "-s"
|
|
|
|
, fromAndroidSerial serial
|
|
|
|
, "shell"
|
|
|
|
, cmd
|
|
|
|
]
|
2018-03-27 16:41:57 +00:00
|
|
|
where
|
2019-04-09 21:52:41 +00:00
|
|
|
processoutput s = map trimcr (lines s)
|
2018-03-27 16:41:57 +00:00
|
|
|
-- For some reason, adb outputs lines with \r\n on linux,
|
|
|
|
-- despite both linux and android being unix systems.
|
|
|
|
trimcr = takeWhile (/= '\r')
|
|
|
|
|
|
|
|
mkAdbCommand :: AndroidSerial -> [CommandParam] -> [CommandParam]
|
|
|
|
mkAdbCommand serial cmd = [Param "-s", Param (fromAndroidSerial serial)] ++ cmd
|
2019-04-09 21:52:41 +00:00
|
|
|
|
|
|
|
-- Gets the current content identifier for a file on the android device.
|
2019-08-13 16:05:00 +00:00
|
|
|
-- If the file is not present, returns Right Nothing
|
2019-04-09 21:52:41 +00:00
|
|
|
getExportContentIdentifier :: AndroidSerial -> AndroidPath -> ExportLocation -> IO (Either ExitCode (Maybe ContentIdentifier))
|
|
|
|
getExportContentIdentifier serial adir loc = liftIO $ do
|
|
|
|
ls <- adbShellRaw serial $ unwords
|
|
|
|
[ "if test -e ", shellEscape aloc
|
|
|
|
, "; then stat -c"
|
|
|
|
, shellEscape adbStatFormat
|
|
|
|
, shellEscape aloc
|
|
|
|
, "; else echo n"
|
|
|
|
, "; fi"
|
|
|
|
]
|
|
|
|
return $ case ls of
|
|
|
|
Just ["n"] -> Right Nothing
|
|
|
|
Just (('S':'T':'\t':stat):[]) -> Right $ Just $
|
|
|
|
ContentIdentifier (encodeBS' stat)
|
|
|
|
_ -> Left (ExitFailure 1)
|
|
|
|
where
|
|
|
|
aloc = fromAndroidPath $ androidExportLocation adir loc
|
|
|
|
|
|
|
|
-- Includes size, modificiation time, and inode.
|
|
|
|
-- Device not included because the adb interface ensures we're talking to
|
|
|
|
-- the same android device.
|
|
|
|
adbStatFormat :: String
|
|
|
|
adbStatFormat = "ST\t%s %Y %i"
|