Advertisement
DOGGYWOOF

Untitled

Aug 27th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 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. return false
  34. end
  35.  
  36. local file = fs.open(filePath, "r")
  37. if not file then
  38. local msg = "Error: Could not open file " .. filePath
  39. print(msg)
  40. log(msg)
  41. return false
  42. end
  43.  
  44. local success, errorMessage = pcall(function()
  45. local content = file.readAll()
  46. file.close()
  47. end)
  48.  
  49. if not success then
  50. local msg = "Error reading file " .. filePath .. ": " .. errorMessage
  51. print(msg)
  52. log(msg)
  53. return false
  54. end
  55.  
  56. local msg = "File " .. filePath .. " is okay."
  57. print(msg)
  58. log(msg)
  59. return true
  60. end
  61.  
  62. -- Function to recursively check files in a directory, excluding /disk/users subdirectories
  63. local function checkDirectory(directory)
  64. local files = fs.list(directory)
  65. for _, file in ipairs(files) do
  66. local filePath = directory .. "/" .. file
  67. if fs.isDir(filePath) then
  68. -- Skip /disk/users subdirectories
  69. if not filePath:match("^/disk/users") then
  70. checkDirectory(filePath)
  71. end
  72. else
  73. recordFile(filePath) -- Record the file in the file list
  74. checkFile(filePath)
  75. end
  76. end
  77. end
  78.  
  79. -- Function to check files from the recorded file list
  80. local function checkRecordedFiles()
  81. if fs.exists(fileListPath) then
  82. local fileList = fs.open(fileListPath, "r")
  83. if fileList then
  84. for line in fileList.readLine do
  85. checkFile(line)
  86. os.sleep(0.5) -- Reduced delay for faster processing
  87. end
  88. fileList.close()
  89. else
  90. print("Error: Could not open file list for reading.")
  91. end
  92. else
  93. print("No previous file list found. Scanning all files...")
  94. os.sleep(1) -- Longer delay before starting full scan
  95. checkDirectory("/disk")
  96. end
  97. end
  98.  
  99. -- Function to update references in the script
  100. local function updateDirectoryReferences(oldDir, newDir)
  101. local function replaceInFile(filePath)
  102. local file = fs.open(filePath, "r")
  103. if not file then
  104. print("Error: Could not open file for reading " .. filePath)
  105. return
  106. end
  107.  
  108. local content = file.readAll()
  109. file.close()
  110.  
  111. local updatedContent = content:gsub(oldDir, newDir)
  112. if updatedContent ~= content then
  113. local updatedFile = fs.open(filePath, "w")
  114. if updatedFile then
  115. updatedFile.write(updatedContent)
  116. updatedFile.close()
  117. print("Updated file: " .. filePath)
  118. else
  119. print("Error: Could not open file for writing " .. filePath)
  120. end
  121. end
  122. end
  123.  
  124. local function updateInDirectory(directory)
  125. local files = fs.list(directory)
  126. for _, file in ipairs(files) do
  127. local filePath = directory .. "/" .. file
  128. if fs.isDir(filePath) then
  129. updateInDirectory(filePath)
  130. else
  131. replaceInFile(filePath)
  132. end
  133. end
  134. end
  135.  
  136. updateInDirectory("/disk")
  137. end
  138.  
  139. -- Function to ask for new main directory and update code references
  140. local function promptAndUpdate()
  141. print("Current main directory is /disk")
  142. write("Enter new main directory (e.g., /example): ")
  143. local newDir = read()
  144.  
  145. -- Validate and format new directory
  146. if not newDir:match("^/") then
  147. newDir = "/" .. newDir
  148. end
  149. if not newDir:match("/$") then
  150. newDir = newDir .. "/"
  151. end
  152.  
  153. print("Updating references from /disk to " .. newDir .. "...")
  154. updateDirectoryReferences("/disk", newDir)
  155. print("References updated.")
  156. end
  157.  
  158. -- Main program
  159. print("Starting file check...")
  160. log("Starting file check...")
  161.  
  162. -- Prompt user to update the main directory
  163. promptAndUpdate()
  164.  
  165. -- Clear previous log file if it exists
  166. if fs.exists(logFilePath) then
  167. fs.delete(logFilePath)
  168. end
  169.  
  170. -- Clear previous file list if it exists
  171. if fs.exists(fileListPath) then
  172. fs.delete(fileListPath)
  173. end
  174.  
  175. -- Include 'startup' and 'no-os' in the file list
  176. recordFile("/disk/startup")
  177. recordFile("/disk/no-os")
  178.  
  179. -- Start checking files
  180. checkDirectory("/disk")
  181.  
  182. print("File check complete.")
  183. log("File check complete.")
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement