Beispiel #1
0
        /// <summary>
        /// Gets the weight of a package given its path.
        /// </summary>
        /// <param name="path">The path to the package.</param>
        /// <returns>The weight of a package given its path or 0 if the package is missing.</returns>
        internal static long GetWeightFromPath(string path)
        {
            if (File.Exists(path))
            {
                var weight = 0L;

                using (var db = new Database(path, DatabaseOpenMode.ReadOnly))
                {
                    // Get the total size of all files in the package.
                    if (null != db.Tables["File"])
                    {
                        weight += db.ExecuteIntegerQuery(PackageInfo.FileSizeQuery).Sum(i => i);
                    }

                    // TODO: Should the weight of the registry be taken into account?
                    // Storage isn't documented but it's probably a safe assumption value names and string data are double byte.

                    // Sum up reserve costs for local installs (source installs are uncommon).
                    if (null != db.Tables["ReserveCost"])
                    {
                        weight += db.ExecuteIntegerQuery(PackageInfo.ReserveCostSizeQuery).Sum(i => i);
                    }
                }

                // Use weight of package. May include just custom actions.
                if (0 >= weight)
                {
                    var file = new FileInfo(path);
                    weight = file.Length;
                }

                return weight;
            }

            return 0;
        }