Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tilde expansion work on Windows #27

Merged
merged 2 commits into from Sep 29, 2014
Merged

Conversation

gabriel-samfira
Copy link
Contributor

  • use Home() to return users home on Windows as well
  • only match strings starting with tilde
  • fix tests on Windows

)

// UserHomeDir returns the home directory for the specified user, or the
// home directory for the current user if the specified user is empty.
func UserHomeDir(userName string) (homeDir string, err error) {
var u *user.User
var u string
if userName == "" {
// TODO (wallyworld) - fix tests on Windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole comment seems out of place now, since the code part it was refering to just dissapeared, could you please take a look at it and make sure its still valid? it does not seem to me that we are doing anything out of the ordinary, moreover I dont see a UserHomeDir defined in windows speciffic code, also the code below could be outside the else (but that is just me being picky)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserHomeDir() is not really platform specific. We need to avoid user.Current() on windows as well now, so we can patch it during tests. That's why I removed that bit. The comment is stil relevant as we still use Home() instead of user.Current().Home. I can remove the TODO, but as of yet, the tests are still not fully working on Windows. :)

The getHomeDir() function does have windows specific stuff to get the home dir of a different user. Unfortunately, Go cannot get the home directory of a different user using user.Lookup(). So I had to get it from the registry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the else can be removed without a problem at this point.

@@ -113,6 +107,8 @@ func AtomicWriteFileAndChange(filename string, contents []byte, change func(*os.
defer func() {
if err != nil {
// Don't leave the temp file lying around on error.
// Close the file before removing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I get this right, this is due to the limitations of windows regarding file handlers, please add a comment to prevent anyone to ever remove this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quite right. Done

@@ -103,7 +106,7 @@ var atomicWriteFileTests = []struct {
summary: "atomic file write and change",
change: func(filename string, contents []byte) error {
chmodChange := func(f *os.File) error {
return f.Chmod(0700)
return os.Chmod(f.Name(), 0700)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont understand the reason behind this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileMod.Chmod() is not implemented on Windows unfortunately. os.Chmod() is. Will comment :)


func getHomeFromRegistry(sid string) (string, error) {
var h syscall.Handle
keyPath := fmt.Sprintf("Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\%s", sid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems rather hardcodded, this is always NT? I do believe that since XP every windows is NT based but I am not sure so better ask

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if its old enough not to have this, we probably should not support it :). The agent will probably only support windows 2008 and above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small comment for non windows folks would be nice ;)

@perrito666
Copy link
Contributor

Ok, with the new changes LGTM and now you need to get approval to my lgtm from @davecheney

@@ -13,6 +15,8 @@ const (
movefile_write_through = 0x8
)

var noSuchUser = "No mapping between account names and security IDs was done."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks different to our usual messages. Maybe better "no mapping between accound names and security id".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this error is returned by the operating system. I just match the error here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok.

@themue
Copy link
Contributor

themue commented Sep 12, 2014

LGTM, only minor comments.

@natefinch
Copy link
Contributor

LGTM, too

@natefinch
Copy link
Contributor

$$merge$$

natefinch added a commit that referenced this pull request Sep 29, 2014
Make tilde expansion work on Windows
@natefinch natefinch merged commit 462e9fd into juju:master Sep 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants