Ejemplo n.º 1
0
 /// <summary>
 /// 尝试读取源文件并写入本地文件中。
 /// </summary>
 private string CatToFile(Device device, string sourceFile, string local, CatToFileReceiver receiver = null)
 {
     try
     {
         this.UpgradeFilePermission(device, sourceFile);
         receiver = receiver ?? new CatToFileReceiver(local);
         this.ExecuteRemoteAutoCommand(string.Format("cat {0}", sourceFile), device, receiver, 64 * 1024);
         return(string.Empty);
     }
     catch (Exception ex)
     {
         System.IO.File.Delete(local);
         string e = string.Format("try cat to file[{0}] failed", sourceFile);
         return(e);
     }
     finally
     {
         if (receiver != null)
         {
             receiver.Dispose();
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 执行文件拷贝到本地操作
        /// </summary>
        private void DoPullFile(Device device, string sourceFile, string local, PullFileReceiver receiver = null,
                                int bufferSize = 64 * 1024,
                                int maxtimeout = 9000, bool trycat = true)
        {
            if (device == null || String.IsNullOrWhiteSpace(sourceFile) || String.IsNullOrWhiteSpace(local))
            {
                return;
            }

            #region /data/data/文件处理

            //只处理/data/data/文件夹下文件
            bool   IsNeedMoveFile = sourceFile.Replace('\\', '/').StartsWith("/data/data/") && sourceFile.EndsWith(".db");
            var    dr             = new DefaultReceiver();
            string rmCmd          = "";

            if (IsNeedMoveFile)
            {
                string tempSrc = string.Format("/data/local/tmp/{0}", FileHelper.GetFileName(local));
                string cpCmd   = string.Format("cp {0} /data/local/tmp", sourceFile);

                this.ExecuteRemoteAutoCommandNoException(cpCmd, device, dr); //拷贝/data/data/ 到/data/local/tmp

                if (this.IsExistsFileOrPath(device, tempSrc))                //判断是否拷贝成功
                {
                    this.UpgradeFilePermission(device, tempSrc);             //提权
                    sourceFile = tempSrc;
                    rmCmd      = string.Format("rm {0}", tempSrc);
                }
            }

            #endregion

            receiver = receiver ?? new PullFileReceiver(local);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket.Connect(this.SocketAddress);
                socket.ReceiveTimeout    = maxtimeout;
                socket.SendTimeout       = maxtimeout;
                socket.Blocking          = true;
                socket.ReceiveBufferSize = bufferSize;
                this.SetDevice(socket, device);
                this.SendAsyncReqest(socket);
                var request = AdbSocketHelper.FormPullFileRequest(sourceFile);
                AdbSocketHelper.Write(socket, request);
                receiver.DoReceive(socket);
            }
            catch (ADBConnectionException cex)
            {
                var mes = string.Format("pull file[{0}] failed:{1}", sourceFile, cex.Message);

                if (trycat)
                {
                    var rec = new CatToFileReceiver(local);
                    rec.OnReceiveData = receiver.OnReceiveData;
                    var rt = this.CatToFile(device, sourceFile, local, rec);
                    if (rt != string.Empty)
                    {
                        throw new Exception(rt);
                    }
                }
                else
                {
                    throw new ApplicationException(mes, cex);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format("pull file[{0}] failed", sourceFile), ex);
            }
            finally
            {
                if (IsNeedMoveFile && !String.IsNullOrWhiteSpace(rmCmd))
                {//删除temp文件
                    try
                    {
                        this.ExecuteRemoteAutoCommandNoException(rmCmd, device, dr);
                    }
                    catch { }
                }

                this.DisposeSocket(socket);
            }
        }