Recent

Author Topic: How to eject/safetly remove an USB Drive ?  (Read 17677 times)

nhatdung

  • New Member
  • *
  • Posts: 37
How to eject/safetly remove an USB Drive ?
« on: March 31, 2011, 06:59:10 pm »
i can find them all in /etc/mtab
but how to eject/safetly remove ?

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: How to eject/safetly remove an USB Drive ?
« Reply #1 on: March 31, 2011, 09:54:24 pm »
You mean how to unmount a drive from your program? For that you should use API calls...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

nhatdung

  • New Member
  • *
  • Posts: 37
Re: How to eject/safetly remove an USB Drive ?
« Reply #2 on: April 01, 2011, 04:49:00 am »
yes that's what i mean. But what API should i use  %)

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: How to eject/safetly remove an USB Drive ?
« Reply #3 on: April 01, 2011, 10:18:16 pm »
umount call. In C it's like this:
Code: C  [Select][+][-]
  1. #include <sys/mount.h>
  2.  
  3.        int umount(const char *target);
  4.  

(this is from the umount(2) manpage)

Unfortunately, I've never did it from Pascal, so I cannot help you much. You could check the FPC documentation, also someone with some experience in this might help better...

You should also beware it is quite likely for the unmounting to fail, either if the volume requires root privileges to unmount, or there are open files on it (quite often). There isn't much you can do about it from your program if it happens, but handle the error gracefully and report it to the user...
« Last Edit: April 02, 2011, 01:47:18 am by TurboRascal »
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to eject/safetly remove an USB Drive ?
« Reply #4 on: April 02, 2011, 12:38:59 am »
Quote
umount call. In C it's like this:
If the C signature is like that, then it's probably like this in Pascal:
Code: [Select]
uses
  ctypes;

function umount(const target: PChar): cint: cdecl; external;
hoping it resides in the kernel libs (which I assume so).

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: How to eject/safetly remove an USB Drive ?
« Reply #5 on: April 02, 2011, 01:49:53 am »
It should be there, this is a kernel syscall...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

nhatdung

  • New Member
  • *
  • Posts: 37
Re: How to eject/safetly remove an USB Drive ?
« Reply #6 on: April 02, 2011, 03:55:23 pm »
there is no mount/umount command in freepascal. i use tprocess and it work fine  8-)
thanks!

ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: How to eject/safetly remove an USB Drive ?
« Reply #7 on: April 02, 2011, 04:39:20 pm »
I think that you can use syscall for that, something like:

Code: [Select]
fpsyscall(syscall_nr_umount, '/mnt/dok');
Or something like that (I have not tried it myself).

nhatdung

  • New Member
  • *
  • Posts: 37
Re: How to eject/safetly remove an USB Drive ?
« Reply #8 on: April 05, 2011, 09:24:54 am »
i haven't try my self but use tprocess call umount work very good :D
btw : where is "fpsyscall" function ?
« Last Edit: April 05, 2011, 12:18:53 pm by nhatdung »

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: How to eject/safetly remove an USB Drive ?
« Reply #9 on: April 08, 2011, 12:30:32 am »
there is no mount/umount command in freepascal. i use tprocess and it work fine  8-)
thanks!

So you called the umount binary file from your program? Not too bad a solution since every linux system must have that...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: How to eject/safetly remove an USB Drive ?
« Reply #10 on: April 08, 2011, 09:39:37 am »
Instead of TProcess you can try to use what i know works on Mac OS X
Code: Pascal  [Select][+][-]
  1. uses ... Unix, ...
  2. ...
  3. fpSystem('diskutil umount /Volumes/volume_name');
instead of /Volumes/volume_name put your mount path to usb

ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: How to eject/safetly remove an USB Drive ?
« Reply #11 on: April 08, 2011, 11:36:19 am »
Instead of TProcess you can try to use what i know works on Mac OS X
Code: Pascal  [Select][+][-]
  1. uses ... Unix, ...
  2. ...
  3. fpSystem('diskutil umount /Volumes/volume_name');
instead of /Volumes/volume_name put your mount path to usb

But then you must place the full path of the program.

I offered to use SysCalls that uses the system in code to umount the device itself. So even if a unix/linux does not have umount command for any reason, the program should it.

nhatdung

  • New Member
  • *
  • Posts: 37
Re: How to eject/safetly remove an USB Drive ?
« Reply #12 on: April 09, 2011, 11:01:47 am »
sorry but i can't find function fpsyscall/syscall in Lazarus. Can u tell me what unit to use ?

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: How to eject/safetly remove an USB Drive ?
« Reply #13 on: April 09, 2011, 11:03:23 am »
I think it's fpSystem() in Unix unit.
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

nhatdung

  • New Member
  • *
  • Posts: 37
Re: How to eject/safetly remove an USB Drive ?
« Reply #14 on: April 09, 2011, 12:06:58 pm »
fpSystem only take 1 argument  :(

 

TinyPortal © 2005-2018