Skip to content

Commit

Permalink
FAST AF ljinux.api.isdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
bill88t committed Sep 4, 2023
1 parent a6e2705 commit 24e0dbd
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions source/ljinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def clear_process_storage() -> None:
import busio
from microcontroller import cpu
from storage import remount, VfsFat, mount, getmount
from os import chdir, rmdir, mkdir, sync, getcwd, listdir, remove, sync
from os import chdir, rmdir, mkdir, sync, getcwd, listdir, remove, sync, stat
from math import trunc
import time

Expand Down Expand Up @@ -693,31 +693,21 @@ def isdir(dirr, rdir=None):
"""
res = 2

bckdir = getcwd()
while dirr.endswith("/") and (dirr != "/"):
dirr = dirr[:-1]
if rdir is None:
if "/" in dirr and dirr not in ["/", "&/"]:
rdir = dirr[: dirr.rfind("/")]
if not len(rdir):
rdir = "/"
dirr = dirr[dirr.rfind("/") + 1 :]
cddd = ljinux.api.betterpath(rdir)
else:
cddd = bckdir
else:
cddd = ljinux.api.betterpath(rdir)
dirr = ljinux.api.betterpath(dirr)
chdir(cddd) # We assume ref dir exists
olddir = getcwd()
if rdir is not None:
chdir(ljinux.api.betterpath(rdir))
del rdir
try:
chdir(dirr)
chdir(bckdir)
res = 1 # It's a dir
except OSError: # we are still in cddd
if dirr in listdir():
if stat(ljinux.api.betterpath(dirr))[0] == 32768:
res = 0
chdir(bckdir)
del cddd, rdir, dirr, bckdir
else:
res = 1
except OSError:
pass
chdir(olddir)
del olddir
return res

def betterpath(back=None):
Expand Down

0 comments on commit 24e0dbd

Please sign in to comment.