filesystem.bmx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. SuperStrict
  2. Rem
  3. bbdoc: System/File system
  4. End Rem
  5. Module BRL.FileSystem
  6. ModuleInfo "Version: 1.13"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.13"
  12. ModuleInfo "History: Added SetFileTime"
  13. ModuleInfo "History: Added MaxIO file tree walking"
  14. ModuleInfo "History: 1.12"
  15. ModuleInfo "History: Added file tree walker"
  16. ModuleInfo "History: 1.11"
  17. ModuleInfo "History: Added optional parameter timetype to FileTime"
  18. ModuleInfo "History: 1.10"
  19. ModuleInfo "History: Module is now SuperStrict"
  20. ModuleInfo "History: 1.09 Release"
  21. ModuleInfo "History: Fixed RealPath breaking win32 //server paths"
  22. ModuleInfo "History: 1.08 Release"
  23. ModuleInfo "History: Rebuild for StdC chmod_ linkage"
  24. ModuleInfo "History: 1.07 Release"
  25. ModuleInfo "History: Fixed RealPath failing with 'hidden' dirs"
  26. ModuleInfo "History: 1.06 Release"
  27. ModuleInfo "History: 1.05 Release"
  28. ModuleInfo "History: Fixed Win32 CreateDir"
  29. ModuleInfo "History: 1.04 Release"
  30. ModuleInfo "History: Cleaned up FixPath and RealPath"
  31. ModuleInfo "History: Added optional resurse parameter to CreateDir"
  32. Import Pub.StdC
  33. Import BRL.BankStream
  34. Import "glue.c"
  35. Const FILETYPE_NONE:Int=0,FILETYPE_FILE:Int=1,FILETYPE_DIR:Int=2,FILETYPE_SYM:Int=3
  36. Const FILETIME_MODIFIED:Int=0,FILETIME_CREATED:Int=1,FILETIME_ACCESSED:Int=2
  37. Private
  38. Function _RootPath$( path$ )
  39. If MaxIO.ioInitialized Then
  40. Return "/"
  41. End If
  42. ?Win32
  43. If path.StartsWith( "//" )
  44. Return path[ ..path.Find( "/",2 )+1 ]
  45. EndIf
  46. Local i:Int=path.Find( ":" )
  47. If i<>-1 And path.Find( "/" )=i+1 Return path[..i+2]
  48. ?
  49. If path.StartsWith( "/" ) Return "/"
  50. End Function
  51. Function _IsRootPath:Int( path$ )
  52. Return path And _RootPath( path )=path
  53. End Function
  54. Function _IsRealPath:Int( path$ )
  55. Return _RootPath( path )<>""
  56. End Function
  57. ?Win32
  58. Function _CurrentDrive$()
  59. Local cd$=getcwd_()
  60. Local i:Int=cd.Find( ":" )
  61. If i<>-1 Return cd[..i]
  62. End Function
  63. ?
  64. Public
  65. Function FixPath( path$ Var,dirPath:Int=False )
  66. path=path.Replace("\","/")
  67. If Not MaxIO.ioInitialized Then
  68. ?Win32
  69. If path.StartsWith( "//" )
  70. If path.Find( "/",2 )=-1 path:+"/"
  71. Else
  72. Local i:Int=path.Find( ":" )
  73. If i<>-1 And ( i=path.length-1 Or path[i+1]<>Asc(":") )
  74. Local i2:Int=path.Find( "/" )
  75. If i2=-1 Or i2>i+1 path=path[..i+1]+"/"+path[i+1..]
  76. EndIf
  77. EndIf
  78. ?
  79. End If
  80. If dirPath And path.EndsWith( "/" )
  81. If Not _IsRootPath( path ) path=path[..path.length-1]
  82. EndIf
  83. End Function
  84. Rem
  85. bbdoc: Strips the directory from a file path
  86. End Rem
  87. Function StripDir$( path$ )
  88. FixPath path
  89. Local i:Int=path.FindLast( "/" )
  90. If i<>-1 Return path[i+1..]
  91. Return path
  92. End Function
  93. Rem
  94. bbdoc: Strips the extension from a file path
  95. End Rem
  96. Function StripExt$( path$ )
  97. FixPath path
  98. Local i:Int=path.FindLast( "." )
  99. If i<>-1 And path.Find( "/",i+1 )=-1 Return path[..i]
  100. Return path
  101. End Function
  102. Rem
  103. bbdoc: Strips the directory and extension from a file path
  104. End Rem
  105. Function StripAll$( path$ )
  106. Return StripDir( StripExt( path ) )
  107. End Function
  108. Rem
  109. bbdoc: Strips trailing slash from a file path
  110. about:
  111. #StripSlash will not remove the trailing slash from a 'root' path. For example, "/"
  112. or (on Win32 only) "C:/".
  113. End Rem
  114. Function StripSlash$( path$ )
  115. FixPath path
  116. If path.EndsWith( "/" ) And Not _IsRootPath( path ) path=path[..path.length-1]
  117. Return path
  118. End Function
  119. Rem
  120. bbdoc: Extracts the directory from a file path
  121. End Rem
  122. Function ExtractDir$( path$ )
  123. FixPath path
  124. If path="." Or path=".." Or _IsRootPath( path ) Return path
  125. Local i:Int=path.FindLast( "/" )
  126. If i=-1 Return ""
  127. If _IsRootPath( path[..i+1] ) i:+1
  128. Return path[..i]
  129. End Function
  130. Rem
  131. bbdoc: Extracts the extension from a file path
  132. End Rem
  133. Function ExtractExt$( path$ )
  134. FixPath path
  135. Local i:Int=path.FindLast( "." )
  136. If i<>-1 And path.Find( "/",i+1 )=-1 Return path[i+1..]
  137. End Function
  138. Rem
  139. bbdoc: Gets the Current Directory
  140. returns: The current directory
  141. End Rem
  142. Function CurrentDir$()
  143. If MaxIO.ioInitialized Then
  144. Return "/"
  145. End If
  146. Local path$=getcwd_()
  147. FixPath path
  148. Return path
  149. End Function
  150. Rem
  151. bbdoc: Gets the real, absolute path of a file path
  152. End Rem
  153. Function RealPath$( path$ )
  154. ?Win32
  155. If Not MaxIO.ioInitialized And path.StartsWith( "/" ) And Not path.StartsWith( "//" )
  156. path=_CurrentDrive()+":"+path
  157. EndIf
  158. ?
  159. FixPath path
  160. Local cd$=_RootPath( path )
  161. If cd
  162. If Not MaxIO.ioInitialized Then
  163. path=path[cd.length..]
  164. End If
  165. Else
  166. cd=CurrentDir()
  167. EndIf
  168. path:+"/"
  169. While path
  170. Local i:Int=path.Find( "/" )
  171. Local t$=path[..i]
  172. path=path[i+1..]
  173. Select t
  174. Case ""
  175. Case "."
  176. Case ".."
  177. If Not _IsRootPath( cd ) cd=cd[..cd.FindLast("/")]
  178. Default
  179. If Not cd.EndsWith( "/" ) cd:+"/"
  180. cd:+t
  181. End Select
  182. Wend
  183. Return cd
  184. End Function
  185. Rem
  186. bbdoc: Gets the file type
  187. returns: 0 if file at @path doesn't exist, FILETYPE_FILE (1) if the file is a plain file or FILETYPE_DIR (2) if the file is a directory
  188. End Rem
  189. Function FileType:Int( path$ )
  190. FixPath path
  191. If MaxIO.ioInitialized Then
  192. Local stat:SMaxIO_Stat
  193. If Not MaxIO.Stat(path, stat) Return 0
  194. Select stat._filetype
  195. Case EMaxIOFileType.REGULAR Return FILETYPE_FILE
  196. Case EMaxIOFileType.DIRECTORY Return FILETYPE_DIR
  197. End Select
  198. Else
  199. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  200. If stat_( path,Mode,size,mtime,ctime,atime ) Return 0
  201. Select Mode & S_IFMT_
  202. Case S_IFREG_ Return FILETYPE_FILE
  203. Case S_IFDIR_ Return FILETYPE_DIR
  204. End Select
  205. End If
  206. Return FILETYPE_NONE
  207. End Function
  208. Rem
  209. bbdoc: Gets file time
  210. returns: The time the file at @path was last modified.
  211. End Rem
  212. Function FileTime:Long( path$, timetype:Int=FILETIME_MODIFIED )
  213. FixPath path
  214. If MaxIO.ioInitialized Then
  215. Local stat:SMaxIO_Stat
  216. If Not MaxIO.Stat(path, stat) Return 0
  217. Select timetype
  218. Case FILETIME_CREATED
  219. Return stat._createtime
  220. Case FILETIME_MODIFIED
  221. Return stat._modtime
  222. Case FILETIME_ACCESSED
  223. Return stat._accesstime
  224. EndSelect
  225. Else
  226. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  227. If stat_( path,Mode,size,mtime,ctime,atime ) Return 0
  228. Select timetype
  229. Case FILETIME_CREATED
  230. Return ctime
  231. Case FILETIME_MODIFIED
  232. Return mtime
  233. Case FILETIME_ACCESSED
  234. Return atime
  235. EndSelect
  236. End If
  237. End Function
  238. Rem
  239. bbdoc: Sets the file modified or last accessed time.
  240. about: @time should be number of seconds since epoch.
  241. End Rem
  242. Function SetFileTime( path:String, time:Long, timeType:Int=FILETIME_MODIFIED)
  243. FixPath path
  244. If MaxIO.ioInitialized Then
  245. ' Not available
  246. Else
  247. Select timetype
  248. Case FILETIME_MODIFIED
  249. utime_(path, timeType, time)
  250. Case FILETIME_ACCESSED
  251. utime_(path, timeType, time)
  252. End Select
  253. End If
  254. End Function
  255. Rem
  256. bbdoc: Gets the file size
  257. returns: The size, in bytes, of the file at @path, or -1 if the file does not exist
  258. End Rem
  259. Function FileSize:Long( path$ )
  260. FixPath path
  261. If MaxIO.ioInitialized Then
  262. Local stat:SMaxIO_Stat
  263. If Not MaxIO.Stat(path, stat) Return -1
  264. Return stat._filesize
  265. Else
  266. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  267. If stat_( path,Mode,size,mtime,ctime,atime ) Return -1
  268. Return size
  269. End If
  270. End Function
  271. Rem
  272. bbdoc: Gets the file mode
  273. returns: The file mode flags
  274. End Rem
  275. Function FileMode:Int( path$ )
  276. FixPath path
  277. If Not MaxIO.ioInitialized Then
  278. Local Mode:Int,size:Long,mtime:Int,ctime:Int,atime:Int
  279. If stat_( path,Mode,size,mtime,ctime,atime ) Return -1
  280. Return Mode & 511
  281. End If
  282. End Function
  283. Rem
  284. bbdoc: Sets file mode
  285. End Rem
  286. Function SetFileMode( path$,Mode:Int )
  287. FixPath path
  288. If Not MaxIO.ioInitialized Then
  289. chmod_ path,Mode
  290. End If
  291. End Function
  292. Rem
  293. bbdoc: Creates a file
  294. returns: #True if successful
  295. End Rem
  296. Function CreateFile:Int( path$ )
  297. FixPath path
  298. If MaxIO.ioInitialized Then
  299. MaxIO.DeletePath(path)
  300. Local t:Byte Ptr = MaxIO.OpenWrite(path)
  301. If t MaxIO.Close(t)
  302. Else
  303. remove_ path
  304. Local t:Byte Ptr=fopen_( path,"wb" )
  305. If t fclose_ t
  306. End If
  307. If FileType( path )=FILETYPE_FILE Return True
  308. End Function
  309. Rem
  310. bbdoc: Creates a directory
  311. returns: #True if successful
  312. about:
  313. If @recurse is #True, any required subdirectories are also created.
  314. End Rem
  315. Function CreateDir:Int( path$,recurse:Int=False )
  316. FixPath path,True
  317. If MaxIO.ioInitialized Then
  318. Return MaxIO.MkDir(path)
  319. Else
  320. If Not recurse
  321. mkdir_ path,1023
  322. Return FileType(path)=FILETYPE_DIR
  323. EndIf
  324. Local t$
  325. path=RealPath(path)+"/"
  326. While path
  327. Local i:Int=path.find("/")+1
  328. t:+path[..i]
  329. path=path[i..]
  330. Select FileType(t)
  331. Case FILETYPE_DIR
  332. Case FILETYPE_NONE
  333. Local s$=StripSlash(t)
  334. mkdir_ StripSlash(s),1023
  335. If FileType(s)<>FILETYPE_DIR Return False
  336. Default
  337. Return False
  338. End Select
  339. Wend
  340. Return True
  341. End If
  342. End Function
  343. Rem
  344. bbdoc: Deletes a file
  345. returns: #True if successful
  346. End Rem
  347. Function DeleteFile:Int( path$ )
  348. FixPath path
  349. If MaxIO.ioInitialized Then
  350. MaxIO.DeletePath(path)
  351. Else
  352. remove_ path
  353. End If
  354. Return FileType(path)=FILETYPE_NONE
  355. End Function
  356. Rem
  357. bbdoc: Renames a file
  358. returns: #True if successful
  359. End Rem
  360. Function RenameFile:Int( oldpath$,newpath$ )
  361. If MaxIO.ioInitialized Then
  362. Return False
  363. End If
  364. FixPath oldpath
  365. FixPath newpath
  366. Return rename_( oldpath,newpath)=0
  367. End Function
  368. Rem
  369. bbdoc: Copies a file
  370. returns: #True if successful
  371. End Rem
  372. Function CopyFile:Int( src$,dst$ )
  373. Local in:TStream=ReadStream( src ),ok:Int
  374. If in
  375. Local out:TStream=WriteStream( dst )
  376. If out
  377. Try
  378. CopyStream in,out
  379. ok=True
  380. Catch ex:TStreamWriteException
  381. End Try
  382. out.Close
  383. EndIf
  384. in.Close
  385. EndIf
  386. Return ok
  387. End Function
  388. Rem
  389. bbdoc: Copies a directory
  390. returns: #True if successful
  391. End Rem
  392. Function CopyDir:Int( src$,dst$ )
  393. Function CopyDir_:Int( src$,dst$ )
  394. If FileType( dst )=FILETYPE_NONE CreateDir dst
  395. If FileType( dst )<>FILETYPE_DIR Return False
  396. For Local file$=EachIn LoadDir( src )
  397. Select FileType( src+"/"+file )
  398. Case FILETYPE_DIR
  399. If Not CopyDir_( src+"/"+file,dst+"/"+file ) Return False
  400. Case FILETYPE_FILE
  401. If Not CopyFile( src+"/"+file,dst+"/"+file ) Return False
  402. End Select
  403. Next
  404. Return True
  405. End Function
  406. FixPath src
  407. If FileType( src )<>FILETYPE_DIR Return False
  408. FixPath dst
  409. Return CopyDir_( src,dst )
  410. End Function
  411. Rem
  412. bbdoc: Deletes a directory
  413. returns: #True if successful
  414. about: Set @recurse to #True to delete all subdirectories and files recursively -
  415. but be careful!
  416. End Rem
  417. Function DeleteDir:Int( path$,recurse:Int=False )
  418. FixPath path,True
  419. If recurse
  420. Local dir:Byte Ptr=ReadDir( path )
  421. If Not dir Return False
  422. Repeat
  423. Local t$=NextFile( dir )
  424. If t="" Exit
  425. If t="." Or t=".." Continue
  426. Local f$=path+"/"+t
  427. Select FileType( f )
  428. Case 1 DeleteFile f
  429. Case 2 DeleteDir f,True
  430. End Select
  431. Forever
  432. CloseDir dir
  433. EndIf
  434. rmdir_ path
  435. If FileType( path )=0 Return True
  436. End Function
  437. Rem
  438. bbdoc: Changes the current directory
  439. returns: True if successful
  440. End Rem
  441. Function ChangeDir:Int( path$ )
  442. If MaxIO.ioInitialized Then
  443. Return False
  444. Else
  445. FixPath path,True
  446. If chdir_( path )=0 Return True
  447. End If
  448. End Function
  449. Rem
  450. bbdoc: Opens a directory
  451. returns: A directory handle, or #Null if the directory does not exist
  452. about: Use #NextFile to get the next file in the directory.
  453. The directory must be closed with #CloseDir.
  454. End Rem
  455. Function ReadDir:Byte Ptr( path$ )
  456. FixPath path,True
  457. If MaxIO.ioInitialized Then
  458. Return bmx_blitzio_readdir(path)
  459. Else
  460. Return opendir_( path )
  461. End If
  462. End Function
  463. Rem
  464. bbdoc: Returns the next file in a directory
  465. returns: File name of next file in the directory opened using #ReadDir, or an empty #String if there are no more files to read.
  466. End Rem
  467. Function NextFile$( dir:Byte Ptr )
  468. If MaxIO.ioInitialized Then
  469. Return bmx_blitzio_nextFile(dir)
  470. Else
  471. Return readdir_( dir )
  472. End If
  473. End Function
  474. Rem
  475. bbdoc: Closes a directory.
  476. about: Closes a directory opened with #ReadDir.
  477. End Rem
  478. Function CloseDir( dir:Byte Ptr )
  479. If MaxIO.ioInitialized Then
  480. bmx_blitzio_closeDir(dir)
  481. Else
  482. closedir_ dir
  483. End If
  484. End Function
  485. Rem
  486. bbdoc: Loads a directory
  487. returns: A string array containing contents of @dir
  488. about: The @skip_dots parameter, if true, removes the '.' (current) and '..'
  489. (parent) directories from the returned array.
  490. End Rem
  491. Function LoadDir$[]( dir$,skip_dots:Int=True )
  492. FixPath dir,True
  493. Local d:Byte Ptr=ReadDir( dir )
  494. If Not d Return Null
  495. Local i$[100],n:Int
  496. Repeat
  497. Local f$=NextFile( d )
  498. If Not f Exit
  499. If skip_dots And (f="." Or f="..") Continue
  500. If n=i.length i=i[..n+100]
  501. i[n]=f
  502. n=n+1
  503. Forever
  504. CloseDir d
  505. Return i[..n]
  506. End Function
  507. Rem
  508. bbdoc: Opens a file for input and/or output.
  509. about:
  510. This command is similar to the #OpenStream command but will attempt
  511. to cache the contents of the file to ensure serial streams such as
  512. http: based url's are seekable. Use the #CloseStream command when
  513. finished reading and or writing to a Stream returned by #OpenFile.
  514. End Rem
  515. Function OpenFile:TStream( url:Object,readable:Int=True,writeable:Int=True )
  516. Local stream:TStream=OpenStream( url,readable,writeable )
  517. If Not stream Return Null
  518. If stream.Pos()=-1 Return TBankStream.Create( TBank.Load(stream) )
  519. Return stream
  520. End Function
  521. Rem
  522. bbdoc: Opens a file For Input.
  523. about:
  524. This command is similar to the #ReadStream command but will attempt
  525. to cache the contents of the file to ensure serial streams such as
  526. http: based url's are seekable. Use the #CloseStream command when
  527. finished reading and or writing to a Stream returned by #OpenFile.
  528. End Rem
  529. Function ReadFile:TStream( url:Object )
  530. Return OpenFile( url,True,False )
  531. End Function
  532. Rem
  533. bbdoc: Opens a file for output.
  534. about:
  535. This command is identical to the #WriteStream command.
  536. End Rem
  537. Function WriteFile:TStream( url:Object )
  538. Return OpenFile( url,False,True )
  539. End Function
  540. Rem
  541. bbdoc: Closes a file stream.
  542. about:
  543. After performing file operations on an open file make sure to
  544. close the file stream with either #CloseFile or the identical
  545. #CloseStream command.
  546. End Rem
  547. Function CloseFile( stream:TStream )
  548. stream.Close
  549. End Function
  550. Rem
  551. bbdoc: Walks a file tree.
  552. End Rem
  553. Function WalkFileTree:Int(path:String, fileWalker:IFileWalker, options:EFileWalkOption = EFileWalkOption.None, maxDepth:Int = 0)
  554. FixPath(path)
  555. If MaxIO.ioInitialized Then
  556. If FileType(path) = FILETYPE_DIR Then
  557. Return FSWalkFileTree(path, fileWalker, options, 0, maxDepth)
  558. End If
  559. Else
  560. Return bmx_filesystem_walkfiletree(path, _walkfile, fileWalker, options, maxDepth)
  561. End If
  562. End Function
  563. Rem
  564. bbdoc: An interface for file tree traversal.
  565. End rem
  566. Interface IFileWalker
  567. Rem
  568. bbdoc: Called once for each file/folder traversed.
  569. about: Return EFileWalkResult.OK to continue the tree traversal, or EFileWalkResult.Terminate to exit early.
  570. The contents of @attributes is only valid for the duration of the call.
  571. End Rem
  572. Method WalkFile:EFileWalkResult(attributes:SFileAttributes Var)
  573. End Interface
  574. Rem
  575. bbdoc: File attributes
  576. End rem
  577. Struct SFileAttributes
  578. ?win32
  579. Field StaticArray name:Short[8192]
  580. ?not win32
  581. Field StaticArray name:Byte[8192]
  582. ?
  583. Field fileType:Short
  584. Field depth:Short
  585. Rem
  586. bbdoc: The size, in bytes, of the file.
  587. End Rem
  588. Field size:ULong
  589. Field creationTime:Int
  590. Field modifiedTime:Int
  591. Rem
  592. bbdoc: Returns the name of the file/directory.
  593. End rem
  594. Method GetName:String()
  595. ?win32
  596. Return String.FromWString(name)
  597. ?not win32
  598. Return String.FromUTF8String(name)
  599. ?
  600. End Method
  601. Method IsRegularFile:Int()
  602. Return fileType = FILETYPE_FILE
  603. End Method
  604. Method IsDirectory:Int()
  605. Return fileType = FILETYPE_DIR
  606. End Method
  607. Method IsSymbolicLink:Int()
  608. Return fileType = FILETYPE_SYM
  609. End Method
  610. End Struct
  611. Rem
  612. bbdoc:
  613. End rem
  614. Enum EFileWalkOption
  615. None
  616. FollowLinks
  617. End Enum
  618. Rem
  619. bbdoc:
  620. End rem
  621. Enum EFileWalkResult
  622. OK
  623. Terminate
  624. SkipSubtree
  625. SkipSiblings
  626. End Enum
  627. Private
  628. Function _walkFile:EFileWalkResult(fileWalker:IFileWalker, attributes:SFileAttributes Var) { nomangle }
  629. Return fileWalker.WalkFile(attributes)
  630. End Function
  631. Function FSWalkFileTree:Int(dir:string, fileWalker:IFileWalker, options:EFileWalkOption, depth:Int, maxDepth:Int)
  632. Local attributes:SFileAttributes
  633. ApplyAttributes(dir, depth, VarPtr attributes)
  634. Local res:EFileWalkResult = fileWalker.WalkFile(attributes)
  635. If res = EFileWalkResult.Terminate Then
  636. Return 1
  637. End If
  638. Local d:Byte Ptr = ReadDir(dir)
  639. If d Then
  640. Local f:String = NextFile(d)
  641. While f
  642. Local path:String = dir + "/" + f
  643. If ApplyAttributes(path, depth + 1, VarPtr attributes) Then
  644. If attributes.fileType = FILETYPE_DIR Then
  645. Local ret:Int = FSWalkFileTree(path, fileWalker, options, depth + 1, maxDepth)
  646. If ret Then
  647. CloseDir(d)
  648. Return ret
  649. End If
  650. Else
  651. res = fileWalker.WalkFile(attributes)
  652. If res = EFileWalkResult.Terminate Then
  653. CloseDir(d)
  654. Return 1
  655. End If
  656. End If
  657. End IF
  658. f = NextFile(d)
  659. Wend
  660. CloseDir(d)
  661. End If
  662. End Function
  663. Function ApplyAttributes:Int(path:String, depth:Int, attributes:SFileAttributes Ptr)
  664. Local stat:SMaxIO_Stat
  665. If Not MaxIO.Stat(path, stat) Then
  666. Return False
  667. End If
  668. Select stat._filetype
  669. Case EMaxIOFileType.REGULAR
  670. attributes.fileType = FILETYPE_FILE
  671. Case EMaxIOFileType.DIRECTORY
  672. attributes.fileType = FILETYPE_DIR
  673. Case EMaxIOFileType.SYMLINK
  674. attributes.fileType = FILETYPE_SYM
  675. End Select
  676. Local length:Size_T = 8192
  677. ?win32
  678. path.ToWStringBuffer(attributes.name, length)
  679. ?not win32
  680. path.ToUTF8StringBuffer(attributes.name, length)
  681. ?
  682. attributes.depth = depth
  683. attributes.size = stat._filesize
  684. attributes.creationTime = stat._createtime
  685. attributes.modifiedTime = stat._modtime
  686. Return True
  687. End Function
  688. Extern
  689. Function bmx_filesystem_walkfiletree:Int(path:String, callback:EFileWalkResult(fileWalker:IFileWalker, attributes:SFileAttributes Var), walker:IFileWalker, options:EFileWalkOption, maxDepth:Int)
  690. End Extern