113 lines
3.8 KiB
C#
113 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Foodsoft.Alpm
|
|
{
|
|
public sealed class Database : IDisposable
|
|
{
|
|
private readonly SafeDatabaseHandle _handle;
|
|
|
|
internal Database(SafeDatabaseHandle handle) => _handle = handle;
|
|
|
|
public void Unregister() => _handle.Close();
|
|
|
|
public void AddServer(string url) =>
|
|
API.WrapError(_handle.SafeAlpmHandle, () => alpm.alpm_db_add_server(_handle, url));
|
|
|
|
public bool RemoveServer(string url) =>
|
|
API.WrapErrorBool(_handle.SafeAlpmHandle, () => alpm.alpm_db_remove_server(_handle, url));
|
|
|
|
private readonly struct ServersImpl : ICollectionImpl<string>
|
|
{
|
|
private readonly SafeDatabaseHandle _handle;
|
|
internal ServersImpl(SafeDatabaseHandle handle) => _handle = handle;
|
|
public SafeHandle Handle => _handle;
|
|
public IntPtr GetItems() => alpm.alpm_db_get_servers(_handle);
|
|
public string PtrToItem(IntPtr p) => Marshal.PtrToStringUTF8(p)!;
|
|
}
|
|
|
|
// FIXME: This is "bug correct", but probably dumb to do it this way, instead just call add_server
|
|
// like all the stuff in handle.
|
|
public IEnumerable<string> Servers
|
|
{
|
|
get => EnumerableWrapper<string>.Create(new ServersImpl(_handle));
|
|
set
|
|
{
|
|
var listPtr = IntPtr.Zero;
|
|
var success = false;
|
|
var err = 0;
|
|
RuntimeHelpers.PrepareConstrainedRegions();
|
|
try
|
|
{
|
|
// ReSharper disable once LoopCanBeConvertedToQuery
|
|
foreach (var s in value)
|
|
{
|
|
if (alpm.alpm_list_append_strdup(ref listPtr, s) == IntPtr.Zero)
|
|
throw new AlpmException(ErrNo.ERR_MEMORY);
|
|
}
|
|
success = true;
|
|
}
|
|
finally
|
|
{
|
|
if (success)
|
|
err = alpm.alpm_db_set_servers(_handle, listPtr);
|
|
else
|
|
alpm.alpm_list_free(listPtr);
|
|
}
|
|
|
|
if (err != 0)
|
|
throw new AlpmException(_handle.SafeAlpmHandle);
|
|
}
|
|
}
|
|
|
|
public string Name => alpm.alpm_db_get_name(_handle);
|
|
|
|
public SigLevel SigLevel => alpm.alpm_db_get_siglevel(_handle);
|
|
|
|
public ErrNo Valid =>
|
|
alpm.alpm_db_get_valid(_handle) != 0 ? alpm.alpm_errno(_handle.SafeAlpmHandle) : ErrNo.ERR_OK;
|
|
|
|
public bool Update(bool force = false)
|
|
{
|
|
var err = alpm.alpm_db_update(force ? 1 : 0, _handle);
|
|
if (err < 0)
|
|
{
|
|
throw new AlpmException(_handle.SafeAlpmHandle);
|
|
}
|
|
|
|
return err == 0;
|
|
}
|
|
|
|
public Package? GetPackage(string name)
|
|
{
|
|
var pkgPtr = alpm.alpm_pkg_get_pkg(_handle, name);
|
|
if (pkgPtr == IntPtr.Zero)
|
|
{
|
|
var err = alpm.alpm_errno(_handle.SafeAlpmHandle);
|
|
if (err == ErrNo.ERR_PKG_NOT_FOUND)
|
|
return null;
|
|
throw new AlpmException(err);
|
|
}
|
|
|
|
return new CachePackage(new SafeCachePackageHandle(pkgPtr, _handle), this);
|
|
}
|
|
|
|
public PackageList PackageCache
|
|
{
|
|
get
|
|
{
|
|
var listPtr = alpm.alpm_db_get_pkgcache(_handle);
|
|
if (listPtr == IntPtr.Zero)
|
|
throw new AlpmException(_handle.SafeAlpmHandle);
|
|
return new PackageList(listPtr, _handle, this);
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_handle.Dispose();
|
|
}
|
|
}
|
|
} |