private void Update(string fileName, string factoryPath = "")
{
if (!IsUserFolder)
{
if (fileName.ToLowerInvariant().Contains("xmatter") && !fileName.ToLower().StartsWith("factory-xmatter"))
{
return; //we don't want to copy custom xmatters around to the program files directory, template directories, the Bloom src code folders, etc.
}
}
// do not attempt to copy files to the installation directory
var targetDirInfo = new DirectoryInfo(_folderPath);
if (SIL.PlatformUtilities.Platform.IsMono)
{
// do not attempt to copy files to the "/usr" directory
if (targetDirInfo.FullName.StartsWith("/usr")) return;
}
else
{
// do not attempt to copy files to the "Program Files" directory
var programFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
if (!string.IsNullOrEmpty(programFolderPath))
{
var programsDirInfo = new DirectoryInfo(programFolderPath);
if (String.Compare(targetDirInfo.FullName, programsDirInfo.FullName, StringComparison.InvariantCultureIgnoreCase) == 0) return;
}
// do not attempt to copy files to the "Program Files (x86)" directory either
if (Environment.Is64BitOperatingSystem)
{
programFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
if (!string.IsNullOrEmpty(programFolderPath))
{
var programsDirInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
if (String.Compare(targetDirInfo.FullName, programsDirInfo.FullName, StringComparison.InvariantCultureIgnoreCase) == 0) return;
}
}
}
string documentPath="notSet";
try
{
if(string.IsNullOrEmpty(factoryPath))
{
factoryPath = _fileLocator.LocateFile(fileName);
}
if(string.IsNullOrEmpty(factoryPath))//happens during unit testing
return;
documentPath = Path.Combine(_folderPath, fileName);
if(!RobustFile.Exists(documentPath))
{
Logger.WriteMinorEvent("BookStorage.Update() Copying missing file {0} to {1}", factoryPath, documentPath);
RobustFile.Copy(factoryPath, documentPath);
return;
}
// due to BL-2166, we no longer compare times since downloaded books often have
// more recent times than the DistFiles versions we want to use
// var documentTime = File.GetLastWriteTimeUtc(documentPath);
if (factoryPath == documentPath)
return; // no point in trying to update self!
if (IsPathReadonly(documentPath))
{
var msg = string.Format("Could not update one of the support files in this document ({0}) because the destination was marked ReadOnly.", documentPath);
Logger.WriteEvent(msg);
SIL.Reporting.ErrorReport.NotifyUserOfProblem(msg);
return;
}
Logger.WriteMinorEvent("BookStorage.Update() Copying file {0} to {1}", factoryPath, documentPath);
RobustFile.Copy(factoryPath, documentPath, true);
//if the source was locked, don't copy the lock over
RobustFile.SetAttributes(documentPath, FileAttributes.Normal);
}
catch (Exception e)
{
if(documentPath.Contains(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles))
|| documentPath.ToLowerInvariant().Contains("program"))//english only
{
Logger.WriteEvent("Could not update file {0} because it was in the program directory.", documentPath);
return;
}
if(_alreadyNotifiedAboutOneFailedCopy)
return;//don't keep bugging them
_alreadyNotifiedAboutOneFailedCopy = true;
var msg = String.Format("Could not update one of the support files in this document ({0} to {1}).", documentPath, factoryPath);
NonFatalProblem.Report(ModalIf.None, PassiveIf.All, "Can't Update Support File", msg, exception: e);
}
}