0

I have an EXT2 partition on my usb thumb drive and I can't get the C function open() to return anything other than -1.

Partition manager says that the partition is located at /dev/sdb2 and my code says

int fd = open("/dev/sdb2", O_RDONLY);

But after debugging, (fd == -1) and my usb notifier says that it isn't mounted before or after.

Side Note: the super-block and boot section seem to populate with values...

tshepang
  • 12,111
  • 21
  • 91
  • 136
Eric Fossum
  • 2,395
  • 4
  • 26
  • 50
  • 4
    what is the value of `errno` after the failed open? (You do know that `open` will _not_ mount that partition, right? it'll attempt to open the block device directly, allowing you to read/write directly to the raw disk - very dangerous - if you have the right privileges) – Mat Jun 08 '11 at 21:45
  • Yeah, I didn't explain it well enough (I do know the purpose of open). How do I find errno? – Eric Fossum Jun 08 '11 at 21:55
  • `#include `, `#include `, then after `open`, `if (fd == -1) { perror("open failed"); exit(1); }` or just `printf` `errno` (it's an int). – Mat Jun 08 '11 at 22:00
  • @Mat I used `printf("%s\n", strerror(errno));` and got **Permission denied** how do I grant permission? – Eric Fossum Jun 08 '11 at 22:01
  • Check the permissions on `/dev/sdb2` and either run your code with a user that has the appropriate privileges (probably: root), or change the permissions on the device. **Warning** this is dangerous. – Mat Jun 08 '11 at 22:03
  • How do I change permissions on the device? I'm debugging in Netbeans so I don't know if I can run it with superuser permissions – Eric Fossum Jun 08 '11 at 22:07
  • @Eric Fossum: `chmod` works on block device special files just like it does on ordinary files. – caf Jun 08 '11 at 22:40

2 Answers2

0

Opening /dev/sdb2 will open the raw block device accessing the partition on the thumb drive, which, if you have a filesystem on the partition, is not at all what you want to do. You need to mount the filesystem first, after which you can access files within it via the mount point. Most linux systems will mount the thumb drive automatically when you plug it in -- if you type mount at the command line it will show you all the mounted filesystems and where they were mounted. This will probably show you a line like:

/dev/sdb2 on /media/usb type ext2 (...

In which case you open /media/usb/file to access file in the root directory of the filesystem on the thumb drive.

If its not mounted automatically, you'll need to mount it manually before accessing it -- you can do that with the mount program or the mount(2) system call from within a program, but it generally requires superuser permissions.

Regardless of how you mount it, make sure to unmount it before unplugging the thumb drive to make sure that all the data has been written and is up to date.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Thanks for the reply, but I'm writing a program to access and modify inodes. This requires `open()` and `read()`, Mat was right in that I needed to check my **errno** and act accordingly. – Eric Fossum Jun 09 '11 at 01:19
0

The code to open the thumb drive is correct in my question, but the program either needs to be run as super-user or chmod 777 /dev/sdb2

To see if this is your issue use the code above printf("Error: %s\n", strerror(errno));

Eric Fossum
  • 2,395
  • 4
  • 26
  • 50