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: OpenFile creates incorrect file name on Windows #61408

Closed
epowsal opened this issue Jul 18, 2023 · 9 comments
Closed

os: OpenFile creates incorrect file name on Windows #61408

epowsal opened this issue Jul 18, 2023 · 9 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@epowsal
Copy link

epowsal commented Jul 18, 2023

windows os.OpenFile bug: if open path like this:X:/aa:393.txt the create filename will be "aa"
it should panic.
1.20.5

@qmuntal qmuntal added OS-Windows NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 18, 2023
@qmuntal
Copy link
Contributor

qmuntal commented Jul 18, 2023

@golang/windows

@qmuntal
Copy link
Contributor

qmuntal commented Jul 18, 2023

Could you share a code sample that we can use to reproduce this issue? Thanks.

@qmuntal qmuntal changed the title windows os.OpenFile bug: if open path like this:X:/aa:.txt the create filename will be aa os: OpenFile creates incorrect file name on Windows Jul 18, 2023
@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 18, 2023
@bcmills bcmills added this to the Backlog milestone Jul 18, 2023
@mauri870
Copy link
Member

I'm able to reproduce it using os.OpenFile:

package main

import (
        "log"
        "os"
)

func main() {
        f, err := os.OpenFile("C:/aa:393.txt", os.O_RDWR|os.O_CREATE, 0755)
        if err != nil {
                log.Fatal(err)
        }
        defer f.Close()
}

From the win docs:

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

    The following reserved characters:
        < (less than)
        > (greater than)
        : (colon)
        " (double quote)
        / (forward slash)
        \ (backslash)
        | (vertical bar or pipe)
        ? (question mark)
        * (asterisk)

Weirdly enough trying any of the other reserved characters result in a proper error:

open H:/aa*393.txt: The filename, directory name, or volume label syntax is incorrect.
open H:/aa>393.txt: The filename, directory name, or volume label syntax is incorrect.
open H:/aa"393.txt: The filename, directory name, or volume label syntax is incorrect.
open H:/aa?393.txt: The filename, directory name, or volume label syntax is incorrect.
open H:/aa|393.txt: The filename, directory name, or volume label syntax is incorrect.

I'll try to work on a fix.

@mauri870
Copy link
Member

mauri870 commented Jul 18, 2023

Seems like python also allows a : in a file path:

.\python.exe -c "open('C:/aa:393.txt', 'w').close()" # ok, results in a file named "aa"

.\python.exe -c "open('C:/aa?393.txt', 'w').close()" # bad
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [Errno 22] Invalid argument: 'H:/aa?393.txt'  

@ianlancetaylor
Copy link
Contributor

This kind of sounds like a Windows bug to me. Why does Windows not return an error if it can't create the file?

@ianlancetaylor ianlancetaylor removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 19, 2023
@seankhliao
Copy link
Member

It appears : addresses NTFS ALternate Data Streams, taking the form filename:stream, with :$DATA being the default stream.
So it's probably not a bug?

@mauri870
Copy link
Member

I think you are right @seankhliao, aparently that is a thing. tdil

@alexbrainman
Copy link
Member

@mauri870

I'm able to reproduce it using os.OpenFile:

Why would you use : in file name on Windows? You are looking for trouble.

Alex

@mauri870
Copy link
Member

@seankhliao I think we can close this issue. The problem seems to be the use of : in the file name, which is used by NTFS data streams, as mentioned by you in previous comments.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

7 participants