Commit 618a8e47 authored by Julian Andres Klode's avatar Julian Andres Klode

apt.Cache: Keep archive locked during commit()/ in fetch_archives()

Use Acquire.get_lock() to get the lock as part of the acquire object
to keep the lock alive as long as the acquire object exists. This is
somewhat bad style, as it relies on reference counting...
parent 45e44f8a
......@@ -426,21 +426,22 @@ class Cache(object):
raise CacheClosedException(
"Cache object used after close() called")
# get lock
lockfile = apt_pkg.config.find_dir("Dir::Cache::Archives") + "lock"
lock = apt_pkg.get_lock(lockfile)
if lock < 0:
raise LockFailedException("Failed to lock %s" % lockfile)
# this may as well throw a SystemError exception
if not pm.get_archives(fetcher, self._list, self._records):
return False
# now run the fetcher, throw exception if something fails to be
# fetched
return self._run_fetcher(fetcher)
def _get_archive_lock(self, fetcher):
# type: (apt_pkg.Acquire) -> None
# get lock
archive_dir = apt_pkg.config.find_dir("Dir::Cache::Archives")
try:
# this may as well throw a SystemError exception
if not pm.get_archives(fetcher, self._list, self._records):
return False
# now run the fetcher, throw exception if something fails to be
# fetched
return self._run_fetcher(fetcher)
finally:
os.close(lock)
fetcher.get_lock(archive_dir)
except apt_pkg.Error as e:
raise LockFailedException(("Failed to lock archive directory %s: "
" %s") % (archive_dir, e))
def fetch_archives(self, progress=None, fetcher=None):
# type: (AcquireProgress, apt_pkg.Acquire) -> int
......@@ -463,6 +464,8 @@ class Cache(object):
if fetcher is None:
fetcher = apt_pkg.Acquire(progress)
self._get_archive_lock(fetcher)
return self._fetch_archives(fetcher,
apt_pkg.PackageManager(self._depcache))
......@@ -630,6 +633,8 @@ class Cache(object):
with apt_pkg.SystemLock():
pm = apt_pkg.PackageManager(self._depcache)
fetcher = apt_pkg.Acquire(fetch_progress)
self._get_archive_lock(fetcher)
while True:
# fetch archives first
res = self._fetch_archives(fetcher, pm)
......
......@@ -239,6 +239,17 @@ static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args)
return HandleErrors(Py_None);
}
static PyObject *PkgAcquireGetLock(PyObject *Self,PyObject *Args)
{
pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self);
PyApt_Filename path;
if (PyArg_ParseTuple(Args, "O&", PyApt_Filename::Converter, &path) == 0)
return 0;
fetcher->GetLock(path);
Py_INCREF(Py_None);
return HandleErrors(Py_None);
}
static PyMethodDef PkgAcquireMethods[] = {
......@@ -254,6 +265,9 @@ static PyMethodDef PkgAcquireMethods[] = {
"Shut the fetcher down, removing all items from it. Future access to\n"
"queued AcquireItem objects will cause a segfault. The partial result\n"
"is kept on the disk and not removed and APT might reuse it."},
{"get_lock",PkgAcquireGetLock, METH_VARARGS,
"get_lock(log: str)\n\n"
"Acquires a log for the given directory, using a file 'lock' in it."},
{}
};
......
......@@ -169,6 +169,7 @@ class Acquire:
def __init__(self, progress: AcquireProgress=None) -> None: ...
def run(self) -> int: ...
def shutdown(self) -> None: ...
def get_lock(self, path: str) -> None: ...
class AcquireWorker:
current_item: AcquireItemDesc
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment