LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2012, 01:52 PM   #1
richw42
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
How to determine if USB flash drive is write protected


I have an application running on an embedded computer. The user may plug in a USB flash memory and try to access it. I would like to give an error message immediately if the user tries to write to a write-protected memory, but haven't figured out how. For example, I tried
Code:
i = open(file_name, O_WRONLY | O_CREAT | O_SYNC);
	if (i < 0)
	    perror("open");
	else
	{
	    j = write(i, &i, 4);
	    if (j != 4)
		perror("write");
	    close(i);
	}
on a write protected memory, but it gave no immediate error. I can even remove the USB memory, and then fopen, fwrite, and fclose a file with no error reported to the application. The console terminal gets pelted with error messages, though.
It's an older computer, so it has Linux kernel 2.4.26 with libc 2.3.2.
 
Old 07-02-2012, 03:39 PM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by richw42 View Post
I have an application running on an embedded computer. The user may plug in a USB flash memory and try to access it. I would like to give an error message immediately if the user tries to write to a write-protected memory, but haven't figured out how. For example, I tried
Code:
        i = open(file_name, O_WRONLY | O_CREAT | O_SYNC);
        if (i < 0)
            perror("open");
        else
        ...
are you sure your error criterion is correct? I know that fopen() returns a Null handle on error, and I'd assume that open() behaves the same. So your check for a negative result would never catch.
Anyway, I usually check if the result of fopen() is non-zero to proceed normally.

[X] Doc CPU
 
Old 07-02-2012, 04:32 PM   #3
richw42
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
are you sure your error criterion is correct?
From http://linux.die.net/man/2/open:
Quote:
open() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).
I am checking the f* functions properly for non-zero.
It turns out I misremembered my own code. I'm actually writing the files to a temporary ram disk and then upon getting the "finish" command from the user, I do
Code:
sprintf(buf, "mv /tmp/tmpmidi.mid %s", file_name);
	    if (system(buf))
	    {
		perror("move failed");
	    }
and the mv doesn't give an error (but I wish it did).
I do it this way because I don't want any flash activity while I'm recording a MIDI file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB flash write protected vijayabcdef Linux - Hardware 4 09-14-2011 11:00 AM
USB Flash Drive, write protect, and flash file system questions storkus Linux - Hardware 4 12-01-2008 04:32 AM
unable to write to my usb flash drive. schneidz Fedora 4 10-13-2008 09:30 PM
Cannot write to USB flash drive leisa Linux - Hardware 7 01-02-2008 09:30 AM
USB Flash drive will not let me write. sniperhf Linux - Hardware 1 12-05-2003 02:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration