public IEnumerable <ScalarRepoRegistration> GetRegisteredRepos() { if (this.fileSystem.DirectoryExists(this.registryFolderPath)) { IEnumerable <string> registryFilePaths = this.fileSystem.EnumerateFiles(this.registryFolderPath, $"*{RegistryFileExtension}"); foreach (string registryFilePath in registryFilePaths) { ScalarRepoRegistration registration = null; try { string repoData = this.fileSystem.ReadAllText(registryFilePath); registration = ScalarRepoRegistration.FromJson(repoData); } catch (Exception e) { EventMetadata metadata = CreateEventMetadata(e); metadata.Add(nameof(registryFilePath), registryFilePath); this.tracer.RelatedWarning( metadata, $"{nameof(this.GetRegisteredRepos)}: Failed to read registry file"); } if (registration != null) { yield return(registration); } } } }
public bool TryRegisterRepo(string normalizedRepoRoot, string userId, out string errorMessage) { if (!this.TryCreateRepoRegistryDirectory(out errorMessage)) { return(false); } string tempRegistryPath = this.GetRepoRegistryTempFilePath(normalizedRepoRoot); try { ScalarRepoRegistration repoRegistration = new ScalarRepoRegistration(normalizedRepoRoot, userId); string registryFileContents = repoRegistration.ToJson(); using (Stream tempFile = this.fileSystem.OpenFileStream( tempRegistryPath, FileMode.Create, FileAccess.Write, FileShare.None, callFlushFileBuffers: true)) using (StreamWriter writer = new StreamWriter(tempFile)) { writer.WriteLine(registryFileContents); tempFile.Flush(); } } catch (Exception e) { errorMessage = $"Error while registering repo {normalizedRepoRoot}: {e.Message}"; EventMetadata metadata = CreateEventMetadata(e); metadata.Add(nameof(normalizedRepoRoot), normalizedRepoRoot); metadata.Add(nameof(tempRegistryPath), tempRegistryPath); this.tracer.RelatedError(metadata, $"{nameof(this.TryRegisterRepo)}: Exception while writing temp registry file"); return(false); } string registryFilePath = this.GetRepoRegistryFilePath(normalizedRepoRoot); try { this.fileSystem.MoveAndOverwriteFile(tempRegistryPath, registryFilePath); } catch (Win32Exception e) { errorMessage = $"Error while registering repo {normalizedRepoRoot}: {e.Message}"; EventMetadata metadata = CreateEventMetadata(e); metadata.Add(nameof(normalizedRepoRoot), normalizedRepoRoot); metadata.Add(nameof(tempRegistryPath), tempRegistryPath); metadata.Add(nameof(registryFilePath), registryFilePath); this.tracer.RelatedError(metadata, $"{nameof(this.TryRegisterRepo)}: Exception while renaming temp registry file"); return(false); } errorMessage = null; return(true); }