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

os: Lstat failed on C:\$Recycle.Bin #13763

Closed
chai2010 opened this issue Dec 29, 2015 · 6 comments
Closed

os: Lstat failed on C:\$Recycle.Bin #13763

chai2010 opened this issue Dec 29, 2015 · 6 comments

Comments

@chai2010
Copy link
Contributor

go version devel +c7c7c70 Mon Dec 28 01:12:24 2015 +0000 windows/amd64

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"
)

func main() {
    f, err := os.Open("C:")
    if err != nil {
        log.Fatal("os.Open:", err)
    }
    defer f.Close()

    names, err := f.Readdirnames(-1)
    if err != nil {
        log.Fatal("f.Readdirnames:", err)
    }

    for i, s := range names {
        fmt.Println(i, s)
        filename := filepath.Join("C:", s)
        fileInfo, err := os.Lstat(filename)
        if err != nil {
            log.Fatal("os.Lstat:", err)
        }
        fmt.Println("\t", fileInfo.IsDir())
    }
}

error:
os.Lstat:GetFileAttributesEx C:$Recycle.Bin: The system cannot find the file specified.

@mikioh mikioh changed the title os.Lstat failed on C:\$Recycle.Bin os: Lstat failed on C:\$Recycle.Bin Dec 29, 2015
@mattn
Copy link
Member

mattn commented Dec 29, 2015

@chai2010 because you don't append last backslash for Join().

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"
)

func main() {
    f, err := os.Open(`C:\`)
    if err != nil {
        log.Fatal("os.Open:", err)
    }
    defer f.Close()

    names, err := f.Readdirnames(-1)
    if err != nil {
        log.Fatal("f.Readdirnames:", err)
    }

    for i, s := range names {
        fmt.Println(i, s)
        filename := filepath.Join(`C:\`, s)
        fileInfo, err := os.Lstat(filename)
        if err != nil {
            log.Fatal("os.Lstat:", err)
        }
        fmt.Println("\t", fileInfo.IsDir())
    }
}

But it will be fail with some system-file on C:\

2015/12/29 20:57:39 os.Lstat:GetFileAttributesEx C:\hiberfil.sys: The process cannot access the file because it is being used by another process.
exit status 1

@chai2010
Copy link
Contributor Author

@mattn thanks.
I want to say the os.Lstat can't process some system-file.
I need use mklink /J dst d: to mount some disk to subdir, but the filepath.Walk failed, if the mounted dir has some system-file(such as $Recycle.Bin).

@mattn
Copy link
Member

mattn commented Dec 29, 2015

On my windows10, my modified code above display like below.

0 $Recycle.Bin
     true
1 $WINDOWS.~BT
     true
2 $Windows.~WS
     true
3 bootmgr
     false
4 BOOTNXT
     false
5 dev
     true
6 Documents and Settings
     true
7 hiberfil.sys
2015/12/29 22:20:22 os.Lstat:GetFileAttributesEx C:\hiberfil.sys: The process cannot access the file because it is being used by another process.
exit status 1

So $Recycle.Bin is not related on this issue, I guess.

@alexbrainman
Copy link
Member

@chai2010, what do you think the problem is?

I think the problem is that os.Open("C:") currently opens root directory of C: (C:\). I think we should change the code so it opens "current directory" on C: (C:.), just like "dir c:" command does. What do you think?

I suspect if you run your example program from root directory on C:, it will run just fine.

Alex

@chai2010
Copy link
Contributor Author

@mattn I has the same error as you: os.Lstat failed on C:\hiberfil.sys.

@alexbrainman I donot know the reason. I think there are three problems:

  1. C:\ is not same as C:
  2. os.Lstat failed on C:\hiberfil.sys, I donot know the reason.
  3. filepath.Walk failed: C:\$Recycle.Bin\S-1-5-21-1654714224-3836454192-1760979379-1000\$R52OC9K\System Volume Information: Access is denied.

The filepath.Walk test code:

func main() {
    const root = `C:\`
    err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
        if err != nil {
            return err
        }
        fmt.Printf("filepath.Walk: path = %s; isdir = %v\n", path, fi.IsDir())
        return nil
    })
    if err != nil {
        log.Fatal("filepath.Walk:", err)
    }
    fmt.Println("Done")
}

@gopherbot
Copy link

CL https://golang.org/cl/18184 mentions this issue.

@golang golang locked and limited conversation to collaborators Jan 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants