Page 1 of 1

FileSize = -2 for invalid directory

Posted: Sat Dec 18, 2021 7:28 am
by BarryG
PureBasic v5.73 LTS and v6.00 Beta 1. This returns -2 for an invalid directory. It seems FileSize() is ignoring the double slashes, when it shouldn't.

Code: Select all

Debug FileSize("c:\windows\\\\\system32\\\\\") ; -2 (why valid?)

Re: FileSize = -2 for invalid directory

Posted: Sat Dec 18, 2021 10:10 am
by acreis

Code: Select all

Debug "c:\windows\\\\\system32\\\\\"

If FileSize("c:\windows\\\\\system32\\\\\") = -2
  Debug "Purebasic thinks it's a DIRECTORY"
EndIf   

#GetFileExInfoStandard = 0
GetFileAttributesEx_("c:\windows\\\\\system32\\\\\", #GetFileExInfoStandard, r.WIN32_FILE_ATTRIBUTE_DATA )

If  r\dwFileAttributes = #FILE_ATTRIBUTE_DIRECTORY
  Debug "WINAPI thinks it's a DIRECTORY ... too"
Else
  Debug "Shame on you, Purebasic"
EndIf   

Re: FileSize = -2 for invalid directory

Posted: Sat Dec 18, 2021 11:18 am
by BarryG
Cool, so it's a confirmed bug with FileSize(), then. Thanks!

Image

Re: FileSize = -2 for invalid directory

Posted: Sat Dec 18, 2021 12:09 pm
by Axolotl
sorry, for my Windows 10 with PureBasic 5.73 LTS (x64) i cannot confirm your results. :oops:

I copied your code to a new tab and compiled it. After that I added another (similar) check procedure.

Code: Select all

Debug "c:\windows\\\\\system32\\\\\"

If FileSize("c:\windows\\\\\system32\\\\\") = -2
  Debug "Purebasic thinks it's a DIRECTORY"
EndIf   

#GetFileExInfoStandard = 0
GetFileAttributesEx_("c:\windows\\\\\system32\\\\\", #GetFileExInfoStandard, r.WIN32_FILE_ATTRIBUTE_DATA )

If  r\dwFileAttributes = #FILE_ATTRIBUTE_DIRECTORY
  Debug "WINAPI thinks it's a DIRECTORY ... too"
Else
  Debug "Shame on you, Purebasic"
EndIf   

;:: the standard purebasic procedure returns the flag as well 
If GetFileAttributes("c:\windows\\\\\system32\\\\\") & #FILE_ATTRIBUTE_DIRECTORY 
  Debug "GetFileAttributes() thinks it's a DIRECTORY ... too" 
EndIf    

Debug "the end" 

; == Debug Output == 
; c:\windows\\\\\system32\\\\\
; Purebasic thinks it's a DIRECTORY
; WINAPI thinks it's a DIRECTORY ... too
; GetFileAttributes() thinks it's a DIRECTORY ... too
; the end 


With the following windows procedure I got this results:

Code: Select all

  
Define buffer$ = Space(#MAX_PATH) 
Debug GetFullPathName_("c:\windows\\\\\system32\\\\\", #MAX_PATH, @buffer$, 0)    

Debug "  '"+buffer$+"'"
;:: == Debug Output ==
;::20
;::  'c:\windows\system32\'


I tested the directory with pure basic example code (i thought this would be a good idea)

Code: Select all

countItems =  0  ;:: save some time 
maxItems   = 10  ;:: do not show all files and directories  

  ;:: the example from Help with the special directory 
  If ExamineDirectory(0, "c:\windows\\\\\system32\\\\\", "*.*")  
Debug "begin" 
    While NextDirectoryEntry(0) 
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        Type$ = "[File] "
        Size$ = " (Size: " + DirectoryEntrySize(0) + ")"
countItems + 1
      Else
        Type$ = "[Directory] "
        Size$ = "" ; A directory doesn't have a size
      EndIf
      
      Debug Type$ + DirectoryEntryName(0) + Size$
If countItems > maxItems  
  Break
EndIf 
    Wend
Debug "finish" 
    FinishDirectory(0)
  EndIf

But it works differently than expected.

Re: FileSize = -2 for invalid directory

Posted: Tue Dec 21, 2021 1:16 am
by DeanH
I get Directory regardless of which approach is used. Using PB6.00 Beta 1 on Win 10.

I see the point. The double-double blackslashes appear to be reduced to one backslash. Technically this shouldn't be allowed but I suspect the Win API is doing this.

Code: Select all

Debug "c:\windows\\\\\system32\\\\\"

If FileSize("c:\windows\\\\\system32\\\\\") = -2
	Debug "Purebasic FileSize() thinks it's a DIRECTORY"
EndIf   

;:: the standard purebasic procedure returns the flag as well 
If GetFileAttributes("c:\windows\\\\\system32\\\\\") & #FILE_ATTRIBUTE_DIRECTORY 
	Debug "GetFileAttributes() thinks it's a DIRECTORY"
EndIf

; Win API method
#GetFileExInfoStandard = 0
GetFileAttributesEx_("c:\windows\\\\\system32\\\\\", #GetFileExInfoStandard, r.WIN32_FILE_ATTRIBUTE_DATA )
If  r\dwFileAttributes = #FILE_ATTRIBUTE_DIRECTORY
	Debug "WINAPI thinks it's a DIRECTORY ... too"
Else
	Debug "Shame on you, Purebasic"
EndIf   

Define buffer$ = Space(#MAX_PATH) 
GetFullPathName_("c:\windows\\\\\system32\\\\\", #MAX_PATH, @buffer$, 0)
Debug "GetFullPathName_() shows "+buffer$

; == Debug Output == 
; c:\windows\\\\\system32\\\\\
; Purebasic FileSize() thinks it's a DIRECTORY
; GetFileAttributes() thinks it's a DIRECTORY ... too
; WINAPI thinks it's a DIRECTORY ... too
; GetFullPathName_() shows c:\windows\system32\

Re: FileSize = -2 for invalid directory

Posted: Tue Dec 21, 2021 2:32 am
by Demivec
If the name ends with a slash it is treated as a directory in Windows.

Re: FileSize = -2 for invalid directory

Posted: Tue Dec 21, 2021 6:35 am
by kenmo
Consecutive backslashes are fine, they don't make a path invalid. It's just not the shortest possible, canonical path.

See some discussion here:
https://superuser.com/questions/1412182 ... ath-or-url
https://stackoverflow.com/questions/330 ... dows-paths
Most operating systems allow the inclusion of multiple slashes between file name or directory components of a file path. This is true of both Windows and most *nix operating systems. The only exception is slashes used in conjunction with a UNC, where only two backward slashes are allowed with the UNC (\\?\UNC\).
Open a terminal and try "cd C:\\\\\\\\Windows", it works fine :)

Re: FileSize = -2 for invalid directory

Posted: Tue Dec 21, 2021 9:52 am
by BarryG
kenmo wrote: Tue Dec 21, 2021 6:35 amConsecutive backslashes are fine, they don't make a path invalid.
Okay, but what about on Mac/Linux then? If they don't support them, then the Windows version of FileSize() needs to disallow them too, since the language is cross-platform.

Re: FileSize = -2 for invalid directory

Posted: Tue Jan 18, 2022 5:40 pm
by Fred
As we use regular OS API in the commands, it makes sense to accept this kind of oddities (it's a valid directory according to Windows).

Re: FileSize = -2 for invalid directory

Posted: Tue Jan 18, 2022 5:58 pm
by NicTheQuick
BarryG wrote: Tue Dec 21, 2021 9:52 am
kenmo wrote: Tue Dec 21, 2021 6:35 amConsecutive backslashes are fine, they don't make a path invalid.
Okay, but what about on Mac/Linux then? If they don't support them, then the Windows version of FileSize() needs to disallow them too, since the language is cross-platform.
You can use consecutive Slashes on Linux too:

Code: Select all

Debug FileSize("///home/nicolas/////////tmp")
Returns -2 because this is a directory. You can also do it in the terminal:

Code: Select all

nicolas@Rocky:~/.config$ LANG=C stat //////home/nicolas/////tmp
  File: //////home/nicolas/////tmp
  Size: 20480     	Blocks: 40         IO Block: 4096   directory
Device: fd00h/64768d	Inode: 54526167    Links: 61
Access: (0755/drwxr-xr-x)  Uid: ( 1000/ nicolas)   Gid: (  100/   users)
Access: 2021-03-29 14:48:04.537557642 +0200
Modify: 2022-01-03 20:09:19.075796438 +0100
Change: 2022-01-03 20:09:19.075796438 +0100
 Birth: 2021-03-29 14:48:04.537557642 +0200
You can also use them in your browser. Try to change the address of this page in the address bar and add some slashes.