checkpoint

This commit is contained in:
2020-04-29 23:47:06 -04:00
parent 64f7dd4b7d
commit 19a9fc06ba
31 changed files with 678 additions and 360 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Foodsoft.Alpm
@@ -10,44 +11,54 @@ namespace Foodsoft.Alpm
internal Database(SafeDatabaseHandle handle) => _handle = handle;
public void Unregister()
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>
{
try
{
API.WrapError(_handle.SafeAlpmHandle, () => alpm.alpm_db_unregister(_handle));
}
finally
{
_handle.Close();
}
}
private readonly struct ServersAccessor : IItemsReader<string, SafeDatabaseHandle>
{
internal ServersAccessor(SafeDatabaseHandle handle)
{
Handle = handle;
}
public SafeDatabaseHandle Handle { get; }
public IntPtr GetItems() => alpm.alpm_db_get_servers(Handle);
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)!;
}
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));
// 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 => new EnumerableWrapper<ServersAccessor, string, SafeDatabaseHandle>(new ServersAccessor(_handle));
get => EnumerableWrapper<string>.Create(new ServersImpl(_handle));
set
{
foreach (var s in value)
var listPtr = IntPtr.Zero;
var success = false;
var err = 0;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
API.WrapError(_handle.SafeAlpmHandle, () => alpm.alpm_db_add_server(_handle, s));
// 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);
}
}
@@ -63,7 +74,7 @@ namespace Foodsoft.Alpm
var err = alpm.alpm_db_update(force ? 1 : 0, _handle);
if (err < 0)
{
throw new Exception(_handle.SafeAlpmHandle);
throw new AlpmException(_handle.SafeAlpmHandle);
}
return err == 0;
@@ -77,7 +88,7 @@ namespace Foodsoft.Alpm
var err = alpm.alpm_errno(_handle.SafeAlpmHandle);
if (err == ErrNo.ERR_PKG_NOT_FOUND)
return null;
throw new Exception(err);
throw new AlpmException(err);
}
return new CachePackage(new SafeCachePackageHandle(pkgPtr, _handle), this);
@@ -89,8 +100,8 @@ namespace Foodsoft.Alpm
{
var listPtr = alpm.alpm_db_get_pkgcache(_handle);
if (listPtr == IntPtr.Zero)
throw new Exception(alpm.alpm_errno(_handle.SafeAlpmHandle));
return new PackageList(new SafeListHandle<SafeDatabaseHandle>(listPtr, _handle), this);
throw new AlpmException(_handle.SafeAlpmHandle);
return new PackageList(listPtr, _handle, this);
}
}