Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- migrate_to_ndisk.lua
- -- Script to migrate data from local disk "students/" folder to NDisk
- local ndisk = require("ndisk_api")
- local disk = peripheral.find("drive")
- if not disk then
- print("Error: No disk drive found. Cannot read source data.")
- return
- end
- local diskMount = disk.getMountPath()
- if not diskMount then
- print("Error: No disk inserted.")
- return
- end
- if not fs.exists(diskMount .. "/students") then
- print("Error: 'students' directory not found on disk.")
- return
- end
- print("Initializing NDisk...")
- ndisk.init()
- print("Starting migration...")
- local successCount = 0
- local failCount = 0
- local function migrateFile(localPath, ndiskPath)
- print("Migrating: " .. ndiskPath)
- local file = fs.open(localPath, "r")
- local content = file.readAll()
- file.close()
- local success, err = ndisk.write(ndiskPath, content)
- if success then
- -- Verify
- local readBack, readErr = ndisk.read(ndiskPath)
- if readBack == content then
- print(" [OK]")
- successCount = successCount + 1
- else
- print(" [FAIL] Verification mismatch: " .. (readErr or "Content differs"))
- failCount = failCount + 1
- end
- else
- print(" [FAIL] Write error: " .. (err or "matches"))
- failCount = failCount + 1
- end
- end
- local function traverse(path, relativePath)
- local list = fs.list(path)
- for _, item in ipairs(list) do
- local fullPath = fs.combine(path, item)
- local newRelPath = relativePath == "" and item or (relativePath .. "/" .. item)
- if fs.isDir(fullPath) then
- traverse(fullPath, newRelPath)
- else
- -- Check if it's a student file?
- -- path starts with diskMount .. "/students"
- -- relPath starts with "students/..." if we pass that?
- -- Actually, we want keys in NDisk to be "students/ID/file.txt"
- -- So if we iterate inside "diskMount/students", relativePath is "ID/file.txt"
- -- We want to prepend "students/"
- local ndiskKey = "students/" .. newRelPath
- migrateFile(fullPath, ndiskKey)
- end
- end
- end
- traverse(diskMount .. "/students", "")
- print("Migration Complete.")
- print("Success: " .. successCount)
- print("Failed: " .. failCount)
- print("You can verify manually via 'ndisk_api' or running the server.")
Advertisement
Add Comment
Please, Sign In to add comment