About opening USB memory files with write protection

Asked 2 years ago, Updated 2 years ago, 44 views

I have a question about USB memory with write protection (buffalo, unknown model number) in an embedded Linux environment.
This memory has a SW so you can turn on/off write protection.

Mounting this memory with Protect ON

 mount:/dev/sda1 is write-protected, mounting read-only

You can mount the message.
In this state, opening the file with read attributes will result in an error:

fp=fopen("/mdeia/usb/test.txt", "r");

When opened, an error occurs and errno=2 returns.
You can mount it manually from the command line and view the contents of the file in cat.

Protect OFF will open normally.

The ls-al file attributes show no difference between Protect ON/OFF.

Is it impossible to open with fopen() with Protect ON?

fd=open("/media/usb/test.txt", O_RDONLY);

I also tried it.It's the same as fopen(), but -1 was returned for Protect ON and opened for Protect OFF.

linux c

2022-09-30 16:54

1 Answers

I looked at the GNU cat source.

If you don't have an option, you might want to try it because it seems to be loaded with O_RDWR|O_BINARY.

 if (!( number | | show_ends | | squeeze_blank )
    {
      file_open_mode | = O_BINARY;
      xset_binary_mode (STDOUT_FILENO, O_BINARY);
    }

  /* Check if any of the input files are the same as the output file.*/

  /* Main loop.*/

  file="-";
  argind = optind;

  do
    {
      if(argind<argc)
        infile=argv [argind];

      if(STREQ(infile, "-"))
        {
          have_read_stdin = true;
          input_desc = STDIN_FILENO;
          if(file_open_mode&O_BINARY)
            xset_binary_mode(STDIN_FILENO, O_BINARY);
        }
      else
        {
          input_desc=open(infile,file_open_mode);
          if(input_desc<0)
            {
              error(0, errno, "%s", quotef(infile)));
              ok = false;
              continue;
            }
        }


2022-09-30 16:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.