67 posts
Location
Brazil
Posted 29 November 2013 - 05:30 PM
Heyo everybody, this is probably a very stupid question, but I really want to know what the 'Fs API' is used for.
I've seen it in a couple Programs out there, but I don't really understand it - I saw the wiki, but nothing there really helped me, I'm just looking for a quick and easy explanation, and, if possible, some examples.
Thanks.
321 posts
Location
Melbourne, Australia
Posted 29 November 2013 - 06:15 PM
The FS API is used for reading and writing to the file system of that computer. Of course, you could also use the IO API, but that is just a wrapper for FS.
8543 posts
Posted 29 November 2013 - 06:26 PM
It's not just a wrapper for the FS API, it also adds the :lines() method.
67 posts
Location
Brazil
Posted 29 November 2013 - 06:30 PM
I don't really know what files are, in terms of cc - basicly, are files equals programs? If not, what are they, exactly?
1688 posts
Location
'MURICA
Posted 29 November 2013 - 06:38 PM
Files are just files; what they are and what they're used for in the context of CC is the same as any real life computer. In CC, files can be run as programs, but they're still just files.
67 posts
Location
Brazil
Posted 29 November 2013 - 09:24 PM
So, they can theoretically be used to do pretty much anything? Like, store information, start programs, print, and etc?
Edited on 29 November 2013 - 08:24 PM
1688 posts
Location
'MURICA
Posted 29 November 2013 - 09:38 PM
Yes, and they are used that way quite often.
67 posts
Location
Brazil
Posted 29 November 2013 - 09:45 PM
I just don't see the advantage of using a file over a program itself, I'm probably just being stupid but, if a program can do everything a file can, why to use a file, then?
EDIT: dat rhyme.
Edited on 29 November 2013 - 08:45 PM
892 posts
Location
Where you'd least expect it.
Posted 29 November 2013 - 10:30 PM
I just don't see the advantage of using a file over a program itself, I'm probably just being stupid but, if a program can do everything a file can, why to use a file, then?
EDIT: dat rhyme.
A program is a file that can be run. Files and directories (or folders) are part of a file structure. Directories CONTAIN files.
Files are where data is stored. Pictures, images, programs, plain text, even applications are types of files. The FS API is an API used for editing or reading files in their plaintext or binary forms, or also for creating directories, and moving/deleting/copying files/directories.
TL;DR: Go back and take a minute of your time to read what I said.
1688 posts
Location
'MURICA
Posted 29 November 2013 - 11:55 PM
I just don't see the advantage of using a file over a program itself, I'm probably just being stupid but, if a program can do everything a file can, why to use a file, then?
EDIT: dat rhyme.
It'd be pretty difficult for a program to store its data in the program itself, wouldn't you think? The program would need to use a separate, non-program file for that.
67 posts
Location
Brazil
Posted 30 November 2013 - 12:30 AM
I'm kinda getting it now, but, in tearms of computercraft, what can we do with it? If possible, give me some simple examples, so I can see it better. It's kinda hard to visualize it this way - Sorry for the extreme newbie behavior.
331 posts
Posted 30 November 2013 - 12:55 AM
Well most commonly people use it with their turtle program's to keep track of where it is in case of a shutdown and having to start from the start( cant give some code here )
But an example i can give is a password lock
repeat
if fs.exists("password") then
fileHandle = fs.open("password","r") -- open file in read mode
local pass = fileHandle.readAll() -- read everything in the file
fileHandle.close() -- make sure to close
else
write("Enter new password: ")
local pass = read("*")
fileHandle = fs.open("password","w") -- open file in write mode
fileHandle.write(pass) -- store password in the file
fileHandle.close() -- again close the file
end
term.clear()
term.setCursorPos(1,1)
write("Enter Your Password: ")
input = read("*")
until input == pass
Edit: Basically if a password file doesn't exist yet then it will create the file and write the password to it otherwise it will read from the password file and then store it in the variable called pass and then compare later on
Edited on 29 November 2013 - 11:57 PM
67 posts
Location
Brazil
Posted 30 November 2013 - 01:03 AM
It's still a bit confusing to me, I'm just stupid, don't mind me - I'm going to search a little more, do some reseach, some testing, I'll figure it out.
But still, thank you guys, this whole topic is much more clear now, at least know I know the very basics, you really helped me!
See ya' all later!
Edited on 30 November 2013 - 12:04 AM
331 posts
Posted 30 November 2013 - 01:41 AM
I know your a bit confused but, files arnt just for program's. See above posts but files can be a useful way to save information that you can then read or edit from th fs api
7083 posts
Location
Tasmania (AU)
Posted 30 November 2013 - 02:35 AM
I don't really know what files are, in terms of cc - basicly, are files equals programs? If not, what are they, exactly?
I suspect you've somehow got the mindset that "CC files" are different to "regular files" that you may have on your own real life computer.
They're not. When you use the FS API to open a file and write data into it, it's creating a real file on the server and writing data into that. The whole MineCraft server can be shut down and restarted and your file will stay there, ready to read from when you want it. On the other hand, the contents of your CC computer's "RAM" gets cleared as soon as the chunk they're in unloads (same as what happens to the RAM of your real computer if you turn THAT off).
67 posts
Location
Brazil
Posted 17 December 2013 - 08:58 PM
I once again tried to learn a bit more about Fs, after this couple weeks, and I started by, whitout looking at this topic, trying to create a password system. The code "works", at first it asks for the new password, I typed "123456", without the brackets of course, and next, the program asked me to retype the password, I did, but the program seems to understand that the password is wrong. It keeps asking me the password, although I already did.
Here's the code:
pastebin.com/4L8tLppcWhat am I doing wrong?
995 posts
Location
Canada
Posted 17 December 2013 - 11:06 PM
There are some things wrong before I will even go into your specific code. First, use an if statement to check passwords in a while loop. You can implement a timeout after too many failures, and when input == password you can use the break keyword. In your code, your input is local to the repeat body, so the check always fails. (I think, don't quote me)
15 posts
Posted 18 December 2013 - 07:30 AM
awsmazinggenius is correct, and your repeat loop is fine. There's no need to use an if statement in a while loop here. The variable, pass, is local, and so only exists on the same level it's declared as well as deeper levels, which in this case is inside the repeat body. For example, a series of conditional statements would have scope that looks like:
--start level 1
repeat
--start level 2
if x == 1 then
--start level 3
print("asdf")
--end level 3
end
--end level 2
until y == 1
--end level 1
You can fix your code by declaring pass before the repeat loop with say, local pass = "", then inside the if statement, change that to pass = file.readAll()
This is an example of block scope in programming. Read about it in
http://en.wikipedia....mputer_science)
Edited on 18 December 2013 - 06:45 AM
995 posts
Location
Canada
Posted 18 December 2013 - 09:35 AM
Part of the reason indentation is useful - lets you see the scope of variables. You could make pass global, but that is not a good choice because any program can print(pass) to see the password.
While you are at it, I would encrypt or hash the password on disk. I use GravityScore's SHA256 Algorithm to hash passwords on disk, and then I hash the input as well in awsmazingOS.
Edited on 18 December 2013 - 08:36 AM
67 posts
Location
Brazil
Posted 18 December 2013 - 02:07 PM
Thanks for all the suggestions and the corrections, it was really helpful. But, the point here isn't actually to code a flawless password system that no one can acess, if I wanted to do that, I would actually focused on that, probably with the help of you guys. I was just testing the fs API, which I'm absolutely terrible at. I figured the problem with the variables and the files, so the problem was solved, and I really am understading a bit more about the API as a whole now. But, awsmazinggenius, you said something quite interesting that I never heard about. What is this GravityScore's SHA256 Algorithm that you talked about? I'm curious.
1583 posts
Location
Germany
Posted 18 December 2013 - 05:18 PM
A hash algorithm is a one way encryption for files/text/numbers/etc. To secure them.
995 posts
Location
Canada
Posted 18 December 2013 - 08:26 PM
Yeah. SHA256 is the second most secure, and the most secure for CC that I have found. It can secure passwords on disk so malicious programs (or server owners) can't see them.
169 posts
Posted 18 December 2013 - 09:22 PM
Yeah. SHA256 is the second most secure
Source please.
67 posts
Location
Brazil
Posted 18 December 2013 - 09:31 PM
Any tutorials/examples on how to use it?
995 posts
Location
Canada
Posted 19 December 2013 - 09:19 AM
Yeah. SHA256 is the second most secure
Source please.
GravityScore himself (maybe not the best :)/>) (on his Thunderhawk thread) and also found on a couple other pages on the internet.
To use, you can load it as an API and call sha256("your string") and it will return a hashed version.
Edited on 19 December 2013 - 08:20 AM
882 posts
Location
Behind you.
Posted 19 December 2013 - 09:20 AM
Immibis' peripherals has a Cryptographic Accelerator with a sha256 hasn and a sha512 hash
995 posts
Location
Canada
Posted 19 December 2013 - 08:32 PM
I realize that - and awsmazingOS has support for SHA512 hashing via the Cryptographic Accelerator. However, the peripheral is a bit expensive to craft (needs 64 computers and some other materials) - and I don't want to rely on it - but it can be enabled in the awsmazingOS Admin panel. (Not in the user preferences panel (message to my beta testers))
Edited on 19 December 2013 - 09:20 PM
882 posts
Location
Behind you.
Posted 20 December 2013 - 12:10 PM
64 computers would only take me about an hour of mining with an Iron pick max.
The only problem I would have would be the smelting, but I'd just leave on my game overnight or something for that.
995 posts
Location
Canada
Posted 20 December 2013 - 11:11 PM
Upgraded furnaces/EE3 transmutation is the answer to that. But I also do not want to depend on immibis's Peripherals in awsmazingOS. But if you need something that secure, you shouldn't do it in Minecraft anyways. Data is still sent to the server unencrypted (and this is practically useless in singleplayer). But, if you are worried about IRL viruses coming to your Minecraft directories and stealing your stuff (fat chance), it can be enabled under the System Admin Panel.
I also do not know about how well the server would run when using the Cryptographic Accelerator to hash passwords - and then having many users elevating application permissions at the same time, which calls Java methods to the peripheral and what not. (In awsmazingOS, if an app needs access that the user might not want happening on the computer, such as HTTP Use (on untrusted apps), or modifying the system settings, it has to make an elevation call with the scope(s) it needs, which will prompt the user to authenticate via awsmazingGuard and return a key that can be used to make elevated calls.)
Edited on 21 December 2013 - 08:26 AM