Unable to create a specific file in windows

508 views
Skip to first unread message

nilsocket

unread,
Oct 15, 2020, 6:57:09 PM10/15/20
to golang-nuts
var fileName = `408_-IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041-`
var dir1 = "a"

func main() {
createFile(dir1, fileName) // not working, any dir not working
createFile("", fileName) // working
createFile("c", "d") // working
}

func createFile(dir, filename string) {
os.MkdirAll(dir, os.ModePerm)

if f, err := os.Create(filepath.Join(dir, filename)); err != nil {
log.Println(err)
} else {
f.Close()
}
}

I was trying to create this file from yesterday, I went through windows documentation, but not able to find any solution.

I'm getting error: Linked picture

Filename length is 221
With dir: 223
It doesn't cross windows Max path length, I'm running windows 10 Home version 1909.
It doesn't contain any invalid characters.

It's working fine in Linux.

If there something wrong with what I'm doing or a bug?


Thank you

IMG_20201016_041646_compress18.jpg

Kurtis Rader

unread,
Oct 15, 2020, 10:30:03 PM10/15/20
to nilsocket, golang-nuts
Works for me. Of course the actual error you're seeing is "The system cannot find the path specified", not "Linked picture". You're not checking the error returned by `os.MkdirAll` so I would presume that creating the directory "a" is failing on your system. Try printing the error returned by `os.MkdirAll`.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0aaec817-0709-4d96-83f2-cc3d55439b10n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Kurtis Rader

unread,
Oct 15, 2020, 10:36:24 PM10/15/20
to nilsocket, golang-nuts
On Thu, Oct 15, 2020 at 7:29 PM Kurtis Rader <kra...@skepticism.us> wrote:
Works for me. Of course the actual error you're seeing is "The system cannot find the path specified", not "Linked picture". You're not checking the error returned by `os.MkdirAll` so I would presume that creating the directory "a" is failing on your system. Try printing the error returned by `os.MkdirAll`.

In fact, if I first create an empty file (not directory) named "a" on Windows 10 then your program fails for me with the error shown in your screen grab. Always check the error value returned by a function.

P.S., Please don't include screen grabs. Use a decent terminal that lets you copy/paste the text displayed within it.

nilsocket

unread,
Oct 15, 2020, 10:59:17 PM10/15/20
to golang-nuts
No, dir "a" is being created.

Intact, I was testing out big list of naughty strings, I didn't have problem with any string but this one.

Regarding error, I don't mean error is "linked picture", I mean, for error one can look at the picture which I have linked.

Kurtis Rader

unread,
Oct 15, 2020, 11:09:03 PM10/15/20
to nilsocket, golang-nuts
On Thu, Oct 15, 2020 at 8:00 PM nilsocket <nils...@gmail.com> wrote:
No, dir "a" is being created.

Yes, I understand you are trying to create the directory. My point is that you don't verify that the `os.MkdirAll` call succeeds. On my Windows 10 system your program works fine unless I create a file named "a" before running your program. Please modify your program to check the error value returned by `os.MkdirAll` and verify that it is nil.
 
Intact, I was testing out big list of naughty strings, I didn't have problem with any string but this one.

Regarding error, I don't mean error is "linked picture", I mean, for error one can look at the picture which I have linked.

That wasn't obvious at first glance. The indirection also wasn't necessary. All you had to do was write that the error was something like this (what I saw when I deliberately induced a failure by running "touch a"):

2020/10/15 19:32:16 open a\408_-IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000
114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#00000
39&#0000088&#0000083&#0000083&#0000039&#0000041-: The system cannot find the path specified.

nilsocket

unread,
Oct 16, 2020, 5:36:02 PM10/16/20
to golang-nuts
I was trying to create a package which sanitizes file name, for testing purposes, I choose blns.

I create a directory, named "blns" and sanitize names from blns and create those files in "blns" directory.

I have tested 516 strings in Linux, everything worked fine, for sake of cross-compatibility I choose to test in windows, then I got this problem.

I was able to create 515 files in "blns" directory but this one.

Since You were not able to reproduce, I will consider some kind of problem with windows OS and skip it, as this is error is from windows system calls.

Ian Lance Taylor

unread,
Oct 16, 2020, 8:14:11 PM10/16/20
to nilsocket, golang-nuts
On Fri, Oct 16, 2020 at 2:36 PM nilsocket <nils...@gmail.com> wrote:
>
> I was trying to create a package which sanitizes file name, for testing purposes, I choose blns.
>
> I create a directory, named "blns" and sanitize names from blns and create those files in "blns" directory.
>
> I have tested 516 strings in Linux, everything worked fine, for sake of cross-compatibility I choose to test in windows, then I got this problem.
>
> I was able to create 515 files in "blns" directory but this one.
>
> Since You were not able to reproduce, I will consider some kind of problem with windows OS and skip it, as this is error is from windows system calls.

I don't think you understand the earlier comment, so I will restate it.

Your sample code says

os.MkdirAll(dir, os.ModePerm)

Do not do that.

Instead write

if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Println(err)
return
}

Ian


> On Friday, October 16, 2020 at 8:39:03 AM UTC+5:30 Kurtis Rader wrote:
>>
>> On Thu, Oct 15, 2020 at 8:00 PM nilsocket <nils...@gmail.com> wrote:
>>>
>>> No, dir "a" is being created.
>>
>>
>> Yes, I understand you are trying to create the directory. My point is that you don't verify that the `os.MkdirAll` call succeeds. On my Windows 10 system your program works fine unless I create a file named "a" before running your program. Please modify your program to check the error value returned by `os.MkdirAll` and verify that it is nil.
>>
>>>
>>> Intact, I was testing out big list of naughty strings, I didn't have problem with any string but this one.
>>>
>>> Regarding error, I don't mean error is "linked picture", I mean, for error one can look at the picture which I have linked.
>>
>>
>> That wasn't obvious at first glance. The indirection also wasn't necessary. All you had to do was write that the error was something like this (what I saw when I deliberately induced a failure by running "touch a"):
>>
>> 2020/10/15 19:32:16 open a\408_-IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000
>> 114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#00000
>> 39&#0000088&#0000083&#0000083&#0000039&#0000041-: The system cannot find the path specified.
>>
>> --
>> Kurtis Rader
>> Caretaker of the exceptional canines Junior and Hank
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/bf97b64b-b243-4b67-8c7e-d6e7e3895aecn%40googlegroups.com.

Kurtis Rader

unread,
Oct 17, 2020, 12:24:35 AM10/17/20
to nilsocket, golang-nuts
On Fri, Oct 16, 2020 at 2:36 PM nilsocket <nils...@gmail.com> wrote:
I was trying to create a package which sanitizes file name, for testing purposes, I choose blns.

I create a directory, named "blns" and sanitize names from blns and create those files in "blns" directory.

Your problem reproduction program does not do that. It creates a directory named "a".
 
I have tested 516 strings in Linux, everything worked fine, for sake of cross-compatibility I choose to test in windows, then I got this problem.

I was able to create 515 files in "blns" directory but this one.

Since You were not able to reproduce, I will consider some kind of problem with windows OS and skip it, as this is error is from windows system calls.

I was able to reproduce your problem. Specifically, by creating a non-directory (such as a regular file) named "a". That causes the os.MkdirAll call to fail. Does the program you included in your first email actually fail? If so, what is the return value of the os.MkdirAll call?

Carlos Alberto Costa Beppler

unread,
Oct 17, 2020, 10:48:38 AM10/17/20
to golang-nuts
The problem is that on Windows API, to keep combability with previous versions, the maximum length for a full file name is 255 (may be a little less).

The a directory is being created relative to C:\Users\nil\Downloads\sanitize\temp, so the full file name is C:\Users\nil\Downloads\sanitize\temp\a\408_-IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041-.

This is 261 characters long. that is the reason to your program fail.

There are ways to overcome this limit that are documented on https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation.

One of them is prefix your file name with \\?\, but you must use the complete file name, not a relative one like you used.

The other is enabling long file names on windows and embed a manifest on your program.
Reply all
Reply to author
Forward
0 new messages