From 78396583840f349194e19d04033bbeabbd4b6111 Mon Sep 17 00:00:00 2001 From: zyj <18107291228@163.com> Date: Wed, 5 Nov 2025 18:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adb/sync.go | 67 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/adb/sync.go b/adb/sync.go index 1613606..eab4855 100644 --- a/adb/sync.go +++ b/adb/sync.go @@ -39,23 +39,7 @@ func (s *Sync) SyncPushFile(localPath, remotePath string, mode int, debug bool) if debug { fmt.Printf("[DEBUG] local filesize=%d\n", fi.Size()) } - - // 打开 sync - if err := WriteAdbCmd(s.Conn, "sync:"); err != nil { - return 0, err - } - tok, err := readN(s.Conn, 4, 10*time.Second) - if err != nil { - return 0, err - } - if string(tok) != "OKAY" { - _, msg, _ := ReadResponse(s.Conn, true) - if len(msg) > 0 { - return 0, fmt.Errorf("sync open failed: %s", string(msg)) - } - return 0, fmt.Errorf("sync open failed: %q", string(tok)) - } - + s.StartSync() // 构造 SEND payload modeStr := strconv.Itoa(syscall.S_IFREG | mode) sendPayload := []byte(remotePath + "," + modeStr) @@ -102,7 +86,8 @@ func (s *Sync) SyncPushFile(localPath, remotePath string, mode int, debug bool) // 发送 DONE done := make([]byte, 8) copy(done[:4], []byte("DONE")) - binary.LittleEndian.PutUint32(done[4:], uint32(time.Now().Unix())) + mtime := uint32(fi.ModTime().Unix()) + binary.LittleEndian.PutUint32(done[4:], mtime) if _, err := s.Conn.Write(done); err != nil { return total, err } @@ -111,7 +96,7 @@ func (s *Sync) SyncPushFile(localPath, remotePath string, mode int, debug bool) } // 读取最终响应 - resp, msg, err := ReadResponse(s.Conn, true) + resp, msg, err := ReadSyncStatus(s.Conn) if err != nil { return total, err } @@ -123,3 +108,47 @@ func (s *Sync) SyncPushFile(localPath, remotePath string, mode int, debug bool) } return total, nil } + +func (s *Sync) StartSync() error { + if err := WriteAdbCmd(s.Conn, "sync:"); err != nil { + return err + } + tok, err := readN(s.Conn, 4, 10*time.Second) + if err != nil { + return err + } + if string(tok) != "OKAY" { + _, msg, _ := ReadSyncStatus(s.Conn) + if len(msg) > 0 { + return fmt.Errorf("sync open failed: %s", string(msg)) + } + return fmt.Errorf("sync open failed: %q", string(tok)) + } + return nil +} + +func ReadSyncStatus(r io.Reader) (string, string, error) { + hdr := make([]byte, 4) + if _, err := io.ReadFull(r, hdr); err != nil { + return "", "", err + } + status := string(hdr) + switch status { + case "OKAY": + return "OKAY", "", nil + case "FAIL": + lenBuf := make([]byte, 4) + if _, err := io.ReadFull(r, lenBuf); err != nil { + return "FAIL", "", err + } + l := binary.LittleEndian.Uint32(lenBuf) + msg := make([]byte, l) + if _, err := io.ReadFull(r, msg); err != nil { + return "FAIL", "", err + } + return "FAIL", string(msg), nil + default: + // 非预期状态,直接返回原始字符串,便于上层报错 + return status, "", nil + } +}