Skip to content

Commit 8342fc7

Browse files
committed
Fixed FATSTAT.LUA overallocating to disk image when -z option is used.
1 parent 040fe4a commit 8342fc7

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

demo/xtra/FATSTAT.LUA

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ function F16(f,o,s)
1212
f:seek("set",o)local fd,fat=f:read(s),{}
1313
for i=2,#fd-1,2 do local lo,hi=fd:byte(i),fd:byte(i+1)fat[((i-2)//2)+2]=lo+hi*256 end return fat end
1414
function FD(ts,rs,nf,sf,rd,sc)
15-
local cc=math.floor((ts-rs-(nf*sf)-rd)/sc)
16-
if cc<4085 then return 12 elseif cc<65525 then return 16 else return 32 end end
15+
local c,r=math.floor((ts-rs-(nf*sf)-rd)/sc)r=c<4085 and 12 or c<65525 and 16 or 32 return r,c end
1716
function CC(fat,sc)
1817
local h,c={},sc
1918
while c>=2 and c<0xFF8 do table.insert(h,c)c=fat[c] end return h
2019
end
2120
function FR(c)
2221
table.sort(c)local ranges,i={},1
23-
while i <=#c do
22+
while i<=#c do
2423
local s,j,f=c[i],i while j+1<=#c and c[j+1]==c[j]+1 do j=j+1 end f=c[j]
2524
if s==f then table.insert(ranges,string.format("<%d>",s))
2625
else table.insert(ranges,string.format("<%d-%d>",s,f))end
@@ -32,15 +31,15 @@ function DE(f,o,e,fat)
3231
if fb~=229 and(en:byte(12)&8)==0 then
3332
local n,x=en:sub(1,8):gsub("+$",""),en:sub(9,11):gsub("+$","")
3433
io.write(string.format("\t%8s %3s %s\n",n,x,FR(CC(fat,R16(en,27)))))end end end
35-
function FC(fat,f,st,s)
34+
function FC(fat,f,st,s,uc)
3635
local fr,z={},string.rep("\0",s)io.write("\n\tFree Space ")
37-
for i=2,#fat do if fat[i]==0 then table.insert(fr,i)if Z then local o=st+(i-2)*s f:seek("set",o)f:write(z)end end end print(FR(fr))end
36+
for i=2,uc+1 do if fat[i]==0 then table.insert(fr,i)if Z then local o=st+(i-2)*s f:seek("set",o)f:write(z)end end end print(FR(fr))end
3837
F={}for _,v in ipairs(arg)do if v=="-z" then Z=1 else table.insert(F,v)end end for _,file in ipairs(F)do
3938
local f=io.open(file,(Z and"r+b"or"rb"))if f then
4039
print(file..":")
4140
local b,o=f:read(512)o=b:sub(1,1):byte()if o==235 or o==233 then
42-
local bs,sc,rs,nf,re,ts,sf=R16(b,12),b:byte(14),R16(b,15),b:byte(17),R16(b,18),R16(b,19)or R32(b,32),R16(b,23)
43-
local rd,fo,fs,ro,ft=math.ceil(re*32/bs),rs*bs,sf*bs,(rs+nf*sf)*bs ft=FD(ts,rs,nf,sf,rd,bs)
41+
local bs,sc,rs,nf,re,ts,sf=R16(b,12),b:byte(14),R16(b,15),b:byte(17),R16(b,18),R16(b,20),R16(b,23) if ts==0 then ts=R32(b,32) end
42+
local rd,fo,fs,ro,ft,uc=math.ceil(re*32/bs),rs*bs,sf*bs,(rs+nf*sf)*bs ft,uc=FD(ts,rs,nf,sf,rd,sc)
4443
print(string.format("\t FAT type: FAT%s\n\t Bytes/sector: %d\n\tSectors/cluster: %d\n\n",ft,bs,sc))
4544
local fat=ft==12 and F12(f,fo,fs)or ft==16 and F16(f,fo,fs)or error("Non-FAT type")
46-
DE(f,ro,re,fat)FC(fat,f,(rs+(nf*sf)+rd)*bs,sc*bs)end f:close()print()end end
45+
DE(f,ro,re,fat)FC(fat,f,(rs+(nf*sf)+rd)*bs,sc*bs,uc)end f:close()print()end end

0 commit comments

Comments
 (0)