2018-03-27 16:41:57 +00:00
|
|
|
{- Remote on Android device accessed using adb.
|
|
|
|
-
|
2022-05-09 17:18:47 +00:00
|
|
|
- Copyright 2018-2022 Joey Hess <id@joeyh.name>
|
2018-03-27 16:41:57 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
incremental verify for byteRetriever special remotes
Several special remotes verify content while it is being retrieved,
avoiding a separate checksum pass. They are: S3, bup, ddar, and
gcrypt (with a local repository).
Not done when using chunking, yet.
Complicated by Retriever needing to change to be polymorphic. Which in turn
meant RankNTypes is needed, and also needed some code changes. The
change in Remote.External does not change behavior at all but avoids
the type checking failing because of a "rigid, skolem type" which
"would escape its scope". So I refactored slightly to make the type
checker's job easier there.
Unfortunately, directory uses fileRetriever (except when chunked),
so it is not amoung the improved ones. Fixing that would need a way for
FileRetriever to return a Verification. But, since the file retrieved
may be encrypted or chunked, it would be extra work to always
incrementally checksum the file while retrieving it. Hm.
Some other special remotes use fileRetriever, and so don't get incremental
verification, but could be converted to byteRetriever later. One is
GitLFS, which uses downloadConduit, which writes to the file, so could
verify as it goes. Other special remotes like web could too, but don't
use Remote.Helper.Special and so will need to be addressed separately.
Sponsored-by: Dartmouth College's DANDI project
2021-08-11 17:43:30 +00:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
incremental verify for byteRetriever special remotes
Several special remotes verify content while it is being retrieved,
avoiding a separate checksum pass. They are: S3, bup, ddar, and
gcrypt (with a local repository).
Not done when using chunking, yet.
Complicated by Retriever needing to change to be polymorphic. Which in turn
meant RankNTypes is needed, and also needed some code changes. The
change in Remote.External does not change behavior at all but avoids
the type checking failing because of a "rigid, skolem type" which
"would escape its scope". So I refactored slightly to make the type
checker's job easier there.
Unfortunately, directory uses fileRetriever (except when chunked),
so it is not amoung the improved ones. Fixing that would need a way for
FileRetriever to return a Verification. But, since the file retrieved
may be encrypted or chunked, it would be extra work to always
incrementally checksum the file while retrieving it. Hm.
Some other special remotes use fileRetriever, and so don't get incremental
verification, but could be converted to byteRetriever later. One is
GitLFS, which uses downloadConduit, which writes to the file, so could
verify as it goes. Other special remotes like web could too, but don't
use Remote.Helper.Special and so will need to be addressed separately.
Sponsored-by: Dartmouth College's DANDI project
2021-08-11 17:43:30 +00:00
|
|
|
|
2018-03-27 16:41:57 +00:00
|
|
|
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
|
2023-01-12 17:42:28 +00:00
|
|
|
import Config
|
2018-03-27 16:41:57 +00:00
|
|
|
import Config.Cost
|
|
|
|
import Remote.Helper.Special
|
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
|
2020-01-10 18:10:20 +00:00
|
|
|
import Types.ProposedAccepted
|
2020-01-14 19:41:34 +00:00
|
|
|
import Annex.SpecialRemote.Config
|
2022-05-09 17:18:47 +00:00
|
|
|
import Annex.Verify
|
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
|
2020-01-14 19:41:34 +00:00
|
|
|
remote = specialRemoteType $ RemoteType
|
2018-03-27 16:41:57 +00:00
|
|
|
{ typename = "adb"
|
|
|
|
, enumerate = const (findSpecialRemotes "adb")
|
|
|
|
, generate = gen
|
2020-01-14 19:41:34 +00:00
|
|
|
, configParser = mkRemoteConfigParser
|
|
|
|
[ optionalStringParser androiddirectoryField
|
2020-01-20 19:20:04 +00:00
|
|
|
(FieldDesc "location on the Android device where the files are stored")
|
2020-01-14 19:41:34 +00:00
|
|
|
, optionalStringParser androidserialField
|
2020-01-20 19:20:04 +00:00
|
|
|
(FieldDesc "sometimes needed to specify which Android device to use")
|
2022-01-11 01:15:30 +00:00
|
|
|
, yesNoParser ignorefinderrorField (Just False)
|
|
|
|
(FieldDesc "ignore adb find errors")
|
2022-07-13 22:00:47 +00:00
|
|
|
, yesNoParser oldandroidField (Just False)
|
|
|
|
(FieldDesc "support old versions of android (slower)")
|
2020-01-14 19:41:34 +00:00
|
|
|
]
|
2018-03-27 16:41:57 +00:00
|
|
|
, setup = adbSetup
|
2018-03-27 20:10:28 +00:00
|
|
|
, exportSupported = exportIsSupported
|
2019-04-09 21:52:41 +00:00
|
|
|
, importSupported = importIsSupported
|
add thirdPartyPopulated interface
This is to support, eg a borg repo as a special remote, which is
populated not by running git-annex commands, but by using borg. Then
git-annex sync lists the content of the remote, learns which files are
annex objects, and treats those as present in the remote.
So, most of the import machinery is reused, to a new purpose. While
normally importtree maintains a remote tracking branch, this does not,
because the files stored in the remote are annex object files, not
user-visible filenames. But, internally, a git tree is still generated,
of the files on the remote that are annex objects. This tree is used
by retrieveExportWithContentIdentifier, etc. As with other import/export
remotes, that the tree is recorded in the export log, and gets grafted
into the git-annex branch.
importKey changed to be able to return Nothing, to indicate when an
ImportLocation is not an annex object and so should be skipped from
being included in the tree.
It did not seem to make sense to have git-annex import do this, since
from the user's perspective, it's not like other imports. So only
git-annex sync does it.
Note that, git-annex sync does not yet download objects from such
remotes that are preferred content. importKeys is run with
content downloading disabled, to avoid getting the content of all
objects. Perhaps what's needed is for seekSyncContent to be run with these
remotes, but I don't know if it will just work (in particular, it needs
to avoid trying to transfer objects to them), so I skipped that for now.
(Untested and unused as of yet.)
This commit was sponsored by Jochen Bartl on Patreon.
2020-12-18 18:52:57 +00:00
|
|
|
, thirdPartyPopulated = False
|
2018-03-27 16:41:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 19:41:34 +00:00
|
|
|
androiddirectoryField :: RemoteConfigField
|
|
|
|
androiddirectoryField = Accepted "androiddirectory"
|
|
|
|
|
|
|
|
androidserialField :: RemoteConfigField
|
|
|
|
androidserialField = Accepted "androidserial"
|
|
|
|
|
2022-01-11 01:15:30 +00:00
|
|
|
ignorefinderrorField :: RemoteConfigField
|
|
|
|
ignorefinderrorField = Accepted "ignorefinderror"
|
|
|
|
|
2022-07-13 22:00:47 +00:00
|
|
|
oldandroidField :: RemoteConfigField
|
|
|
|
oldandroidField = Accepted "oldandroid"
|
2022-01-11 01:15:30 +00:00
|
|
|
|
fix encryption of content to gcrypt and git-lfs
Fix serious regression in gcrypt and encrypted git-lfs remotes.
Since version 7.20200202.7, git-annex incorrectly stored content
on those remotes without encrypting it.
Problem was, Remote.Git enumerates all git remotes, including git-lfs
and gcrypt. It then dispatches to those. So, Remote.List used the
RemoteConfigParser from Remote.Git, instead of from git-lfs or gcrypt,
and that parser does not know about encryption fields, so did not
include them in the ParsedRemoteConfig. (Also didn't include other
fields specific to those remotes, perhaps chunking etc also didn't
get through.)
To fix, had to move RemoteConfig parsing down into the generate methods
of each remote, rather than doing it in Remote.List.
And a consequence of that was that ParsedRemoteConfig had to change to
include the RemoteConfig that got parsed, so that testremote can
generate a new remote based on an existing remote.
(I would have rather fixed this just inside Remote.Git, but that was not
practical, at least not w/o re-doing work that Remote.List already did.
Big ugly mostly mechanical patch seemed preferable to making git-annex
slower.)
2020-02-26 21:20:56 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> RemoteStateHandle -> Annex (Maybe Remote)
|
|
|
|
gen r u rc gc rs = do
|
|
|
|
c <- parsedRemoteConfig remote rc
|
2023-01-12 17:42:28 +00:00
|
|
|
-- adb operates over USB or wifi, so is not as cheap
|
|
|
|
-- as local, but not too expensive
|
|
|
|
cst <- remoteCost gc c semiExpensiveRemoteCost
|
2018-03-27 16:41:57 +00:00
|
|
|
let this = Remote
|
|
|
|
{ uuid = u
|
2023-01-12 17:42:28 +00:00
|
|
|
, cost = cst
|
2018-03-27 16:41:57 +00:00
|
|
|
, name = Git.repoDescribe r
|
|
|
|
, storeKey = storeKeyDummy
|
2020-05-13 21:05:56 +00:00
|
|
|
, retrieveKeyFile = retrieveKeyFileDummy
|
|
|
|
, retrieveKeyFileCheap = Nothing
|
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
|
remove "checking remotename" message
This fixes fsck of a remote that uses chunking displaying
(checking remotename) (checking remotename)" for every chunk.
Also, some remotes displayed the message, and others did not, with no
consistency. It was originally displayed only when accessing remotes
that were expensive or might involve a password prompt, I think, but
nothing in the API said when to do it so it became an inconsistent mess.
Originally I thought fsck should always display it. But it only displays
in fsck --from remote, so the user knows the remote is being accessed,
so there is no reason to tell them it's accessing it over and over.
It was also possible for git-annex move to sometimes display it twice,
due to checking if content is present twice. But, the user of move
specifies --from/--to, so it does not need to display when it's
accessing the remote, as the user expects it to access the remote.
git-annex get might display it, but only if the remote also supports
hasKeyCheap, which is really only local git remotes, which didn't
display it always; and in any case nothing displayed it before hasKeyCheap,
which is checked first, so I don't think this needs to display it ever.
mirror is like move. And that's all the main places it would have been
displayed.
This commit was sponsored by Jochen Bartl on Patreon.
2021-04-27 16:50:45 +00:00
|
|
|
, checkPresentExport = checkPresentExportM serial adir
|
2018-03-27 20:10:28 +00:00
|
|
|
, removeExportDirectory = Just $ removeExportDirectoryM serial adir
|
2024-03-09 17:37:51 +00:00
|
|
|
, renameExport = Just $ renameExportM serial adir
|
2018-03-27 20:10:28 +00:00
|
|
|
}
|
2019-04-09 21:52:41 +00:00
|
|
|
, importActions = ImportActions
|
2022-01-11 01:15:30 +00:00
|
|
|
{ listImportableContents = listImportableContentsM serial adir c
|
2020-07-03 18:02:50 +00:00
|
|
|
, importKey = Nothing
|
2019-04-09 21:52:41 +00:00
|
|
|
, 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
|
2023-08-16 18:31:31 +00:00
|
|
|
, availability = pure LocallyAvailable
|
2018-03-27 16:41:57 +00:00
|
|
|
, readonly = False
|
2018-08-30 15:12:18 +00:00
|
|
|
, appendonly = False
|
2020-12-28 19:08:53 +00:00
|
|
|
, untrustworthy = 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
|
2020-05-13 15:50:31 +00:00
|
|
|
(store serial adir)
|
|
|
|
(retrieve serial adir)
|
|
|
|
(remove serial adir)
|
remove "checking remotename" message
This fixes fsck of a remote that uses chunking displaying
(checking remotename) (checking remotename)" for every chunk.
Also, some remotes displayed the message, and others did not, with no
consistency. It was originally displayed only when accessing remotes
that were expensive or might involve a password prompt, I think, but
nothing in the API said when to do it so it became an inconsistent mess.
Originally I thought fsck should always display it. But it only displays
in fsck --from remote, so the user knows the remote is being accessed,
so there is no reason to tell them it's accessing it over and over.
It was also possible for git-annex move to sometimes display it twice,
due to checking if content is present twice. But, the user of move
specifies --from/--to, so it does not need to display when it's
accessing the remote, as the user expects it to access the remote.
git-annex get might display it, but only if the remote also supports
hasKeyCheap, which is really only local git remotes, which didn't
display it always; and in any case nothing displayed it before hasKeyCheap,
which is checked first, so I don't think this needs to display it ever.
mirror is like move. And that's all the main places it would have been
displayed.
This commit was sponsored by Jochen Bartl on Patreon.
2021-04-27 16:50:45 +00:00
|
|
|
(checkKey serial adir)
|
2018-03-27 16:41:57 +00:00
|
|
|
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
|
2020-01-10 18:10:20 +00:00
|
|
|
adir <- maybe
|
|
|
|
(giveup "Specify androiddirectory=")
|
|
|
|
(pure . AndroidPath . fromProposedAccepted)
|
2020-01-14 19:41:34 +00:00
|
|
|
(M.lookup androiddirectoryField c)
|
2020-04-02 14:46:46 +00:00
|
|
|
serial <- getserial =<< enumerateAdbConnected
|
2020-01-14 19:41:34 +00:00
|
|
|
let c' = M.insert androidserialField (Proposed (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
|
|
|
|
2020-04-02 14:46:46 +00:00
|
|
|
ok <- adbShellBool serial
|
2018-03-27 16:41:57 +00:00
|
|
|
[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.."
|
2020-01-14 19:41:34 +00:00
|
|
|
getserial l = case fromProposedAccepted <$> M.lookup androidserialField 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
|
2020-05-13 18:03:00 +00:00
|
|
|
in unlessM (store' serial dest src) $
|
|
|
|
giveup "adb failed"
|
2018-03-27 20:10:28 +00:00
|
|
|
|
|
|
|
store' :: AndroidSerial -> AndroidPath -> FilePath -> Annex Bool
|
adb send to final filename not tmp file
Avoids some problems with unusual character in exporttree filenames
that confuse adb shell commands.
In particular, with a filename that contains \351, adb push sends the file
to the correct filename in /sdcard. And running find on the android device
roundtrips the filename. But, running mv on that filename on the android
device fails with "bad <filename>: No such file or directory".
Interestingly, ls on android works, and rm fails.
adb push to the final name to avoids this problem. But what about
atomicity? Well, I tried an adb push and interrupted it part way through.
The file was present while the push was running, but was removed once the
push got interrupted. I also tried yanking the cable while adb push was
running, and the partially received file was also deleted then. That avoids
most problems.
An import that runs at the same time as an export will see the partially
sent file. But that is unlikely to be done, and if it did happen, it would
notice that the imported file had changed in the meantime and discard it.
Note that, since rm on the android device fails on these filenames,
exporting a tree where the file is deleted is going to fail to remove it. I
don't see what I can do about that, so long as android is using an rm that
has issues with filename encodings.
This was tested on a phone where find, ls, and rm all come from Toybox 0.8.6.
Sponsored-by: unqueued on Patreon
2023-09-11 17:13:05 +00:00
|
|
|
store' serial dest src = checkAdbInPath False $ do
|
2018-03-27 20:10:28 +00:00
|
|
|
let destdir = takeDirectory $ fromAndroidPath dest
|
2020-04-02 14:46:46 +00:00
|
|
|
void $ adbShell serial [Param "mkdir", Param "-p", File destdir]
|
2018-03-27 16:41:57 +00:00
|
|
|
showOutput -- make way for adb push output
|
adb send to final filename not tmp file
Avoids some problems with unusual character in exporttree filenames
that confuse adb shell commands.
In particular, with a filename that contains \351, adb push sends the file
to the correct filename in /sdcard. And running find on the android device
roundtrips the filename. But, running mv on that filename on the android
device fails with "bad <filename>: No such file or directory".
Interestingly, ls on android works, and rm fails.
adb push to the final name to avoids this problem. But what about
atomicity? Well, I tried an adb push and interrupted it part way through.
The file was present while the push was running, but was removed once the
push got interrupted. I also tried yanking the cable while adb push was
running, and the partially received file was also deleted then. That avoids
most problems.
An import that runs at the same time as an export will see the partially
sent file. But that is unlikely to be done, and if it did happen, it would
notice that the imported file had changed in the meantime and discard it.
Note that, since rm on the android device fails on these filenames,
exporting a tree where the file is deleted is going to fail to remove it. I
don't see what I can do about that, so long as android is using an rm that
has issues with filename encodings.
This was tested on a phone where find, ls, and rm all come from Toybox 0.8.6.
Sponsored-by: unqueued on Patreon
2023-09-11 17:13:05 +00:00
|
|
|
liftIO $ boolSystem "adb" $ mkAdbCommand serial
|
|
|
|
[Param "push", File src, File (fromAndroidPath dest)]
|
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
|
2021-08-16 20:22:00 +00:00
|
|
|
in retrieve' serial src (fromRawFilePath dest)
|
2018-03-27 20:10:28 +00:00
|
|
|
|
2020-05-15 16:51:09 +00:00
|
|
|
retrieve' :: AndroidSerial -> AndroidPath -> FilePath -> Annex ()
|
|
|
|
retrieve' serial src dest =
|
|
|
|
unlessM go $
|
|
|
|
giveup "adb pull failed"
|
|
|
|
where
|
|
|
|
go = checkAdbInPath False $ do
|
|
|
|
showOutput -- make way for adb pull output
|
|
|
|
liftIO $ boolSystem "adb" $ mkAdbCommand serial
|
|
|
|
[ Param "pull"
|
2022-01-03 17:53:34 +00:00
|
|
|
, Param "-a"
|
2020-05-15 16:51:09 +00:00
|
|
|
, File $ fromAndroidPath src
|
|
|
|
, File dest
|
|
|
|
]
|
2018-03-27 16:41:57 +00:00
|
|
|
|
|
|
|
remove :: AndroidSerial -> AndroidPath -> Remover
|
toward SafeDropProof expiry checking
Added Maybe POSIXTime to SafeDropProof, which gets set when the proof is
based on a LockedCopy. If there are several LockedCopies, it uses the
closest expiry time. That is not optimal, it may be that the proof
expires based on one LockedCopy but another one has not expired. But
that seems unlikely to really happen, and anyway the user can just
re-run a drop if it fails due to expiry.
Pass the SafeDropProof to removeKey, which is responsible for checking
it for expiry in situations where that could be a problem. Which really
only means in Remote.Git.
Made Remote.Git check expiry when dropping from a local remote.
Checking expiry when dropping from a P2P remote is not yet implemented.
P2P.Protocol.remove has SafeDropProof plumbed through to it for that
purpose.
Fixing the remaining 2 build warnings should complete this work.
Note that the use of a POSIXTime here means that if the clock gets set
forward while git-annex is in the middle of a drop, it may say that
dropping took too long. That seems ok. Less ok is that if the clock gets
turned back a sufficient amount (eg 5 minutes), proof expiry won't be
noticed. It might be better to use the Monotonic clock, but that doesn't
advance when a laptop is suspended, and while there is the linux
Boottime clock, that is not available on other systems. Perhaps a
combination of POSIXTime and the Monotonic clock could detect laptop
suspension and also detect clock being turned back?
There is a potential future flag day where
p2pDefaultLockContentRetentionDuration is not assumed, but is probed
using the P2P protocol, and peers that don't support it can no longer
produce a LockedCopy. Until that happens, when git-annex is
communicating with older peers there is a risk of data loss when
a ssh connection closes during LOCKCONTENT.
2024-07-04 16:23:46 +00:00
|
|
|
remove serial adir _proof k =
|
2020-05-14 18:08:09 +00:00
|
|
|
unlessM (remove' serial (androidLocation adir k)) $
|
|
|
|
giveup "adb failed"
|
2018-03-27 20:10:28 +00:00
|
|
|
|
|
|
|
remove' :: AndroidSerial -> AndroidPath -> Annex Bool
|
2020-04-02 14:46:46 +00:00
|
|
|
remove' serial aloc = adbShellBool serial
|
2018-03-27 20:10:28 +00:00
|
|
|
[Param "rm", Param "-f", File (fromAndroidPath aloc)]
|
2018-03-27 16:41:57 +00:00
|
|
|
|
remove "checking remotename" message
This fixes fsck of a remote that uses chunking displaying
(checking remotename) (checking remotename)" for every chunk.
Also, some remotes displayed the message, and others did not, with no
consistency. It was originally displayed only when accessing remotes
that were expensive or might involve a password prompt, I think, but
nothing in the API said when to do it so it became an inconsistent mess.
Originally I thought fsck should always display it. But it only displays
in fsck --from remote, so the user knows the remote is being accessed,
so there is no reason to tell them it's accessing it over and over.
It was also possible for git-annex move to sometimes display it twice,
due to checking if content is present twice. But, the user of move
specifies --from/--to, so it does not need to display when it's
accessing the remote, as the user expects it to access the remote.
git-annex get might display it, but only if the remote also supports
hasKeyCheap, which is really only local git remotes, which didn't
display it always; and in any case nothing displayed it before hasKeyCheap,
which is checked first, so I don't think this needs to display it ever.
mirror is like move. And that's all the main places it would have been
displayed.
This commit was sponsored by Jochen Bartl on Patreon.
2021-04-27 16:50:45 +00:00
|
|
|
checkKey :: AndroidSerial -> AndroidPath -> CheckPresent
|
|
|
|
checkKey serial adir k = checkKey' serial (androidLocation adir k)
|
2018-03-27 20:10:28 +00:00
|
|
|
|
remove "checking remotename" message
This fixes fsck of a remote that uses chunking displaying
(checking remotename) (checking remotename)" for every chunk.
Also, some remotes displayed the message, and others did not, with no
consistency. It was originally displayed only when accessing remotes
that were expensive or might involve a password prompt, I think, but
nothing in the API said when to do it so it became an inconsistent mess.
Originally I thought fsck should always display it. But it only displays
in fsck --from remote, so the user knows the remote is being accessed,
so there is no reason to tell them it's accessing it over and over.
It was also possible for git-annex move to sometimes display it twice,
due to checking if content is present twice. But, the user of move
specifies --from/--to, so it does not need to display when it's
accessing the remote, as the user expects it to access the remote.
git-annex get might display it, but only if the remote also supports
hasKeyCheap, which is really only local git remotes, which didn't
display it always; and in any case nothing displayed it before hasKeyCheap,
which is checked first, so I don't think this needs to display it ever.
mirror is like move. And that's all the main places it would have been
displayed.
This commit was sponsored by Jochen Bartl on Patreon.
2021-04-27 16:50:45 +00:00
|
|
|
checkKey' :: AndroidSerial -> AndroidPath -> Annex Bool
|
|
|
|
checkKey' serial aloc = do
|
2020-04-02 14:46:46 +00:00
|
|
|
out <- 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
|
|
|
|
2020-05-15 16:17:15 +00:00
|
|
|
storeExportM :: AndroidSerial -> AndroidPath -> FilePath -> Key -> ExportLocation -> MeterUpdate -> Annex ()
|
|
|
|
storeExportM serial adir src _k loc _p =
|
|
|
|
unlessM (store' serial dest src) $
|
|
|
|
giveup "adb failed"
|
2018-03-27 20:10:28 +00:00
|
|
|
where
|
|
|
|
dest = androidExportLocation adir loc
|
|
|
|
|
2022-05-09 16:25:04 +00:00
|
|
|
retrieveExportM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> FilePath -> MeterUpdate -> Annex Verification
|
2022-05-09 17:18:47 +00:00
|
|
|
retrieveExportM serial adir k loc dest _p =
|
|
|
|
verifyKeyContentIncrementally AlwaysVerify k $ \iv ->
|
|
|
|
tailVerify iv (toRawFilePath dest) $
|
|
|
|
retrieve' serial src dest
|
2018-03-27 20:10:28 +00:00
|
|
|
where
|
|
|
|
src = androidExportLocation adir loc
|
|
|
|
|
2020-05-15 18:11:59 +00:00
|
|
|
removeExportM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> Annex ()
|
|
|
|
removeExportM serial adir _k loc =
|
|
|
|
unlessM (remove' serial aloc) $
|
|
|
|
giveup "adb failed"
|
2018-03-27 20:10:28 +00:00
|
|
|
where
|
|
|
|
aloc = androidExportLocation adir loc
|
|
|
|
|
2020-05-15 18:32:45 +00:00
|
|
|
removeExportDirectoryM :: AndroidSerial -> AndroidPath -> ExportDirectory -> Annex ()
|
|
|
|
removeExportDirectoryM serial abase dir =
|
|
|
|
unlessM go $
|
|
|
|
giveup "adb failed"
|
2018-03-27 20:10:28 +00:00
|
|
|
where
|
2020-05-15 18:32:45 +00:00
|
|
|
go = adbShellBool serial [Param "rm", Param "-rf", File (fromAndroidPath adir)]
|
2018-03-27 20:10:28 +00:00
|
|
|
adir = androidExportLocation abase (mkExportLocation (fromExportDirectory dir))
|
|
|
|
|
remove "checking remotename" message
This fixes fsck of a remote that uses chunking displaying
(checking remotename) (checking remotename)" for every chunk.
Also, some remotes displayed the message, and others did not, with no
consistency. It was originally displayed only when accessing remotes
that were expensive or might involve a password prompt, I think, but
nothing in the API said when to do it so it became an inconsistent mess.
Originally I thought fsck should always display it. But it only displays
in fsck --from remote, so the user knows the remote is being accessed,
so there is no reason to tell them it's accessing it over and over.
It was also possible for git-annex move to sometimes display it twice,
due to checking if content is present twice. But, the user of move
specifies --from/--to, so it does not need to display when it's
accessing the remote, as the user expects it to access the remote.
git-annex get might display it, but only if the remote also supports
hasKeyCheap, which is really only local git remotes, which didn't
display it always; and in any case nothing displayed it before hasKeyCheap,
which is checked first, so I don't think this needs to display it ever.
mirror is like move. And that's all the main places it would have been
displayed.
This commit was sponsored by Jochen Bartl on Patreon.
2021-04-27 16:50:45 +00:00
|
|
|
checkPresentExportM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> Annex Bool
|
|
|
|
checkPresentExportM serial adir _k loc = checkKey' serial aloc
|
2018-03-27 20:10:28 +00:00
|
|
|
where
|
|
|
|
aloc = androidExportLocation adir loc
|
|
|
|
|
2020-05-15 19:05:52 +00:00
|
|
|
renameExportM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> ExportLocation -> Annex (Maybe ())
|
|
|
|
renameExportM serial adir _k old new = do
|
|
|
|
unlessM (adbShellBool serial ps) $
|
|
|
|
giveup "adb failed"
|
|
|
|
return (Just ())
|
2018-03-27 20:10:28 +00:00
|
|
|
where
|
|
|
|
oldloc = fromAndroidPath $ androidExportLocation adir old
|
|
|
|
newloc = fromAndroidPath $ androidExportLocation adir new
|
2020-04-02 14:46:46 +00:00
|
|
|
ps =
|
|
|
|
[ Param "mv"
|
|
|
|
, Param "-f"
|
|
|
|
, File oldloc
|
|
|
|
, File newloc
|
|
|
|
]
|
2018-03-27 20:10:28 +00:00
|
|
|
|
2022-01-11 01:15:30 +00:00
|
|
|
listImportableContentsM :: AndroidSerial -> AndroidPath -> ParsedRemoteConfig -> Annex (Maybe (ImportableContentsChunkable Annex (ContentIdentifier, ByteSize)))
|
|
|
|
listImportableContentsM serial adir c = adbfind >>= \case
|
2021-10-06 21:05:32 +00:00
|
|
|
Just ls -> return $ Just $ ImportableContentsComplete $
|
|
|
|
ImportableContents (mapMaybe mk ls) []
|
2020-12-22 18:20:11 +00:00
|
|
|
Nothing -> giveup "adb find failed"
|
|
|
|
where
|
2022-07-13 22:00:47 +00:00
|
|
|
adbfind = adbShell' serial findparams
|
|
|
|
(\s -> if oldandroid
|
|
|
|
then s ++ ignorefinderrorsh
|
|
|
|
else concat
|
|
|
|
[ "set -o pipefail; "
|
|
|
|
, s
|
|
|
|
, "| xargs -0 stat -c " ++ shellEscape statformat ++ " --"
|
|
|
|
, ignorefinderrorsh
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
findparams =
|
2019-04-09 21:52:41 +00:00
|
|
|
[ Param "find"
|
|
|
|
-- trailing slash is needed, or android's find command
|
|
|
|
-- won't recurse into the directory
|
|
|
|
, File $ fromAndroidPath adir ++ "/"
|
|
|
|
, Param "-type", Param "f"
|
2022-07-13 22:00:47 +00:00
|
|
|
] ++ if oldandroid
|
|
|
|
then
|
|
|
|
[ Param "-exec"
|
|
|
|
, Param "stat"
|
|
|
|
, Param "-c"
|
|
|
|
, Param statformat
|
|
|
|
, Param "{}"
|
|
|
|
, Param ";"
|
|
|
|
]
|
|
|
|
else
|
|
|
|
[ Param "-printf"
|
|
|
|
, Param "%p\\0"
|
|
|
|
]
|
2022-01-11 01:15:30 +00:00
|
|
|
|
|
|
|
ignorefinderror = fromMaybe False (getRemoteConfigValue ignorefinderrorField c)
|
2022-07-13 22:00:47 +00:00
|
|
|
oldandroid = fromMaybe False (getRemoteConfigValue oldandroidField c)
|
2019-04-09 21:52:41 +00:00
|
|
|
|
|
|
|
statformat = adbStatFormat ++ "\t%n"
|
|
|
|
|
2022-07-13 22:00:47 +00:00
|
|
|
ignorefinderrorsh = if ignorefinderror then " || true" else ""
|
|
|
|
|
2019-04-09 21:52:41 +00:00
|
|
|
mk ('S':'T':'\t':l) =
|
|
|
|
let (stat, fn) = separate (== '\t') l
|
|
|
|
sz = fromMaybe 0 (readish (takeWhile (/= ' ') stat))
|
2021-08-11 00:45:02 +00:00
|
|
|
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
|
2024-04-06 13:50:58 +00:00
|
|
|
-- connection is reasonably fast, it's probably as good as
|
2019-04-09 21:52:41 +00:00
|
|
|
-- git's handling of similar situations with files being modified while
|
|
|
|
-- it's updating the working tree for a merge.
|
change retrieveExportWithContentIdentifier to take a list of ContentIdentifier
This partly fixes an issue where there are duplicate files in the
special remote, and the first file gets swapped with another duplicate,
or deleted. The swap case is fixed by this, the deleted case will need
other changes.
This makes retrieveExportWithContentIdentifier take a list of allowed
ContentIdentifier, same as storeExportWithContentIdentifier,
removeExportWithContentIdentifier, and
checkPresentExportWithContentIdentifier.
Of the special remotes that support importtree, borg is a special case
and does not use content identifiers, S3 I assume can't get mixed up
like this, directory certainly has the problem, and adb also appears to
have had the problem.
Sponsored-by: Graham Spencer on Patreon
2022-09-20 17:15:31 +00:00
|
|
|
retrieveExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> ExportLocation -> [ContentIdentifier] -> FilePath -> Either Key (Annex Key) -> MeterUpdate -> Annex (Key, Verification)
|
|
|
|
retrieveExportWithContentIdentifierM serial adir loc cids dest gk _p = do
|
2022-05-09 19:38:21 +00:00
|
|
|
case gk of
|
|
|
|
Right mkkey -> do
|
|
|
|
go
|
|
|
|
k <- mkkey
|
|
|
|
return (k, UnVerified)
|
|
|
|
Left k -> do
|
|
|
|
v <- verifyKeyContentIncrementally DefaultVerify k
|
|
|
|
(\iv -> tailVerify iv (toRawFilePath dest) go)
|
|
|
|
return (k, v)
|
2019-04-09 21:52:41 +00:00
|
|
|
where
|
2022-05-09 19:38:21 +00:00
|
|
|
go = do
|
|
|
|
retrieve' serial src dest
|
change retrieveExportWithContentIdentifier to take a list of ContentIdentifier
This partly fixes an issue where there are duplicate files in the
special remote, and the first file gets swapped with another duplicate,
or deleted. The swap case is fixed by this, the deleted case will need
other changes.
This makes retrieveExportWithContentIdentifier take a list of allowed
ContentIdentifier, same as storeExportWithContentIdentifier,
removeExportWithContentIdentifier, and
checkPresentExportWithContentIdentifier.
Of the special remotes that support importtree, borg is a special case
and does not use content identifiers, S3 I assume can't get mixed up
like this, directory certainly has the problem, and adb also appears to
have had the problem.
Sponsored-by: Graham Spencer on Patreon
2022-09-20 17:15:31 +00:00
|
|
|
getExportContentIdentifier serial adir loc >>= \case
|
|
|
|
Right (Just currcid)
|
|
|
|
| any (currcid ==) cids -> return ()
|
|
|
|
_ -> giveup "the file on the android device has changed"
|
2019-04-09 21:52:41 +00:00
|
|
|
src = androidExportLocation adir loc
|
|
|
|
|
2020-05-15 16:17:15 +00:00
|
|
|
storeExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> FilePath -> Key -> ExportLocation -> [ContentIdentifier] -> MeterUpdate -> Annex ContentIdentifier
|
2019-08-13 16:05:00 +00:00
|
|
|
storeExportWithContentIdentifierM serial adir src _k loc overwritablecids _p =
|
2019-04-09 23:11:38 +00:00
|
|
|
ifM checkcanoverwrite
|
adb send to final filename not tmp file
Avoids some problems with unusual character in exporttree filenames
that confuse adb shell commands.
In particular, with a filename that contains \351, adb push sends the file
to the correct filename in /sdcard. And running find on the android device
roundtrips the filename. But, running mv on that filename on the android
device fails with "bad <filename>: No such file or directory".
Interestingly, ls on android works, and rm fails.
adb push to the final name to avoids this problem. But what about
atomicity? Well, I tried an adb push and interrupted it part way through.
The file was present while the push was running, but was removed once the
push got interrupted. I also tried yanking the cable while adb push was
running, and the partially received file was also deleted then. That avoids
most problems.
An import that runs at the same time as an export will see the partially
sent file. But that is unlikely to be done, and if it did happen, it would
notice that the imported file had changed in the meantime and discard it.
Note that, since rm on the android device fails on these filenames,
exporting a tree where the file is deleted is going to fail to remove it. I
don't see what I can do about that, so long as android is using an rm that
has issues with filename encodings.
This was tested on a phone where find, ls, and rm all come from Toybox 0.8.6.
Sponsored-by: unqueued on Patreon
2023-09-11 17:13:05 +00:00
|
|
|
( ifM (store' serial dest src)
|
2020-05-15 16:17:15 +00:00
|
|
|
( getExportContentIdentifier serial adir loc >>= \case
|
|
|
|
Right (Just cid) -> return cid
|
|
|
|
Right Nothing -> giveup "adb failed to store file"
|
|
|
|
Left _ -> giveup "unable to get content identifier for file stored by adb"
|
|
|
|
, giveup "adb failed to store file"
|
2019-04-09 23:11:38 +00:00
|
|
|
)
|
2020-05-15 16:17:15 +00:00
|
|
|
, giveup "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
|
2020-04-02 14:46:46 +00:00
|
|
|
checkcanoverwrite =
|
2019-04-09 21:52:41 +00:00
|
|
|
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
|
|
|
|
2020-05-15 18:11:59 +00:00
|
|
|
removeExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> [ContentIdentifier] -> Annex ()
|
|
|
|
removeExportWithContentIdentifierM serial adir k loc removeablecids =
|
2020-04-02 14:46:46 +00:00
|
|
|
getExportContentIdentifier serial adir loc >>= \case
|
2020-05-15 18:11:59 +00:00
|
|
|
Right Nothing -> return ()
|
|
|
|
Right (Just cid)
|
|
|
|
| cid `elem` removeablecids ->
|
|
|
|
removeExportM serial adir k loc
|
|
|
|
| otherwise -> giveup "file on Android device is modified, cannot remove"
|
|
|
|
Left _ -> giveup "unable to access Android device"
|
2019-04-09 21:52:41 +00:00
|
|
|
|
|
|
|
checkPresentExportWithContentIdentifierM :: AndroidSerial -> AndroidPath -> Key -> ExportLocation -> [ContentIdentifier] -> Annex Bool
|
|
|
|
checkPresentExportWithContentIdentifierM serial adir _k loc knowncids =
|
2020-04-02 14:46:46 +00:00
|
|
|
getExportContentIdentifier serial adir loc >>= \case
|
2019-04-09 21:52:41 +00:00
|
|
|
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.
|
2020-04-02 14:46:46 +00:00
|
|
|
enumerateAdbConnected :: Annex [AndroidSerial]
|
|
|
|
enumerateAdbConnected = checkAdbInPath [] $ liftIO $
|
2018-03-27 16:41:57 +00:00
|
|
|
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.
|
2020-04-02 14:46:46 +00:00
|
|
|
adbShell :: AndroidSerial -> [CommandParam] -> Annex (Maybe [String])
|
2022-01-31 17:19:09 +00:00
|
|
|
adbShell serial cmd = adbShell' serial cmd id
|
2022-01-11 01:15:30 +00:00
|
|
|
|
2022-01-31 17:19:09 +00:00
|
|
|
adbShell' :: AndroidSerial -> [CommandParam] -> (String -> String) -> Annex (Maybe [String])
|
|
|
|
adbShell' serial cmd f = adbShellRaw serial $
|
|
|
|
f (unwords $ map shellEscape (toCommand cmd))
|
2018-03-27 16:41:57 +00:00
|
|
|
|
2020-04-02 14:46:46 +00:00
|
|
|
adbShellBool :: AndroidSerial -> [CommandParam] -> Annex 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.
|
2020-04-02 14:46:46 +00:00
|
|
|
adbShellRaw :: AndroidSerial -> String -> Annex (Maybe [String])
|
|
|
|
adbShellRaw serial cmd = checkAdbInPath Nothing $ liftIO $ catchMaybeIO $
|
2019-04-09 21:52:41 +00:00
|
|
|
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')
|
|
|
|
|
2020-04-02 14:46:46 +00:00
|
|
|
checkAdbInPath :: a -> Annex a -> Annex a
|
|
|
|
checkAdbInPath d a = ifM (isJust <$> liftIO (searchPath "adb"))
|
|
|
|
( a
|
|
|
|
, do
|
|
|
|
warning "adb command not found in PATH. Install it to use this remote."
|
|
|
|
return d
|
|
|
|
)
|
|
|
|
|
2018-03-27 16:41:57 +00:00
|
|
|
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
|
2020-04-02 14:46:46 +00:00
|
|
|
getExportContentIdentifier :: AndroidSerial -> AndroidPath -> ExportLocation -> Annex (Either ExitCode (Maybe ContentIdentifier))
|
|
|
|
getExportContentIdentifier serial adir loc = do
|
2019-04-09 21:52:41 +00:00
|
|
|
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 $
|
2021-08-11 00:45:02 +00:00
|
|
|
ContentIdentifier (encodeBS stat)
|
2019-04-09 21:52:41 +00:00
|
|
|
_ -> 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"
|