Jump to content




fs.getFreeSpace not working for some reson?


7 replies to this topic

#1 Ta©ti_Tac0Z

  • Members
  • 59 posts

Posted 13 August 2018 - 11:41 AM

file = fs.open("/test.txt", "wb")
print(fs.getFreeSpace("/"))
file.write(23)
print(fs.getFreeSpace("/"))
file.write(90)
print(fs.getFreeSpace("/"))
file.write(54)
print(fs.getFreeSpace("/"))
file.close()
print(fs.getFreeSpace("/"))

the number remains the same even tho the avlable space should go down! (in my game anyway)

am i missunderstanding the use of fs.getFreeSpace ?

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 August 2018 - 11:46 AM

When you request a write through a file handle, the data doesn't actually go to disk immediately - instead, it goes into a buffer. Typically the buffer contents are only written when you either flush or close the handle.

On top of that, files take up a minimum of 512 bytes.

#3 Ta©ti_Tac0Z

  • Members
  • 59 posts

Posted 13 August 2018 - 11:58 AM

View PostBomb Bloke, on 13 August 2018 - 11:46 AM, said:

On top of that, files take up a minimum of 512 bytes.

ummm under testing of my video format i came all the up two 975KB's am i missunderstanding what you're saying?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 August 2018 - 12:28 PM

Sounds like you're reading "minimum" as "maximum". Files can certainly take up more than 512 bytes of storage, but they can't take up any less.

This means, for example, that if you have a 10 byte file (which consumes 512 bytes of storage), then adding another 20 bytes won't make any difference (because it'll still take up 512 bytes of storage). It's not until a file exceeds 512 bytes of content that it'll start to affect your free storage further.

#5 Ta©ti_Tac0Z

  • Members
  • 59 posts

Posted 13 August 2018 - 01:07 PM

ahhhhh ok yeah makes sense

View PostBomb Bloke, on 13 August 2018 - 12:28 PM, said:

Sounds like you're reading "minimum" as "maximum".
yes i was

#6 Ta©ti_Tac0Z

  • Members
  • 59 posts

Posted 13 August 2018 - 01:34 PM

great you're right makeing a for _ = 1, 600 do to one of the file.write functions makes a chance in the fs.getFreeSpace
any way of looking into a file handle and see how many bytes is in the buffer?


am getting a little issue:
Lfree = fs.getFreeSpace(path) --Last free space, path is pointing at a file
local function WB(num)
  if Lfree < 14000 then
   spaceE = true --space Error
   peripheral.call("top", "playNote", 0,0) -- tryed to get a iron noteblock to make a sound when it gets too full
end
		  
file.write(num)
end

--i added a
file.flush()
--at a place where it will be called evrey 2000 bytes should be well before it hits zero


the spaceE never gets set to true
and no sound is ever played

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 August 2018 - 02:26 PM

View PostTa©ti_Tac0Z, on 13 August 2018 - 01:34 PM, said:

any way of looking into a file handle and see how many bytes is in the buffer?

Not really. You can assume it's empty after flushing, though.

View PostTa©ti_Tac0Z, on 13 August 2018 - 01:34 PM, said:

am getting a little issue:

the spaceE never gets set to true
and no sound is ever played

Your code only ever checks the available free space once. If you want to check it multiple times, then you need to call fs.getFreeSpace() multiple times. The value recorded in Lfree isn't going to automatically update.

In cases where drive size is an issue, you might consider increasing it through ComputerCraft.cfg - personally I've got it cranked up to the maximum, roughly a gig per computer.

If you're playing on a server and can't do that, then you may find BBPack to be useful. Among other things, it can compress your whole filesystem, which typically allows for much more storage. To set this up, add os.loadAPI("bbpack") to the top of your startup file, then enter "bbpack compress" at the command line.

#8 Ta©ti_Tac0Z

  • Members
  • 59 posts

Posted 13 August 2018 - 06:46 PM

View PostTa©ti_Tac0Z, on 13 August 2018 - 01:34 PM, said:

Lfree = fs.getFreeSpace(path) --Last free space, path is pointing at a file << omfg am an ideot :D/>/>/>/>/>
local function WB(num)
  if Lfree < 14000 then
   spaceE = true --space Error
   peripheral.call("top", "playNote", 0,0) -- tryed to get a iron noteblock to make a sound when it gets too full
end
		  
file.write(num)
end

--i added a
file.flush()
--at a place where it will be called evrey 2000 bytes should be well before it hits zero


omfg am an ideot :D i didn't spot the fact that the Lspace is defined outside of the function :D i writen that line like 2am or some thing that maybe why

EDIT: with that said evreything is defined in a loop tho

Edited by Ta©ti_Tac0Z, 13 August 2018 - 06:48 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users