Direct sector access for SD Cards

I have a command line application I have written in C which backs up the contents of an SD card to a disc file. The SD is in a non standard disc format which the Mac doesn’t natively recognise (it’s ADFS disc format as used by vintage BBC/Master 128 computers).

In C, I use fopen("/dev/disk1") to open the disc and standard fread/fwrite calls to read/write sectors from the SD card. This all works fine as long as the command line app is run with sudo else I get access denied errors.

I need to incorporate this backup functionality into a Xojo app but can’t get it to work :frowning:

I have tried to create a FolderItem as Dim f As new FolderItem("/dev/disk1") which doesn’t return an error but sets the IsReadable property to False so if I subsequently try to open a BinaryStream on the folder item, I get an IOException error number 2.

Can anyone please offer any advice on how I can read/write sectors straight off the SD card.

Thanks,

Jon

I don’t think Xojo gives you that low-level file access, but you should be able to use a shell object to call the commands you already know and love…

I haven,t tried anything similar, but my instinct would drive me to use a BinaryStream instead of a FolderItem.

Uh… you still need a FolderItem in order to Open or Create a BinaryStream

doh… note to self: rtfm… (completely)

I have come up with a solution which involves making soft declare calls to the c library for the low level open/read/write routines. This seems to work fine (once I chmod 666 /dev/disk1 to give me access).

const libc = "libc.dylib"
const O_RDONLY = &o0000

soft declare function open lib libc (path as CString, oflag as Integer) as Integer
soft declare function read lib libc (fildes as Integer, Byval buf as ptr, nbyte as Integer) as Integer
soft declare function close lib libc (filedes as Integer) as Integer

dim fd as Integer = open("/dev/disk1", O_RDONLY)
if fd  = -1 then
  //an error occurred.
  return
end if

const sizeBlock = 1280
dim mb as new MemoryBlock(sizeBlock)
dim t as Ptr = mb

try
  
  dim read_bytecount as Integer = read(fd, t, sizeBlock)
  
finally
  dim close_result as Integer = close(fd)
  fd = -1
end try

Thanks for the suggestions, I can now crack on writing the rest of my code.