Advertisement
DOGGYWOOF

Untitled

Aug 27th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. -- Paths for the log and file list
  2. local logFilePath = "/disk/check_log.txt"
  3. local fileListPath = "/disk/file_list.txt"
  4.  
  5. -- Function to write to the log file
  6. local function log(message)
  7. local logFile = fs.open(logFilePath, "a")
  8. if logFile then
  9. logFile.writeLine(message)
  10. logFile.close()
  11. else
  12. print("Error: Could not open log file.")
  13. end
  14. end
  15.  
  16. -- Function to record a file path into the file list
  17. local function recordFile(filePath)
  18. local fileList = fs.open(fileListPath, "a")
  19. if fileList then
  20. fileList.writeLine(filePath)
  21. fileList.close()
  22. else
  23. print("Error: Could not open file list.")
  24. end
  25. end
  26.  
  27. -- Function to check if a file exists and is readable
  28. local function checkFile(filePath)
  29. if not fs.exists(filePath) then
  30. local msg = "Error: File does not exist " .. filePath
  31. print(msg)
  32. log(msg)
  33. os.sleep(1) -- Delay for 1 second
  34. return false
  35. end
  36.  
  37. local file = fs.open(filePath, "r")
  38. if not file then
  39. local msg = "Error: Could not open file " .. filePath
  40. print(msg)
  41. log(msg)
  42. os.sleep(1) -- Delay for 1 second
  43. return false
  44. end
  45.  
  46. local success, errorMessage = pcall(function()
  47. local content = file.readAll()
  48. file.close()
  49. end)
  50.  
  51. if not success then
  52. local msg = "Error reading file " .. filePath .. ": " .. errorMessage
  53. print(msg)
  54. log(msg)
  55. os.sleep(1) -- Delay for 1 second
  56. return false
  57. end
  58.  
  59. local msg = "File " .. filePath .. " is okay."
  60. print(msg)
  61. log(msg)
  62. os.sleep(1) -- Delay for 1 second
  63. return true
  64. end
  65.  
  66. -- Function to recursively check files in a directory, excluding /disk/users subdirectories
  67. local function checkDirectory(directory)
  68. local files = fs.list(directory)
  69. for _, file in ipairs(files) do
  70. local filePath = directory .. "/" .. file
  71. if fs.isDir(filePath) then
  72. -- Skip /disk/users subdirectories
  73. if not filePath:match("^/disk/users") then
  74. checkDirectory(filePath)
  75. end
  76. else
  77. recordFile(filePath) -- Record the file in the file list
  78. checkFile(filePath)
  79. end
  80. end
  81. end
  82.  
  83. -- Function to check files from the recorded file list
  84. local function checkRecordedFiles()
  85. if fs.exists(fileListPath) then
  86. local fileList = fs.open(fileListPath, "r")
  87. if fileList then
  88. for line in fileList.readLine do
  89. checkFile(line)
  90. os.sleep(1) -- Delay for 1 second
  91. end
  92. fileList.close()
  93. else
  94. print("Error: Could not open file list for reading.")
  95. end
  96. else
  97. print("No previous file list found. Scanning all files...")
  98. os.sleep(2) -- Longer delay before starting full scan
  99. checkDirectory("/disk")
  100. end
  101. end
  102.  
  103. -- Main program
  104. print("Starting file check...")
  105. log("Starting file check...")
  106.  
  107. -- Clear previous log file if it exists
  108. if fs.exists(logFilePath) then
  109. fs.delete(logFilePath)
  110. end
  111.  
  112. -- Clear previous file list if it exists
  113. if fs.exists(fileListPath) then
  114. fs.delete(fileListPath)
  115. end
  116.  
  117. -- Start checking files
  118. checkDirectory("/disk")
  119.  
  120. print("File check complete.")
  121. log("File check complete.")
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement