Page 1 of 2
[SOLVED]Accessing an usb pendrive
Posted: Fri Apr 15, 2022 10:53 am
by DjPoke
Hello !
I'm trying to develop a retro virtual computer with löve2d. (
https://github.com/DjPoke/LoveRetroBasic)
I develop it in Windows, but in the future, it should boot on linux, recognising usb pens like external drives.
I would like to know how to write a file in an usb pendrive ?
For Windows, it should be easy to check drives exist, by testing a to z letters, an trying to save a file (and delete it if possible).
If the file can't be saved, the drive does not exists.
But i don't know how to check this drive is an usb pen, not a hard drive.
For Linux (ubuntu for example) i even don't know how to check the folders, as the system is more complex.
Re: Accessing an usb pendrive
Posted: Fri Apr 15, 2022 2:50 pm
by BrotSagtMist
Okey even your windows solution is horrid. But thats not my beer.
On ubuntu this is the code to show external drives: print(io.popen("ls /media/","r" ):read("a"))
This doesnt show if its an usb pendrive tho.
The connection type is listed for example inside /dev/disk/by-id.
You would have to scan these entries for if it matches your need.
Re: Accessing an usb pendrive
Posted: Fri Apr 15, 2022 3:30 pm
by DjPoke
Thank you BrotSagtMist
May be i can try to get something like the drive space, telling that less than 128 gigabytes should be an usb pendrive ?
Re: Accessing an usb pendrive
Posted: Fri Apr 15, 2022 5:34 pm
by milon
DjPoke wrote: ↑Fri Apr 15, 2022 3:30 pm
May be i can try to get something like the drive space, telling that less than 128 gigabytes should be an usb pendrive ?
That'll give you some good hints, maybe, but don't rely on that. USB drives get larger all the time. I've actually got a 128GB drive sitting here next to me right now, and that would fail your test as it's not
less than 128GB. The connection type is the way to go, as Brot pointed out.
Re: Accessing an usb pendrive
Posted: Fri Apr 15, 2022 6:03 pm
by BrotSagtMist
If you want a _hacky_ way: Try creating a file with a file name not writable on fat32 drives.
Eg case collisions.
If you create file "F" and file "f" this will trouble you on fat.
Any internal drive should have a proper modern filessystem not causing any problems here.
Of course whatever you are trying there: This is horrible.
Re: Accessing an usb pendrive
Posted: Sat Apr 16, 2022 8:39 am
by DjPoke
This is the solution for Windows with Powershell. Missing a solution for Ubuntu Linux.
Any idea ?
Code: Select all
-- check if drive is external and return its size
function GetDriveSize(driveLetter)
local text = ""
local ret = ""
local script = [[ Get-WmiObject -Class win32_logicaldisk -Filter "DriveType = '2'" | Select-Object -Property DeviceID,Size ]]
local sd = love.filesystem.getAppdataDirectory() .. "/LOVE/temp.tmp"
local p = io.popen("powershell -command - >" .. sd, "w")
p:write(script)
p:close()
if GetExtFileExists(sd) then
file = io.open(sd, "rb")
local text = file:read("*a")
file:close()
local l = utf8.len(text)
for i = 1, l do
local c = string.sub(text, utf8.offset(text, i), utf8.offset(text, i))
local a = bit.band(c:byte(), 127)
-- drive letter found
if a == string.byte(driveLetter) then
if i < l then
c = string.sub(text, utf8.offset(text, i + 1), utf8.offset(text, i + 1))
a = bit.band(c:byte(), 127)
if Chr(a) == ":" then
for j = i + 2, l do
c = string.sub(text, utf8.offset(text, j), utf8.offset(text, j))
a = bit.band(c:byte(), 127)
if (a >= 48 and a <= 57) then
ret = ret .. string.char(a)
elseif a ~= 32 then
break
end
end
end
if ret ~= "" then break end
end
end
end
os.remove(sd)
else
return ""
end
return tonumber(ret)
end
function Asc(c)
if c == nil or #c > 1 then return nil end
return string.byte(c, 1, 1)
end
function GetExtFileExists(filename)
local f = io.open(filename, "r")
if f ~= nil then io.close(f) return true else return false end
end
Re: Accessing an usb pendrive
Posted: Sat Apr 16, 2022 2:57 pm
by DjPoke
Solved by executing lsblk and searching for sdbX.
Re: Accessing an usb pendrive
Posted: Sat Apr 16, 2022 2:59 pm
by glitchapp
I did not tested this because I've never attempted that, but have you tried this library?
https://opensourcelibs.com/lib/nativefs
It says in the description that nativefs replicates a subset of the love.filesystem API, but without LÖVE's path restrictions.
I'll be interested to know if it works
Re: Accessing an usb pendrive
Posted: Sat Apr 16, 2022 3:11 pm
by BrotSagtMist
DjPoke wrote: ↑Sat Apr 16, 2022 2:57 pm
Solved by executing lsblk and searching for sdbX.
This doesnt make sense at all. Youre looking for a partition on any harddrive that happens to be found second during boot here.
Also not every usb drive even uses a partition table, they will not have a number.
Re: Accessing an usb pendrive
Posted: Sat Apr 16, 2022 9:04 pm
by DjPoke
BrotSagtMist wrote: ↑Sat Apr 16, 2022 3:11 pm
DjPoke wrote: ↑Sat Apr 16, 2022 2:57 pm
Solved by executing lsblk and searching for sdbX.
This doesnt make sense at all. Youre looking for a partition on any harddrive that happens to be found second during boot here.
Also not every usb drive even uses a partition table, they will not have a number.
Sorry, i've not understood. I've changed the program now to scan from existing partitions from sda1 to sdz9 (?). All i find gived by the lsblk command help me to ensure the partition exists. And after, i check the size of the drive.
Shoud i also check for /media/user/ drive ?