This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jay5476's profile picture

My Program Not Working Correctly

Started by jay5476, 26 July 2013 - 03:20 AM
jay5476 #1
Posted 26 July 2013 - 05:20 AM
okay I have started my antivirus program and it was working very well until I introduced going into each directory and searching in there so I was wondering if any of you know why it is messing up, I don't want more efficient coding or anything I just want the program to print out all files with dangerous functions defined by the table bad pastebin: pastebin.com/mtvv9da9
GravityScore #2
Posted 26 July 2013 - 06:29 AM
The easiest way to loop over every file in the filesystem is using recursion (a function calling itself from within itself), not using loops.

This code will print out the full path to all files that exist on the computer, excluding any read only folders (such as the rom folder):

local function check(dir)
  for _, v in pairs(fs.list(dir)) do
    local path = dir .. "/" .. v
    if not fs.isReadOnly(path) then
      if fs.isDir(path) then -- is a directory
        check(path) -- call this function again to check this directory
      else -- is a file
        print(path)
        -- Do whatever else you want to here
      end
    end
  end
end

check("") -- the empty string (instead of "/") here represents root
jay5476 #3
Posted 26 July 2013 - 07:45 AM
thanks dude I am going to include this in my code and ill make sure to give you credit :)/>
jay5476 #4
Posted 26 July 2013 - 08:21 AM
:/ sorry to say unsucssefu lagain after trying to figure out what wrong it want test files code

local badFiles = {}
local directories = {}
local bad = {"fs", "io", "os", "delete"}
local function getDirs()
d = fs.list("/")
for i = 1, #d do
if fs.isDir(d[i]) then
table.insert(directories, d[i])
end
end
end
local function test(file)
print("We tested")
f = fs.open(file, "r")
fileContents = f.readAll()
f.close()
for i = 1, #bad do
print("Checking"..file)
if fileContents:find(bad[i]) then
tabel.insert(file, "badFiles")
end
end
end
local function check(dir)
for _, v in pairs(fs.list(dir)) do
local path = dir .. "/" .. v
if not fs.isReadOnly(path) then
if fs.isDir(path) then 
check(path)
else
test(path)
end
end
end
end
getDirs()
for i = 1, #directories do
print("Checking "..directories[i])
dir = directories[i]
check(dir)
end
print("Bad Files")
for i = 1, #badFiles do
print(badFile[i])
end

 
GravityScore #5
Posted 26 July 2013 - 09:03 AM

local function test(file)
print("We tested")
f = fs.open(file, "r")
fileContents = f.readAll()
f.close()
for i = 1, #bad do
print("Checking"..file)
if fileContents:find(bad[i]) then
tabel.insert(file, "badFiles") -- here
end
end
end


tabel.insert(file, "badFiles")

You spelt table wrong :P/>
jay5476 #6
Posted 26 July 2013 - 06:11 PM
okay then now its not testing the files to see if there bad :(/> http://pastebin.com/hmvmuuq2
albrat #7
Posted 27 July 2013 - 06:00 AM

local function delete()
for i = 1, #badFiles do
if fs.exists(badFiles[i]) then
if badFiles[i] ~= shell.getRunningProgram() then
fs.delete(badFiles[i])
end
end
end
end

I would say the Anti-Virus program is worse than a virus program…

I would make a folder on the Drive, then "mv" all files into that folder that are bad… Llike most good Anti-virus programs do.
or at least do a confirm check for the user first.


local function delete()
for i = 1, #badFiles do
if fs.exists(badFiles[i]) then
if badFiles[i] ~= shell.getRunningProgram() then
-- fs.delete(badFiles[i])
fs.move( badFiles[i], "./badfiles/"..tostring(badfiles[i] ))
end
end
end
end
( not 100% sure that is right as I never used fs.move before )
but the idea is to quarantine the files over deleting them… As just deleting them is virus behavior.

The fine line of virus and anti virus.