feat: add gadb pull folder/file

This commit is contained in:
lilong.129
2025-08-06 11:42:22 +08:00
parent 758ece2998
commit 08849850f9
6 changed files with 80 additions and 2 deletions

View File

@@ -610,6 +610,55 @@ func (d *Device) Pull(remotePath string, dest io.Writer) (err error) {
return
}
func (d *Device) PullFolder(remotePath string, localPath string) (err error) {
// Check if remote path exists and is a directory
fileInfos, err := d.List(remotePath)
if err != nil {
return fmt.Errorf("failed to list remote directory: %w", err)
}
// Create local directory if it doesn't exist
if err = os.MkdirAll(localPath, 0o755); err != nil {
return fmt.Errorf("failed to create local directory: %w", err)
}
// Pull each file/directory recursively
for _, fileInfo := range fileInfos {
remoteItemPath := remotePath + "/" + fileInfo.Name
localItemPath := localPath + "/" + fileInfo.Name
if fileInfo.IsDir() {
// Recursively pull subdirectory
if err = d.PullFolder(remoteItemPath, localItemPath); err != nil {
return fmt.Errorf("failed to pull subdirectory %s: %w", remoteItemPath, err)
}
} else {
// Pull file
if err = d.PullFile(remoteItemPath, localItemPath); err != nil {
return fmt.Errorf("failed to pull file %s: %w", remoteItemPath, err)
}
}
}
return nil
}
func (d *Device) PullFile(remotePath string, localPath string) (err error) {
// Create local file
localFile, err := os.Create(localPath)
if err != nil {
return fmt.Errorf("failed to create local file: %w", err)
}
defer localFile.Close()
// Use existing Pull method to pull file content
if err = d.Pull(remotePath, localFile); err != nil {
return fmt.Errorf("failed to pull file content: %w", err)
}
return nil
}
func (d *Device) installViaABBExec(apk io.ReadSeeker, args ...string) (raw []byte, err error) {
var (
tp transport

View File

@@ -296,6 +296,17 @@ func TestDevice_Pull(t *testing.T) {
}
}
func TestDevice_PullFolder(t *testing.T) {
setupDevices(t)
for _, dev := range devices {
err := dev.PullFolder("/storage/emulated/0/Download/", "/tmp/test/")
if err != nil {
t.Fatal(err)
}
}
}
func TestDevice_ScreenRecord(t *testing.T) {
setupDevices(t)