mirror of
https://github.com/systemd/systemd.git
synced 2025-02-25 21:57:32 +03:00
sysv-generator: use generator_add_symlink()
generator_add_symlink() is extended to ignore EEXIST. This should be fine for all existing callers. There's a small difference in behaviour when adding symlinks in sysv-generator: the message is more generic and does not include ", ignored". But creation of symlinks shouldn't ever fail except if things are very wrong, so in practice this shouldn't matter. Test needed updating: os.path.exists(os.readlink(link)) only works if the link is absolute (or if we are in the right directory). Let's just use os.path.exists(link), which properly tests that the symlink target exists.
This commit is contained in:
parent
9cdcf3681c
commit
7f0cc63771
@ -47,6 +47,7 @@ int generator_add_symlink(const char *root, const char *dst, const char *dep_typ
|
||||
|
||||
mkdir_parents_label(to, 0755);
|
||||
if (symlink(from, to) < 0)
|
||||
if (errno != EEXIST)
|
||||
return log_error_errno(errno, "Failed to create symlink \"%s\": %m", to);
|
||||
|
||||
return 0;
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "exit-status.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "generator.h"
|
||||
#include "hashmap.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "install.h"
|
||||
@ -101,29 +102,6 @@ static void free_sysvstub_hashmapp(Hashmap **h) {
|
||||
hashmap_free(*h);
|
||||
}
|
||||
|
||||
static int add_symlink(const char *service, const char *where) {
|
||||
const char *from, *to;
|
||||
int r;
|
||||
|
||||
assert(service);
|
||||
assert(where);
|
||||
|
||||
from = strjoina(arg_dest, "/", service);
|
||||
to = strjoina(arg_dest, "/", where, ".wants/", service);
|
||||
|
||||
mkdir_parents_label(to, 0755);
|
||||
|
||||
r = symlink(from, to);
|
||||
if (r < 0) {
|
||||
if (errno == EEXIST)
|
||||
return 0;
|
||||
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int add_alias(const char *service, const char *alias) {
|
||||
const char *link;
|
||||
int r;
|
||||
@ -219,11 +197,8 @@ static int generate_unit_file(SysvStub *s) {
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to write unit %s: %m", unit);
|
||||
|
||||
STRV_FOREACH(p, s->wanted_by) {
|
||||
r = add_symlink(s->name, *p);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to create 'Wants' symlink to %s, ignoring: %m", *p);
|
||||
}
|
||||
STRV_FOREACH(p, s->wanted_by)
|
||||
(void) generator_add_symlink(arg_dest, *p, "wants", s->name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -153,7 +153,8 @@ class SysvGeneratorTest(unittest.TestCase):
|
||||
link = os.path.join(self.out_dir, '%s.target.wants' % target, unit)
|
||||
if target in targets:
|
||||
unit_file = os.readlink(link)
|
||||
self.assertTrue(os.path.exists(unit_file))
|
||||
# os.path.exists() will fail on a dangling symlink
|
||||
self.assertTrue(os.path.exists(link))
|
||||
self.assertEqual(os.path.basename(unit_file), unit)
|
||||
else:
|
||||
self.assertFalse(os.path.exists(link),
|
||||
|
Loading…
x
Reference in New Issue
Block a user