func (*SymlinkSuite) TestReplace(c *gc.C) { target, err := symlink.GetLongPathAsString(c.MkDir()) c.Assert(err, gc.IsNil) target_second, 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 = os.Stat(target_second) c.Assert(err, gc.IsNil) err = symlink.New(target, link) c.Assert(err, gc.IsNil) link_target, err := symlink.Read(link) c.Assert(err, gc.IsNil) c.Assert(link_target, gc.Equals, filepath.FromSlash(target)) err = symlink.Replace(link, target_second) c.Assert(err, gc.IsNil) link_target, err = symlink.Read(link) c.Assert(err, gc.IsNil) c.Assert(link_target, gc.Equals, filepath.FromSlash(target_second)) }
// ChangeAgentTools atomically replaces the agent-specific symlink // under dataDir so it points to the previously unpacked // version vers. It returns the new tools read. func ChangeAgentTools(dataDir string, agentName string, vers version.Binary) (*coretools.Tools, error) { tools, err := ReadTools(dataDir, vers) if err != nil { return nil, err } // build absolute path to toolsDir. Windows implementation of symlink // will check for the existance of the source file and error if it does // not exists. This is a limitation of junction points (symlinks) on NTFS toolPath := ToolsDir(dataDir, tools.Version.String()) toolsDir := ToolsDir(dataDir, agentName) err = symlink.Replace(toolsDir, toolPath) if err != nil { return nil, fmt.Errorf("cannot replace tools directory: %s", err) } return tools, nil }
// updateBackupMachineTag updates the paths that are stored in the backup // to the current machine. This path is tied, among other factors, to the // machine tag. // Eventually this will change: when backups hold relative paths. func updateBackupMachineTag(oldTag, newTag names.Tag) error { oldTagString := oldTag.String() newTagString := newTag.String() if oldTagString == newTagString { return nil } oldTagPath := path.Join(agent.DefaultPaths.DataDir, oldTagString) newTagPath := path.Join(agent.DefaultPaths.DataDir, newTagString) oldToolsDir := tools.ToolsDir(agent.DefaultPaths.DataDir, oldTagString) oldLink, err := filepath.EvalSymlinks(oldToolsDir) os.Rename(oldTagPath, newTagPath) newToolsDir := tools.ToolsDir(agent.DefaultPaths.DataDir, newTagString) newToolsPath := strings.Replace(oldLink, oldTagPath, newTagPath, -1) err = symlink.Replace(newToolsDir, newToolsPath) return errors.Annotatef(err, "cannot set the new tools path") }