public bool SetMaxStorageSize()

in SDK/AppCenter/Microsoft.AppCenter.Windows.Shared/Storage/StorageAdapter.cs [191:238]


        public bool SetMaxStorageSize(long sizeInBytes)
        {
            var db = _db ?? throw new StorageException("The database wasn't initialized.");

            // Check the current number of pages in the database to determine whether the requested size will shrink the database.
            var currentPageCount = GetPageCount();
            var pageSize = GetPageSize();
            AppCenterLog.Info(AppCenterLog.LogTag, $"Found {currentPageCount} pages in the database.");
            var requestedMaxPageCount = Convert.ToBoolean(sizeInBytes % pageSize) ? sizeInBytes / pageSize + 1 : sizeInBytes / pageSize;
            if (currentPageCount > requestedMaxPageCount)
            {
                AppCenterLog.Warn(AppCenterLog.LogTag, $"Cannot change database size to {sizeInBytes} bytes as it would cause a loss of data. " +
                    "Maximum database size will not be changed.");
                return false;
            }
            else
            {
                // Attempt to set the limit and check the page count to make sure the given limit works.
                var result = raw.sqlite3_exec(db, $"PRAGMA max_page_count = {requestedMaxPageCount};");
                if (result != raw.SQLITE_OK)
                {
                    AppCenterLog.Error(AppCenterLog.LogTag, $"Could not change maximum database size to {sizeInBytes} bytes. SQLite error code: {result}.");
                    return false;
                }
                else
                {
                    var currentMaxPageCount = GetMaxPageCount();
                    var actualMaxSize = currentMaxPageCount * pageSize;
                    if (requestedMaxPageCount != currentMaxPageCount)
                    {
                        AppCenterLog.Error(AppCenterLog.LogTag, $"Could not change maximum database size to {sizeInBytes} bytes, current maximum size is {actualMaxSize} bytes.");
                        return false;
                    }
                    else
                    {
                        if (sizeInBytes == actualMaxSize)
                        {
                            AppCenterLog.Info(AppCenterLog.LogTag, $"Changed maximum database size to {actualMaxSize} bytes.");
                        }
                        else
                        {
                            AppCenterLog.Info(AppCenterLog.LogTag, $"Changed maximum database size to {actualMaxSize} bytes (next multiple of 4KiB).");
                        }
                        return true;
                    }
                }
            }
        }