internal static bool IsFtpBinaryVersionPresent(Uri uriRemoteDirPath, string version, string ftpUser, string ftpPassword, NLog.Logger logger) { bool present = false; Version versionCheck = new Version(version); try { string[] dirContents = SecureFtpRepoUpdate.ListDirectory(uriRemoteDirPath, ftpUser, ftpPassword, false /*details*/, true /*enableSSL*/); for (int i = 0; i < dirContents.Length; ++i) { if (dirContents[i] == version) { continue; } Version ver = new Version(dirContents[i]); if (ver == versionCheck) { present = true; break; } } } catch (Exception e) { logger.ErrorException("Failed to retrieve the latest version from remote ftp server", e); } return(present); }
public static void Cleanup() { try { SecureFtpRepoUpdate.DeleteFile(uriFile, remoteUsername, remoteUserPassword, true /*enableSSL*/); } catch (Exception) { // it's ok for this calls to fail, if the file is not present, ignoring... } try { SecureFtpRepoUpdate.RemoveDirectory(uriDir, remoteUsername, remoteUserPassword, true /*enableSSL*/); } catch (Exception) { // it's ok for this calls to fail, if the directory is not present, ignoring... } string fullpath = Path.GetFullPath(dataDownloadFilePath); if (File.Exists(fullpath)) { File.Delete(fullpath); } }
internal static Version GetFtpHighestVersionFromDir(Uri uriRemoteDirPath, string ftpUser, string ftpPassword, NLog.Logger logger) { string version = "0.0.0.0"; Version highest = new Version(version); try { string[] dirContents = SecureFtpRepoUpdate.ListDirectory(uriRemoteDirPath, ftpUser, ftpPassword, false /*details*/, true /*enableSSL*/); for (int i = 0; i < dirContents.Length; ++i) { if (dirContents[i] == "Latest") { continue; } Version ver = new Version(dirContents[i]); if (ver > highest) { highest = ver; } } } catch (Exception e) { logger.ErrorException("Failed to retrieve the latest version from remote ftp server", e); } return(highest); }
public void SecureFtpRepoUpdateTest_DownloadFile_UsingFtps() { string downloadfileFullPath = Path.GetFullPath(dataDownloadFilePath); SecureFtpRepoUpdate.DownloadFile(uriFile, downloadfileFullPath, remoteUsername, remoteUserPassword, true /*enableSSL*/); Assert.IsTrue(File.Exists(downloadfileFullPath)); Assert.IsTrue((new FileInfo(downloadfileFullPath).Length) == (new FileInfo(dataFilePath).Length)); }
private string GetLatestDeployedPlatformVersion(bool useCachedValue) { string homeOSPlatformVersion = PackagerHelper.BinaryPackagerHelper.UnknownHomeOSUpdateVersionValue; string remoteUsername = Properties.Settings.Default.SetupRepoAccountLogin; string remoteUserPassword = SimpleEncryptDecrypt(Properties.Settings.Default.SetupRepoAccountPassword, null); string remoteHost = Properties.Settings.Default.SetupRepoAccountHost; string remotePort = Properties.Settings.Default.SetupRepoAccountPort; string uri = remoteHost + ":" + remotePort + "/HomeOS/Hub/Platform/Latest/" + PlatformFilename + ".zip"; string localZipFile = ".\\" + PlatformFilename + ".zip"; string tmpFolder = ".\\" + PlatformFilename + ".tmp"; if (useCachedValue) { if (this.deployedPlatformVersionCached != PackagerHelper.BinaryPackagerHelper.UnknownHomeOSUpdateVersionValue) { homeOSPlatformVersion = this.deployedPlatformVersionCached; goto Exit; } } if (File.Exists(localZipFile)) { File.Delete(localZipFile); } if (Directory.Exists(tmpFolder)) { PackagerHelper.PackagerHelper.DeleteFolder(tmpFolder, true); } if (!SecureFtpRepoUpdate.GetZipFromUrl(new Uri(uri), localZipFile, remoteUsername, remoteUserPassword, true /* enableSSL */)) { logger.Error("Failed to download the latest platform zip"); outputBox.Text += "Failed to download the latest platform zip\r\n"; goto Exit; } if (!PackagerHelper.PackagerHelper.ExtractZipToFolder(localZipFile, tmpFolder)) { string s1 = string.Format("Failed to extract platform zip file {0} to folder location: {1}", localZipFile, tmpFolder); logger.Error(s1); outputBox.Text += s1; goto Exit; } homeOSPlatformVersion = Utils.GetHomeOSUpdateVersion(tmpFolder + "\\" + PlatformFilename + ".exe.config"); Exit: this.deployedPlatformVersionCached = homeOSPlatformVersion; return(homeOSPlatformVersion); }
internal static bool IsFtpRemoteDirectoryPresent(Uri uriRemoteDirPath, string ftpUser, string ftpPassword) { bool present = false; try { string[] dirContents = SecureFtpRepoUpdate.ListDirectory(uriRemoteDirPath, ftpUser, ftpPassword, false /*details*/, true /*enableSSL*/); present = true; } catch (Exception) { } return(present); }
private bool IsValidRepositoryAccountPresent() { bool valid = true; try { string[] directories = SecureFtpRepoUpdate.ListDirectory(new Uri(this.formRepoAccountInfo.RepoAccountHost + ":" + this.formRepoAccountInfo.RepoAccountPort), this.formRepoAccountInfo.RepoAccountLogin, this.formRepoAccountInfo.RepoAccountPassword, false, true ); } catch (Exception ex) { logger.ErrorException("Failed to list directories on the ftp server", ex); valid = false; } return(valid); }
private bool UpdateRepositoryWithFiles(string[] filePaths, string binType) { bool success = true; try { Version latestVersion = null; string binaryDirPath = ""; for (int i = 0; i < filePaths.Length; ++i) { string[] ftpFileLocations = filePaths[i].Split(new[] { this.repositoryDir }, StringSplitOptions.RemoveEmptyEntries); if (ftpFileLocations.Length > 0 && !string.IsNullOrWhiteSpace(ftpFileLocations[0])) { string ftpFilePath = ftpFileLocations[0].Replace("\\", "/"); string ftpDirPath = ftpFilePath.Substring(0, ftpFilePath.LastIndexOf("/")); Uri uriFile = new Uri(this.ftpHost + ":" + this.ftpPort + ftpFilePath); string subDirPath = ""; bool gotBinaryDirPath = false; foreach (string subdir in ftpDirPath.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries)) { subDirPath += ("/" + subdir); if (gotBinaryDirPath) { binaryDirPath = subDirPath; gotBinaryDirPath = false; } else if (binaryDirPath.Length == 0 && subDirPath.ToLower().Contains(binType.ToLower())) { gotBinaryDirPath = true; } else if (subdir.Contains('.')) { // assume this is the version number of the type x.x.x.x latestVersion = new Version(subdir); //fix the bug that causes incorrect path to be formed for Latest dir for x/Gadgeteer/MicrosoftResearch/X binaryDirPath = subDirPath.Substring(0, subDirPath.LastIndexOf("/")); } Uri uriDir = new Uri(this.ftpHost + ":" + this.ftpPort + subDirPath); if (!MainForm.IsFtpRemoteDirectoryPresent(uriDir, this.ftpUser, this.ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriDir, this.ftpUser, this.ftpPassword, true); } } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], this.ftpUser, this.ftpPassword, true); } } // update the latest folder for the binary, if this is highest version or if there is no latest folder at all Uri uriBinaryDir = new Uri(this.ftpHost + ":" + this.ftpPort + binaryDirPath); string binaryLatestDir = this.ftpHost + ":" + this.ftpPort + binaryDirPath + "/Latest"; Uri uriBinaryLatestDir = new Uri(binaryLatestDir); if (!MainForm.IsFtpRemoteDirectoryPresent(uriBinaryLatestDir, this.ftpUser, this.ftpPassword) || MainForm.GetFtpHighestVersionFromDir(uriBinaryDir, this.ftpUser, this.ftpPassword, this.logger) == latestVersion) { for (int i = 0; i < filePaths.Length; ++i) { string filename = filePaths[i].Substring(filePaths[i].LastIndexOf('\\') + 1); Uri uriFile = new Uri(binaryLatestDir + "/" + filename); if (!MainForm.IsFtpRemoteDirectoryPresent(uriBinaryLatestDir, this.ftpUser, this.ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriBinaryLatestDir, this.ftpUser, this.ftpPassword, true); } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], this.ftpUser, this.ftpPassword, true); } //Here we will upload the [binary].dll.config file, which contains the homeosupdate version of the binary, to the Latest dir on the repository string versionfile = filePaths[0].Substring(filePaths[0].LastIndexOf('\\') + 1).Replace("zip", "dll.config"); string binaryname = versionfile.Replace(".dll.config", ""); string versionfilepath = this.binaryRootDir + "\\" + binaryname + "\\" + versionfile; Uri uriversionfile = new Uri(binaryLatestDir + "/" + versionfile); SecureFtpRepoUpdate.UploadFile(uriversionfile, versionfilepath, this.ftpUser, this.ftpPassword, true); } } catch (Exception exception) { this.logger.ErrorException("Exception while trying to update binary package (plus hash file) on the ftp server", exception); success = false; } return(success); }
private void buttonPlatUpdate_Click(object sender, EventArgs e) { try { TableLayoutControlCollection tableLayoutCtrlColl = this.tableLayoutPanelPlatform.Controls; System.Windows.Forms.Control labelDepVer = tableLayoutCtrlColl[this.labelPlatDeployedVer.Name]; System.Windows.Forms.Control labelCurVer = tableLayoutCtrlColl[this.labelPlatYourVer.Name]; // string hubId = checkbox.Text; string deployedVersion = labelDepVer.Text; string currentVersion = labelCurVer.Text; string platformDirPath = ""; Version latestVersion = null; string platformRootDir = this.textBoxSetupWorkingFolder.Text + "\\binaries\\Platform"; string repositoryDir = this.textBoxSetupWorkingFolder.Text + "\\HomeStore\\Repository"; string ftpHost = this.formRepoAccountInfo.RepoAccountHost; string ftpPort = this.formRepoAccountInfo.RepoAccountPort; string ftpUser = this.formRepoAccountInfo.RepoAccountLogin; string ftpPassword = this.formRepoAccountInfo.RepoAccountPassword; // make sure you are updating to a higher version Version versionDeployed = new Version(deployedVersion); Version versionCurrent = new Version(currentVersion); if (versionCurrent <= versionDeployed) { MessageBox.Show(string.Format("Only Updates to higher versions of the platform are permissable, please update the version!"), "Update Platform", MessageBoxButtons.OK, MessageBoxIcon.Exclamation ); return; } if (DialogResult.Yes == MessageBox.Show((IWin32Window)sender, string.Format("Are you sure you want to update the platform from the deployed version {0} to your current version {1}?", deployedVersion, currentVersion), "Update Platform", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)) { string[] filePaths = new string[0]; bool success = BinaryPackagerHelper.Package(platformRootDir, MainForm.PlatformFilename, true /*singleBin*/, "exe", "platform", repositoryDir, ref filePaths, this.logger); if (!success) { throw new Exception("Failed to create platform update packages"); } Debug.Assert(filePaths.Length == 2); for (int i = 0; i < filePaths.Length; ++i) { string[] ftpFileLocations = filePaths[i].Split(new[] { repositoryDir }, StringSplitOptions.RemoveEmptyEntries); if (ftpFileLocations.Length > 0 && !string.IsNullOrWhiteSpace(ftpFileLocations[0])) { string ftpFilePath = ftpFileLocations[0].Replace("\\", "/"); string ftpDirPath = ftpFilePath.Substring(0, ftpFilePath.LastIndexOf("/")); Uri uriFile = new Uri(ftpHost + ":" + ftpPort + ftpFilePath); string subDirPath = ""; foreach (string subdir in ftpDirPath.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries)) { subDirPath += ("/" + subdir); if (platformDirPath.Length == 0 && subDirPath.ToLower().Contains(PlatformName.ToLower())) { platformDirPath = subDirPath; } if (subdir.Contains('.')) { // assume this is the version number of the type x.x.x.x latestVersion = new Version(subdir); } Uri uriDir = new Uri(ftpHost + ":" + ftpPort + subDirPath); if (!MainForm.IsFtpRemoteDirectoryPresent(uriDir, ftpUser, ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriDir, ftpUser, ftpPassword, true); } } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], ftpUser, ftpPassword, true); } } // update the latest folder for the platform, if this is highest version or if there is no latest folder at all Uri uriPlatformDir = new Uri(ftpHost + ":" + ftpPort + platformDirPath); string platformLatestDir = ftpHost + ":" + ftpPort + platformDirPath + "/Latest"; Uri uriPlatformLatestDir = new Uri(platformLatestDir); if (!MainForm.IsFtpRemoteDirectoryPresent(uriPlatformLatestDir, ftpUser, ftpPassword) || MainForm.GetFtpHighestVersionFromDir(uriPlatformDir, ftpUser, ftpPassword, this.logger) == latestVersion) { for (int i = 0; i < filePaths.Length; ++i) { string filename = filePaths[i].Substring(filePaths[i].LastIndexOf('\\') + 1); Uri uriFile = new Uri(platformLatestDir + "/" + filename); if (!MainForm.IsFtpRemoteDirectoryPresent(uriPlatformLatestDir, ftpUser, ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriPlatformLatestDir, ftpUser, ftpPassword, true); } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], ftpUser, ftpPassword, true); } } } } catch (Exception exception) { this.logger.ErrorException("Exception while trying to update platform on the ftp server", exception); outputBox.Text += "Exception while trying to update platform on the ftp server" + exception + "\r\n"; } }
public void SecureFtpRepoUpdateTest_DeleteFile_UsingFtps() { SecureFtpRepoUpdate.DeleteFile(uriFile, remoteUsername, remoteUserPassword, true /*enableSSL*/); }
public void SecureFtpRepoUpdateTest_UploadFile_UsingFtps() { SecureFtpRepoUpdate.UploadFile(uriFile, Path.GetFullPath(dataFilePath), remoteUsername, remoteUserPassword, true /*enableSSL*/); }
public void SecureFtpRepoUpdateTest_RemoveDirectory_UsingFtps() { SecureFtpRepoUpdate.RemoveDirectory(uriDir, remoteUsername, remoteUserPassword, true /*enableSSL*/); }
public void SecureFtpRepoUpdateTest_GetFileDateTimeStamp_UsingFtps() { string fileDateTimeStamp = SecureFtpRepoUpdate.GetFileDateTimeStamp(uriFile, remoteUsername, remoteUserPassword, true /*enableSSL*/); Assert.IsTrue(fileDateTimeStamp.Length > 0); }
public void SecureFtpRepoUpdateTest_GetFileSize_UsingFtps() { long fileSize = SecureFtpRepoUpdate.GetFileSize(uriFile, remoteUsername, remoteUserPassword, true /*enableSSL*/); Assert.IsTrue(fileSize > 0); }
public void SecureFtpRepoUpdateTest_ListDirectoryDetails_UsingFtps() { string[] files = SecureFtpRepoUpdate.ListDirectory(uriDir, remoteUsername, remoteUserPassword, true /*details*/, true /*enableSSL*/); Assert.IsTrue(files.GetLength(0) > 0); }