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.
[SOLVED]Accessing an usb pendrive
[SOLVED]Accessing an usb pendrive
Last edited by DjPoke on Sat Apr 16, 2022 2:57 pm, edited 1 time in total.
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Accessing an usb pendrive
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.
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.
obey
Re: Accessing an usb pendrive
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 ?
May be i can try to get something like the drive space, telling that less than 128 gigabytes should be an usb pendrive ?
Last edited by DjPoke on Fri Apr 15, 2022 3:31 pm, edited 1 time in total.
Re: Accessing 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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Accessing an usb pendrive
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.
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.
obey
Re: Accessing an usb pendrive
This is the solution for Windows with Powershell. Missing a solution for Ubuntu Linux.
Any idea ?
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
Solved by executing lsblk and searching for sdbX.
Re: Accessing an usb pendrive
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
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
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Accessing an usb pendrive
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.
obey
Re: Accessing an usb pendrive
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.BrotSagtMist wrote: ↑Sat Apr 16, 2022 3:11 pmThis 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.
Shoud i also check for /media/user/ drive ?
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 0 guests