Ejemplo n.º 1
0
 public static void UpdateTimestamp(Log log, string url)
 {
     Disk.TouchFile(log, GetFileName(url));
 }
Ejemplo n.º 2
0
        void InstallItem(KeyValuePair <string, object> item)
        {
            var itemKey    = item.Key.Replace('/', Path.DirectorySeparatorChar);
            var components = itemKey.Split(Path.DirectorySeparatorChar);

            if (components.Contains(".") || components.Contains(".."))
            {
                throw new FormatException(item.Key + ": '.' or '..' are not valid in directory names");
            }

            var itemFile  = Path.Combine(_parentDirectory, ".uno", "stuff", itemKey);
            var targetDir = Path.Combine(_parentDirectory, itemKey);
            var itemValue = item.Value?.ToString();

            if (string.IsNullOrEmpty(itemValue) ||
                IsItemUpToDate(Log, targetDir, itemFile, itemValue, _flags))
            {
                return;
            }

            using (new FileLock(Log, itemFile, DownloadCache.GetFileName(itemValue)))
            {
                if (IsItemUpToDate(Log, targetDir, itemFile, itemValue, _flags))
                {
                    return;
                }

                for (int tries = 0;; tries++)
                {
                    Log.Verbose("Extracting " + targetDir.ToRelativePath().Quote());

                    // Support local files (e.g. from .STUFF-UPLOAD files)
                    var localFile = Path.Combine(_parentDirectory, itemValue);
                    var isLocal   = File.Exists(localFile);
                    var file      = isLocal
                        ? localFile
                        : DownloadCache.GetFile(Log, itemValue);

                    Disk.DeleteDirectory(Log, targetDir);
                    Disk.CreateDirectory(Log, targetDir);

                    try
                    {
                        if (PlatformDetection.IsWindows)
                        {
                            LongPathZipFile.ExtractToDirectory(file, targetDir);
                        }
                        else
                        {
                            // Use system tar/unzip to preserve file permissions and links
                            // (ZipFile doesn't handle this)
                            if (Path.GetFileName(file).ToUpper().EndsWith(".TAR.GZ"))
                            {
                                Shell.Untar(Log, file, targetDir);
                            }
                            else
                            {
                                Shell.Unzip(Log, file, targetDir);
                            }

                            // Make sure files extracted are writable so we can touch them
                            Shell.Chmod(Log, "+w", targetDir);
                            Disk.TouchAllFiles(Log, targetDir);
                        }

                        Disk.CreateDirectory(Log, Path.GetDirectoryName(itemFile));
                        File.WriteAllText(itemFile, itemValue);
                        break;
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // Don't try more if we get this
                        throw;
                    }
                    catch
                    {
                        // Delete any installed files
                        Disk.DeleteFile(Log, itemFile);
                        Disk.DeleteDirectory(Log, targetDir);

                        if (isLocal)
                        {
                            throw;
                        }

                        // Delete the cached download too
                        Disk.DeleteFile(Log, file);

                        // Redownload, and try just one more time to be sure
                        // This might fix the problem if the cached file was corrupt
                        if (tries > 0)
                        {
                            throw;
                        }

                        Thread.Sleep(150);
                    }
                }
            }
        }