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:
Zbigniew Jędrzejewski-Szmek 2017-07-09 23:59:30 -04:00
parent 9cdcf3681c
commit 7f0cc63771
3 changed files with 7 additions and 30 deletions

View File

@ -47,7 +47,8 @@ int generator_add_symlink(const char *root, const char *dst, const char *dep_typ
mkdir_parents_label(to, 0755);
if (symlink(from, to) < 0)
return log_error_errno(errno, "Failed to create symlink \"%s\": %m", to);
if (errno != EEXIST)
return log_error_errno(errno, "Failed to create symlink \"%s\": %m", to);
return 0;
}

View File

@ -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;
}

View File

@ -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),