func (*SymlinkSuite) TestIsSymlinkFileDoesNotExist(c *gc.C) { dir := c.MkDir() target := filepath.Join(dir, "file") isSymlink, err := symlink.IsSymlink(target) c.Assert(err, gc.ErrorMatches, ".*"+utils.NoSuchFileErrRegexp) c.Assert(isSymlink, jc.IsFalse) }
func (*SymlinkSuite) TestIsSymlinkFalseFolder(c *gc.C) { target, err := symlink.GetLongPathAsString(c.MkDir()) c.Assert(err, gc.IsNil) _, err = os.Stat(target) c.Assert(err, gc.IsNil) isSymlink, err := symlink.IsSymlink(target) c.Assert(err, gc.IsNil) c.Assert(isSymlink, jc.IsFalse) }
func (*SymlinkSuite) TestIsSymlinkFalseFile(c *gc.C) { dir := c.MkDir() target := filepath.Join(dir, "file") err := ioutil.WriteFile(target, []byte("TOP SECRET"), 0644) c.Assert(err, gc.IsNil) _, err = os.Stat(target) c.Assert(err, gc.IsNil) isSymlink, err := symlink.IsSymlink(target) c.Assert(err, gc.IsNil) c.Assert(isSymlink, jc.IsFalse) }
func (*SymlinkSuite) TestIsSymlinkFolder(c *gc.C) { target, err := symlink.GetLongPathAsString(c.MkDir()) c.Assert(err, gc.IsNil) link := filepath.Join(target, "link") _, err = os.Stat(target) c.Assert(err, gc.IsNil) err = symlink.New(target, link) c.Assert(err, gc.IsNil) isSymlink, err := symlink.IsSymlink(link) c.Assert(err, gc.IsNil) c.Assert(isSymlink, jc.IsTrue) }
func (*SymlinkSuite) TestIsSymlinkFile(c *gc.C) { dir, err := symlink.GetLongPathAsString(c.MkDir()) c.Assert(err, gc.IsNil) target := filepath.Join(dir, "file") err = ioutil.WriteFile(target, []byte("TOP SECRET"), 0644) c.Assert(err, gc.IsNil) link := filepath.Join(dir, "link") _, err = os.Stat(target) c.Assert(err, gc.IsNil) err = symlink.New(target, link) c.Assert(err, gc.IsNil) isSymlink, err := symlink.IsSymlink(link) c.Assert(err, gc.IsNil) c.Assert(isSymlink, jc.IsTrue) }
// EnsureSymlinks creates a symbolic link to jujuc within dir for each // hook command. If the commands already exist, this operation does nothing. // If dir is a symbolic link, it will be dereferenced first. func EnsureSymlinks(dir string) (err error) { logger.Infof("ensure jujuc symlinks in %s", dir) defer func() { if err != nil { err = errors.Annotatef(err, "cannot initialize hook commands in %q", dir) } }() isSymlink, err := symlink.IsSymlink(dir) if err != nil { return err } if isSymlink { link, err := symlink.Read(dir) if err != nil { return err } if !filepath.IsAbs(link) { logger.Infof("%s is relative", link) link = filepath.Join(filepath.Dir(dir), link) } dir = link logger.Infof("was a symlink, now looking at %s", dir) } jujudPath := filepath.Join(dir, names.Jujud) logger.Debugf("jujud path %s", jujudPath) for _, name := range CommandNames() { // The link operation fails when the target already exists, // so this is a no-op when the command names already // exist. err := symlink.New(jujudPath, filepath.Join(dir, name)) if err != nil && !os.IsExist(err) { return err } } return nil }