public SyncResult DoPush(IEnumerable <FileSystemInfo> files, string remotePath, ISyncProgressMonitor monitor) { if (monitor == null) { throw new ArgumentNullException("monitor", "Monitor cannot be null"); } // check if we're canceled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } foreach (FileSystemInfo f in files) { // check if we're canceled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } // append the name of the directory/file to the remote path string dest = LinuxPath.Combine(remotePath, f.Name); if (f.Exists) { if (f.IsDirectory()) { DirectoryInfo fsiDir = f as DirectoryInfo; monitor.StartSubTask(f.FullName, dest); SyncResult result = this.DoPush(fsiDir.GetFileSystemInfos(), dest, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } monitor.Advance(1); } else if (f.IsFile()) { monitor.StartSubTask(f.FullName, dest); SyncResult result = this.DoPushFile(f.FullName, dest, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } } } } return(new SyncResult(ErrorCodeHelper.RESULT_OK)); }
/// <summary> /// /// </summary> /// <param name="entries"></param> /// <param name="localPath"></param> /// <param name="fls"></param> /// <param name="monitor"></param> /// <returns></returns> /// <exception cref="System.IO.IOException">Throws if unable to create a file or folder</exception> /// <exception cref="System.ArgumentNullException">Throws if the ISyncProgressMonitor is null</exception> private SyncResult DoPull(IEnumerable <FileEntry> entries, string localPath, FileListingService fileListingService, ISyncProgressMonitor monitor) { if (monitor == null) { throw new ArgumentNullException("monitor", "Monitor cannot be null"); } // check if we're cancelled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } // check if we need to create the local directory DirectoryInfo localDir = new DirectoryInfo(localPath); if (!localDir.Exists) { localDir.Create( ); } foreach (FileEntry e in entries) { // check if we're canceled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } // the destination item (folder or file) String dest = Path.Combine(localPath, e.Name); // get type (we only pull directory and files for now) FileListingService.FileTypes type = e.Type; if (type == FileListingService.FileTypes.Directory) { monitor.StartSubTask(e.FullPath, dest); // then recursively call the content. Since we did a ls command // to get the number of files, we can use the cache FileEntry[] children = fileListingService.GetChildren(e, true, null); SyncResult result = DoPull(children, dest, fileListingService, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } monitor.Advance(1); } else if (type == FileListingService.FileTypes.File) { monitor.StartSubTask(e.FullPath, dest); SyncResult result = DoPullFile(e.FullPath, dest, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } } else if (type == FileListingService.FileTypes.Link) { monitor.StartSubTask(e.FullPath, dest); SyncResult result = DoPullFile(e.FullResolvedPath, dest, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } } else { Log.d("ddms-sync", String.Format("unknown type to transfer: {0}", type)); } } return(new SyncResult(ErrorCodeHelper.RESULT_OK)); }
/// <summary> /// Pulls a remote file /// </summary> /// <param name="remotePath">the remote file (length max is 1024)</param> /// <param name="localPath">the local destination</param> /// <param name="monitor">the monitor. The monitor must be started already.</param> /// <returns>a SyncResult object with a code and an optional message.</returns> /// <exception cref="ArgumentNullException">Throws if monitor is null</exception> private SyncResult DoPullFile(string remotePath, string localPath, ISyncProgressMonitor monitor) { if (monitor == null) { throw new ArgumentNullException("monitor", "Monitor cannot be null"); } byte[] msg = null; byte[] pullResult = new byte[8]; int timeOut = DdmPreferences.Timeout; try { byte[] remotePathContent = remotePath.GetBytes(AdbHelper.DEFAULT_ENCODING); if (remotePathContent.Length > REMOTE_PATH_MAX_LENGTH) { return(new SyncResult(ErrorCodeHelper.RESULT_REMOTE_PATH_LENGTH)); } // create the full request message msg = CreateFileRequest(RECV.GetBytes( ), remotePathContent); // and send it. AdbHelper.Instance.Write(Channel, msg, -1, timeOut); // read the result, in a byte array containing 2 ints // (id, size) AdbHelper.Instance.Read(Channel, pullResult, -1, timeOut); // check we have the proper data back if (CheckResult(pullResult, DATA.GetBytes( )) == false && CheckResult(pullResult, DONE.GetBytes( )) == false) { return(new SyncResult(ErrorCodeHelper.RESULT_CONNECTION_ERROR)); } } catch (EncoderFallbackException e) { Log.e(TAG, e); return(new SyncResult(ErrorCodeHelper.RESULT_REMOTE_PATH_ENCODING, e)); } catch (IOException e) { Log.e(TAG, e); return(new SyncResult(ErrorCodeHelper.RESULT_CONNECTION_ERROR, e)); } // access the destination file FileInfo f = new FileInfo(localPath); // create the stream to write in the file. We use a new try/catch block to differentiate // between file and network io exceptions. FileStream fos = null; try { fos = new FileStream(f.FullName, System.IO.FileMode.Create, FileAccess.Write); } catch (FileNotFoundException e) { return(new SyncResult(ErrorCodeHelper.RESULT_FILE_WRITE_ERROR, e)); } // the buffer to read the data byte[] data = new byte[SYNC_DATA_MAX]; using ( fos ) { // loop to get data until we're done. while (true) { // check if we're canceled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } // if we're done, we stop the loop if (CheckResult(pullResult, DONE.GetBytes( ))) { break; } if (CheckResult(pullResult, DATA.GetBytes( )) == false) { // hmm there's an error return(new SyncResult(ErrorCodeHelper.RESULT_CONNECTION_ERROR)); } int length = pullResult.Swap32bitFromArray(4); if (length > SYNC_DATA_MAX) { // buffer overrun! // error and exit return(new SyncResult(ErrorCodeHelper.RESULT_BUFFER_OVERRUN)); } try { // now read the length we received AdbHelper.Instance.Read(Channel, data, length, timeOut); // get the header for the next packet. AdbHelper.Instance.Read(Channel, pullResult, -1, timeOut); } catch (IOException e) { Log.e(TAG, e); return(new SyncResult(ErrorCodeHelper.RESULT_CONNECTION_ERROR, e)); } // write the content in the file try { fos.Write(data, 0, length); } catch (IOException e) { return(new SyncResult(ErrorCodeHelper.RESULT_FILE_WRITE_ERROR, e)); } monitor.Advance(length); } try { fos.Flush( ); } catch (IOException e) { Log.e(TAG, e); return(new SyncResult(ErrorCodeHelper.RESULT_FILE_WRITE_ERROR, e)); } } return(new SyncResult(ErrorCodeHelper.RESULT_OK)); }
/// <summary> /// Push a single file /// </summary> /// <param name="local">the local file to push</param> /// <param name="remotePath">the remote file (length max is 1024)</param> /// <param name="monitor">the monitor. The monitor must be started already.</param> /// <returns> /// a SyncResult object with a code and an optional message. /// </returns> /// <exception cref="System.ArgumentNullException">monitor;Monitor cannot be null</exception> /// <exception cref="ArgumentNullException">Throws if monitor is null</exception> private SyncResult DoPushFile(string local, string remotePath, ISyncProgressMonitor monitor) { if (monitor == null) { throw new ArgumentNullException("monitor", "Monitor cannot be null"); } FileStream fs = null; byte[] msg; int timeOut = DdmPreferences.Timeout; Log.d(TAG, "Remote File: {0}", remotePath); try { byte[] remotePathContent = remotePath.GetBytes(AdbHelper.DEFAULT_ENCODING); if (remotePathContent.Length > REMOTE_PATH_MAX_LENGTH) { return(new SyncResult(ErrorCodeHelper.RESULT_REMOTE_PATH_LENGTH)); } // this shouldn't happen but still... if (!File.Exists(local)) { return(new SyncResult(ErrorCodeHelper.RESULT_NO_LOCAL_FILE)); } // create the stream to read the file fs = new FileStream(local, System.IO.FileMode.Open, FileAccess.Read); // create the header for the action msg = CreateSendFileRequest(SEND.GetBytes( ), remotePathContent, (FileMode)0644); } catch (EncoderFallbackException e) { return(new SyncResult(ErrorCodeHelper.RESULT_REMOTE_PATH_ENCODING, e)); } catch (FileNotFoundException e) { return(new SyncResult(ErrorCodeHelper.RESULT_FILE_READ_ERROR, e)); } // and send it. We use a custom try/catch block to make the difference between // file and network IO exceptions. try { AdbHelper.Instance.Write(Channel, msg, -1, timeOut); } catch (IOException e) { return(new SyncResult(ErrorCodeHelper.RESULT_CONNECTION_ERROR, e)); } // create the buffer used to read. // we read max SYNC_DATA_MAX, but we need 2 4 bytes at the beginning. if (DataBuffer == null) { DataBuffer = new byte[SYNC_DATA_MAX + 8]; } byte[] bDATA = DATA.GetBytes( ); Array.Copy(bDATA, 0, DataBuffer, 0, bDATA.Length); // look while there is something to read while (true) { // check if we're canceled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } // read up to SYNC_DATA_MAX int readCount = 0; try { readCount = fs.Read(DataBuffer, 8, SYNC_DATA_MAX); } catch (IOException e) { return(new SyncResult(ErrorCodeHelper.RESULT_FILE_READ_ERROR, e)); } if (readCount == 0) { // we reached the end of the file break; } // now send the data to the device // first write the amount read readCount.Swap32bitsToArray(DataBuffer, 4); // now write it try { AdbHelper.Instance.Write(Channel, DataBuffer, readCount + 8, timeOut); } catch (IOException e) { return(new SyncResult(ErrorCodeHelper.RESULT_CONNECTION_ERROR, e)); } // and advance the monitor monitor.Advance(readCount); } // close the local file try { fs.Close( ); } catch (IOException e) { return(new SyncResult(ErrorCodeHelper.RESULT_FILE_READ_ERROR, e)); } try { // create the DONE message long time = DateTime.Now.CurrentTimeMillis( ) / 1000; msg = CreateRequest(DONE, (int)time); // and send it. AdbHelper.Instance.Write(Channel, msg, -1, timeOut); // read the result, in a byte array containing 2 ints // (id, size) byte[] result = new byte[8]; AdbHelper.Instance.Read(Channel, result, -1 /* full length */, timeOut); if (!CheckResult(result, OKAY.GetBytes( ))) { if (CheckResult(result, FAIL.GetBytes( ))) { // read some error message... int len = result.Swap32bitFromArray(4); AdbHelper.Instance.Read(Channel, DataBuffer, len, timeOut); // output the result? String message = DataBuffer.GetString(0, len); Log.e("ddms", "transfer error: " + message); return(new SyncResult(ErrorCodeHelper.RESULT_UNKNOWN_ERROR, message)); } return(new SyncResult(ErrorCodeHelper.RESULT_UNKNOWN_ERROR)); } } catch (IOException e) { return(new SyncResult(ErrorCodeHelper.RESULT_CONNECTION_ERROR, e)); } // files pushed have no permissions... // lets check if we can get to the file... if (this.Device.FileSystem.Exists(remotePath)) { this.Device.FileSystem.Chmod(remotePath, "0666"); } return(new SyncResult(ErrorCodeHelper.RESULT_OK)); }
/// <summary> /// Push a single file /// </summary> /// <param name="local">the local file to push</param> /// <param name="remotePath">the remote file (length max is 1024)</param> /// <param name="monitor">the monitor. The monitor must be started already.</param> /// <returns> /// a SyncResult object with a code and an optional message. /// </returns> /// <exception cref="System.ArgumentNullException">monitor;Monitor cannot be null</exception> /// <exception cref="ArgumentNullException">Throws if monitor is null</exception> private SyncResult DoPushFile( string local, string remotePath, ISyncProgressMonitor monitor ) { if ( monitor == null ) { throw new ArgumentNullException ( "monitor", "Monitor cannot be null" ); } FileStream fs = null; byte[] msg; int timeOut = DdmPreferences.Timeout; Console.WriteLine ( "Remote File: {0}", remotePath ); try { byte[] remotePathContent = remotePath.GetBytes ( AdbHelper.DEFAULT_ENCODING ); if ( remotePathContent.Length > REMOTE_PATH_MAX_LENGTH ) { return new SyncResult ( ErrorCodeHelper.RESULT_REMOTE_PATH_LENGTH ); } // this shouldn't happen but still... if ( !File.Exists ( local ) ) { return new SyncResult ( ErrorCodeHelper.RESULT_NO_LOCAL_FILE ); } // create the stream to read the file fs = new FileStream ( local, System.IO.FileMode.Open, FileAccess.Read ); // create the header for the action msg = CreateSendFileRequest ( SEND.GetBytes ( ), remotePathContent, (FileMode)0644 ); } catch ( EncoderFallbackException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_REMOTE_PATH_ENCODING, e ); } catch ( FileNotFoundException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_FILE_READ_ERROR, e ); } // and send it. We use a custom try/catch block to make the difference between // file and network IO exceptions. try { AdbHelper.Instance.Write ( Channel, msg, -1, timeOut ); } catch ( IOException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_CONNECTION_ERROR, e ); } // create the buffer used to read. // we read max SYNC_DATA_MAX, but we need 2 4 bytes at the beginning. if ( DataBuffer == null ) { DataBuffer = new byte[SYNC_DATA_MAX + 8]; } byte[] bDATA = DATA.GetBytes ( ); Array.Copy ( bDATA, 0, DataBuffer, 0, bDATA.Length ); // look while there is something to read while ( true ) { // check if we're canceled if ( monitor.IsCanceled ) { return new SyncResult ( ErrorCodeHelper.RESULT_CANCELED ); } // read up to SYNC_DATA_MAX int readCount = 0; try { readCount = fs.Read ( DataBuffer, 8, SYNC_DATA_MAX ); } catch ( IOException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_FILE_READ_ERROR, e ); } if ( readCount == 0 ) { // we reached the end of the file break; } // now send the data to the device // first write the amount read readCount.Swap32bitsToArray(DataBuffer, 4); // now write it try { AdbHelper.Instance.Write ( Channel, DataBuffer, readCount + 8, timeOut ); } catch ( IOException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_CONNECTION_ERROR, e ); } // and advance the monitor monitor.Advance ( readCount ); } // close the local file try { fs.Close ( ); } catch ( IOException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_FILE_READ_ERROR, e ); } try { // create the DONE message long time = DateTime.Now.CurrentTimeMillis ( ) / 1000; msg = CreateRequest ( DONE, (int)time ); // and send it. AdbHelper.Instance.Write ( Channel, msg, -1, timeOut ); // read the result, in a byte array containing 2 ints // (id, size) byte[] result = new byte[8]; AdbHelper.Instance.Read ( Channel, result, -1 /* full length */, timeOut ); if ( !CheckResult ( result, OKAY.GetBytes ( ) ) ) { if ( CheckResult ( result, FAIL.GetBytes ( ) ) ) { // read some error message... int len = result.Swap32bitFromArray ( 4 ); AdbHelper.Instance.Read ( Channel, DataBuffer, len, timeOut ); // output the result? String message = DataBuffer.GetString ( 0, len ); Log.e ( "ddms", "transfer error: " + message ); return new SyncResult ( ErrorCodeHelper.RESULT_UNKNOWN_ERROR, message ); } return new SyncResult ( ErrorCodeHelper.RESULT_UNKNOWN_ERROR ); } } catch ( IOException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_CONNECTION_ERROR, e ); } // files pushed have no permissions... // lets check if we can get to the file... if(this.Device.FileSystem.Exists(remotePath)) { this.Device.FileSystem.Chmod(remotePath, "0666"); } return new SyncResult ( ErrorCodeHelper.RESULT_OK ); }
private SyncResult DoPush( IEnumerable<FileSystemInfo> files, string remotePath, ISyncProgressMonitor monitor ) { if ( monitor == null ) { throw new ArgumentNullException ( "monitor", "Monitor cannot be null" ); } // check if we're canceled if ( monitor.IsCanceled ) { return new SyncResult ( ErrorCodeHelper.RESULT_CANCELED ); } foreach ( FileSystemInfo f in files ) { // check if we're canceled if ( monitor.IsCanceled ) { return new SyncResult ( ErrorCodeHelper.RESULT_CANCELED ); } // append the name of the directory/file to the remote path String dest = LinuxPath.Combine ( remotePath, f.Name ); if ( f.Exists ) { if ( f.IsDirectory ( ) ) { DirectoryInfo fsiDir = f as DirectoryInfo; monitor.StartSubTask ( f.FullName, dest ); SyncResult result = DoPush ( fsiDir.GetFileSystemInfos ( ), dest, monitor ); if ( result.Code != ErrorCodeHelper.RESULT_OK ) { return result; } monitor.Advance ( 1 ); } else if ( f.IsFile ( ) ) { monitor.StartSubTask ( f.FullName, dest ); SyncResult result = DoPushFile ( f.FullName, dest, monitor ); if ( result.Code != ErrorCodeHelper.RESULT_OK ) { return result; } } } } return new SyncResult ( ErrorCodeHelper.RESULT_OK ); }
/// <summary> /// Pulls a remote file /// </summary> /// <param name="remotePath">the remote file (length max is 1024)</param> /// <param name="localPath">the local destination</param> /// <param name="monitor">the monitor. The monitor must be started already.</param> /// <returns>a SyncResult object with a code and an optional message.</returns> /// <exception cref="ArgumentNullException">Throws if monitor is null</exception> private SyncResult DoPullFile( string remotePath, string localPath, ISyncProgressMonitor monitor ) { if ( monitor == null ) { throw new ArgumentNullException ( "monitor", "Monitor cannot be null" ); } byte[] msg = null; byte[] pullResult = new byte[8]; int timeOut = DdmPreferences.Timeout; try { byte[] remotePathContent = remotePath.GetBytes ( AdbHelper.DEFAULT_ENCODING ); if ( remotePathContent.Length > REMOTE_PATH_MAX_LENGTH ) { return new SyncResult ( ErrorCodeHelper.RESULT_REMOTE_PATH_LENGTH ); } // create the full request message msg = CreateFileRequest ( RECV.GetBytes ( ), remotePathContent ); // and send it. AdbHelper.Instance.Write ( Channel, msg, -1, timeOut ); // read the result, in a byte array containing 2 ints // (id, size) AdbHelper.Instance.Read ( Channel, pullResult, -1, timeOut ); // check we have the proper data back if ( CheckResult ( pullResult, DATA.GetBytes ( ) ) == false && CheckResult ( pullResult, DONE.GetBytes ( ) ) == false ) { return new SyncResult ( ErrorCodeHelper.RESULT_CONNECTION_ERROR ); } } catch ( EncoderFallbackException e ) { Console.WriteLine ( e ); return new SyncResult ( ErrorCodeHelper.RESULT_REMOTE_PATH_ENCODING, e ); } catch ( IOException e ) { Console.WriteLine ( e ); return new SyncResult ( ErrorCodeHelper.RESULT_CONNECTION_ERROR, e ); } // access the destination file FileInfo f = new FileInfo ( localPath ); // create the stream to write in the file. We use a new try/catch block to differentiate // between file and network io exceptions. FileStream fos = null; try { fos = new FileStream ( f.FullName,System.IO.FileMode.Create,FileAccess.Write ); } catch ( FileNotFoundException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_FILE_WRITE_ERROR, e ); } // the buffer to read the data byte[] data = new byte[SYNC_DATA_MAX]; using ( fos ) { // loop to get data until we're done. while ( true ) { // check if we're canceled if ( monitor.IsCanceled ) { return new SyncResult ( ErrorCodeHelper.RESULT_CANCELED ); } // if we're done, we stop the loop if ( CheckResult ( pullResult, DONE.GetBytes ( ) ) ) { break; } if ( CheckResult ( pullResult, DATA.GetBytes ( ) ) == false ) { // hmm there's an error return new SyncResult ( ErrorCodeHelper.RESULT_CONNECTION_ERROR ); } int length = pullResult.Swap32bitFromArray ( 4 ); if ( length > SYNC_DATA_MAX ) { // buffer overrun! // error and exit return new SyncResult ( ErrorCodeHelper.RESULT_BUFFER_OVERRUN ); } try { // now read the length we received AdbHelper.Instance.Read ( Channel, data, length, timeOut ); // get the header for the next packet. AdbHelper.Instance.Read ( Channel, pullResult, -1, timeOut ); } catch ( IOException e ) { Console.WriteLine ( e ); return new SyncResult ( ErrorCodeHelper.RESULT_CONNECTION_ERROR, e ); } // write the content in the file try { fos.Write ( data, 0, length ); } catch ( IOException e ) { return new SyncResult ( ErrorCodeHelper.RESULT_FILE_WRITE_ERROR, e ); } monitor.Advance ( length ); } try { fos.Flush ( ); } catch ( IOException e ) { Console.WriteLine ( e ); return new SyncResult ( ErrorCodeHelper.RESULT_FILE_WRITE_ERROR, e ); } } return new SyncResult ( ErrorCodeHelper.RESULT_OK ); }
/// <summary> /// /// </summary> /// <param name="entries"></param> /// <param name="localPath"></param> /// <param name="fls"></param> /// <param name="monitor"></param> /// <returns></returns> /// <exception cref="System.IO.IOException">Throws if unable to create a file or folder</exception> /// <exception cref="System.ArgumentNullException">Throws if the ISyncProgressMonitor is null</exception> private SyncResult DoPull( IEnumerable<FileEntry> entries, string localPath, FileListingService fileListingService, ISyncProgressMonitor monitor ) { if ( monitor == null ) { throw new ArgumentNullException ( "monitor", "Monitor cannot be null" ); } // check if we're cancelled if ( monitor.IsCanceled ) { return new SyncResult ( ErrorCodeHelper.RESULT_CANCELED ); } // check if we need to create the local directory DirectoryInfo localDir = new DirectoryInfo ( localPath ); if ( !localDir.Exists ) { localDir.Create ( ); } foreach ( FileEntry e in entries ) { // check if we're canceled if ( monitor.IsCanceled ) { return new SyncResult ( ErrorCodeHelper.RESULT_CANCELED ); } // the destination item (folder or file) String dest = Path.Combine ( localPath, e.Name ); // get type (we only pull directory and files for now) FileListingService.FileTypes type = e.Type; if ( type == FileListingService.FileTypes.Directory ) { monitor.StartSubTask ( e.FullPath, dest ); // then recursively call the content. Since we did a ls command // to get the number of files, we can use the cache FileEntry[] children = fileListingService.GetChildren ( e, true, null ); SyncResult result = DoPull ( children, dest, fileListingService, monitor ); if ( result.Code != ErrorCodeHelper.RESULT_OK ) { return result; } monitor.Advance ( 1 ); } else if ( type == FileListingService.FileTypes.File ) { monitor.StartSubTask ( e.FullPath, dest ); SyncResult result = DoPullFile ( e.FullPath, dest, monitor ); if ( result.Code != ErrorCodeHelper.RESULT_OK ) { return result; } } else if ( type == FileListingService.FileTypes.Link ) { monitor.StartSubTask ( e.FullPath, dest ); SyncResult result = DoPullFile ( e.FullResolvedPath, dest, monitor ); if ( result.Code != ErrorCodeHelper.RESULT_OK ) { return result; } } else { Log.d ( "ddms-sync", String.Format ( "unknown type to transfer: {0}", type ) ); } } return new SyncResult ( ErrorCodeHelper.RESULT_OK ); }