107 posts
Posted 03 July 2013 - 02:20 PM
Ok so here is my problem… I'm making a login for my system where if you fail too many times it locks your account down. It does so by moving the users account to a different folder and renaming it to a number for inspection later by an admin. For example the user file name is Boudragon. I am generating a random number using:
rnd = tostring(math.random(101, 999))
then it prints a message with that number as a reference code… then I do this:
fs.move ("users/".. userlog, "pending/".. rnd)
It makes the new file just fine, however it does not delete the old one from the users folder. Any ideas why? The error I got is this:
login:142: Access denied
line 142 is where the fs.move is so I KNOW its a problem with that but can't see where I went wrong…
892 posts
Location
Where you'd least expect it.
Posted 03 July 2013 - 02:22 PM
Make sure to close all lf your file handles.
When you use anyname = fs.open("filename","handle"), when you are done using anyname, do anyname.close().
107 posts
Posted 03 July 2013 - 02:24 PM
Ah so even for a move I have to frist open and then close the file? Got ya… let me try that then…
107 posts
Posted 03 July 2013 - 02:31 PM
Crap now Im lost again lol… ok so… let me try and post a little more of what I THINK it should look like… Im sure Im wrong but here it goes…
rnd = tostring(math.random(101,999))
file = fs.open("users/".. userlog, "r")
file.move ("users/".. userlog, "pending/".. rnd)
file.close()
is that close at least? :-p
EDIT: I must be close cause now I get login:143: attempt to call nil
143 is the file.move line…
892 posts
Location
Where you'd least expect it.
Posted 03 July 2013 - 02:43 PM
If you are reading a file, close it before you move it. Its fs.move, not file.move.
107 posts
Posted 03 July 2013 - 03:05 PM
Still saying access denied… I tried:
file = fs.open("users/"..userlog, "r")
file.close()
fs.move ("users/".. userlog, "pending/".. rnd)
Thing is at the moment of moving it shouldnt be reading the file at all… it reads it when it checks the password but then closes… here I will give you a larger snippet…
ulog = fs.exists("users/".. userlog)
if ulog == true then
ulog = fs.exists("users/".. userlog)
if (ulog and ulog:read()==userpas) then
term.setCursorPos(16, 18)
print(" Login Success...")
sleep(3)
else
if lockdown < 2 then
term.setCursorPos(16, 18)
print(" Wrong Password ")
sleep(3)
if userlog == puselog then
lockdown = lockdown+1
else
lockdown = 0
puselog = userlog
lockdown = lockdown+1
end
else
term.setCursorPos(15, 18)
rnd = tostring(math.random(101,999))
print(" Lockdown Ref #: ".. rnd)
sleep(3)
file = fs.open("users/".. userlog)
file.close()
fs.move ("users/".. userlog, "pending/".. rnd)
end
end
else
(Code goes on to a different branch of operations for handling an unknown username from here)
107 posts
Posted 03 July 2013 - 06:23 PM
AFter trying a few more combinations still nothing works… Just can't see the problem…
997 posts
Location
Wellington, New Zealand
Posted 03 July 2013 - 08:50 PM
He didn't say to open and close the file when moving it - he said that every time you open the file you need to close it.
107 posts
Posted 03 July 2013 - 08:59 PM
Even so… without any of that simply moving the file doesnt work either. It makes a COPY… but refuses to delete the original. No clue why…
edit: and as far as I can tell… the file isnt opened any other time… unless doing a fs.exists opens it?
8543 posts
Posted 03 July 2013 - 09:21 PM
Are you try to manipulate files in the /rom folder or its sub folders?
107 posts
Posted 03 July 2013 - 09:26 PM
folder structure is outside rom so:
../users/Boudragon
../pending/###
The idea is that when they fail to use the correct password 3 times it generates a random number… displays the number as a reference code and then moves the Boudragon file to the pending directory while also renaming it to the random number.
EDIT: In case you are wondering… YES I added a check to make sure the random number isnt already taken by another file. So the code above is a little out dated so-to-speak but either way… should work the very first time AT LEAST… lol
8543 posts
Posted 03 July 2013 - 09:27 PM
Why the ..? Are you trying to access folders above the root of the computer's "hard drive"?
107 posts
Posted 03 July 2013 - 09:30 PM
No… just saying… the ".." was meant to say that the folders I am trying to access are just in the hard drive lol sorry to confuse. Folders are just /users and /pending
Also I KNOW my directories work just fine… I am able to create accounts, login successfully… everything else works BUT THIS… the lockout feature…
997 posts
Location
Wellington, New Zealand
Posted 03 July 2013 - 10:38 PM
Or you could use fs.copy and fs.delete instead if fs.move won't work.
107 posts
Posted 03 July 2013 - 10:42 PM
Tried it :(/> Was the first thing I tried after none of my other attempts at moving worked… seems to have a problem deleting which I understnad is usually due to opening the file… and I thought I figured it out cause it does look at the file for the password, however, even adding a close after the password check it STILL wont move or delete.
997 posts
Location
Wellington, New Zealand
Posted 04 July 2013 - 02:27 AM
Pastebin your password check, including the place where the file is closed.
159 posts
Location
A Chair
Posted 04 July 2013 - 05:36 AM
ulog:read()
You call a function here, could you paste a snippet with this function please. I am assuming that you open a file and read from it here… (this could be why you can not delete the file hence the Access Denied)
Also try adding "./users/" ..
and "./pending/" ..
Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh… I see the error now while typing this.
/" gives you a string with a " in it instead of a /
try
fs.move ("users//".. userlog, "pending//".. rnd)
could be something silly like that. the interpreter could be taking "users/".. userlog, " as a single string instead of taking users/ then appending userlog
…
107 posts
Posted 04 July 2013 - 09:50 AM
immibis I pasted a snippet of the password check above… it's all in there :-p as for albrat I thought for sure your solution would work… but nope :(/> especially when I use the same kind of operations when creating a user account and it works just fine. The only difference really is with creating an account it just makes the file rather than renaming and moving one.
892 posts
Location
Where you'd least expect it.
Posted 04 July 2013 - 10:56 AM
local pass
ulog = fs.exists("users/".. userlog,"r")
if ulog == true then
ulog = io.open("users/".. userlog)
pass=ulog and ulog:read()
ulog:close()
if (pass and pass==userpass) then
term.setCursorPos(16, 18)
print(" Login Success...")
sleep(3)
else
if lockdown < 2 then
term.setCursorPos(16, 18)
print(" Wrong Password ")
sleep(3)
if userlog == puselog then
lockdown = lockdown+1
else
lockdown = 0
puselog = userlog
lockdown = lockdown+1
end
else
term.setCursorPos(15, 18)
rnd = tostring(math.random(101,999))
print(" Lockdown Ref #: ".. rnd)
sleep(3)
--# I never said you had to open it to move it, I said every time you are done with a file handle, close it!
fs.move ("users/".. userlog, "pending/".. rnd)
end
end
else
I fixed the code you gave me, if it still doesnt work its because some other part is broken.
Edit:
ulog:read()
You call a function here, could you paste a snippet with this function please. I am assuming that you open a file and read from it here… (this could be why you can not delete the file hence the Access Denied)
Also try adding "./users/" ..
and "./pending/" ..
Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh… I see the error now while typing this.
/" gives you a string with a " in it instead of a /
try
fs.move ("users//".. userlog, "pending//".. rnd)
could be something silly like that. the interpreter could be taking "users/".. userlog, " as a single string instead of taking users/ then appending userlog
…
You are mixing forward slashes, /, with backward slashes, \. Backward slashes are the escape characters.
107 posts
Posted 04 July 2013 - 11:17 AM
Ok tried… still Access Denied… :(/> I know what you meant about closing after opening… but the ONLY time the file is opened is when checking the password during that sequence… I've looked before it and there is nothing… and after it nothing exists either…
EDIT: At this point I'm willing to face scrutiny for my messy programming and pastebin the unfinished code just to get the darn thing working LMAO! Would that help? :-p
892 posts
Location
Where you'd least expect it.
Posted 04 July 2013 - 11:33 AM
Ok tried… still Access Denied… :(/>/> I know what you meant about closing after opening… but the ONLY time the file is opened is when checking the password during that sequence… I've looked before it and there is nothing… and after it nothing exists either…
EDIT: At this point I'm willing to face scrutiny for my messy programming and pastebin the unfinished code just to get the darn thing working LMAO! Would that help? :-p
Yes, yes it would.
107 posts
Posted 04 July 2013 - 11:40 AM
Ok… here is the pastebin ID… I know it's not "pretty" but for the most part it works… and being a beginner I think I'm doing ratehr well LOL!
p9TEjd7x
107 posts
Posted 06 July 2013 - 07:55 PM
??? Did I lose my tech support? LOL
107 posts
Posted 07 July 2013 - 03:43 PM
Ok after MANY hours of manipulating my code and google searches I managed to solve the problem. Turns out with all the if/then statements and such I had some of my equations either in or out of loops where the didn't belong and such… long story but all that matters now is it works LOL! Thanx for the help!