FileSize = -2 for invalid directory

Windows specific forum
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

FileSize = -2 for invalid directory

Post 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?)
acreis
Enthusiast
Enthusiast
Posts: 182
Joined: Fri Jun 01, 2012 12:20 am

Re: FileSize = -2 for invalid directory

Post 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   
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: FileSize = -2 for invalid directory

Post by BarryG »

Cool, so it's a confirmed bug with FileSize(), then. Thanks!

Image
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: FileSize = -2 for invalid directory

Post 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.
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: FileSize = -2 for invalid directory

Post 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\
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: FileSize = -2 for invalid directory

Post by Demivec »

If the name ends with a slash it is treated as a directory in Windows.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: FileSize = -2 for invalid directory

Post 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 :)
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: FileSize = -2 for invalid directory

Post 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.
Fred
Administrator
Administrator
Posts: 16623
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: FileSize = -2 for invalid directory

Post 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).
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: FileSize = -2 for invalid directory

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply