LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   write file to a specific sector (https://www.linuxquestions.org/questions/linux-general-1/write-file-to-a-specific-sector-816789/)

wiener 06-28-2010 08:13 AM

write file to a specific sector
 
Hello,
I need to copy a file into a Flash memory which is connected to my computer via USB. The file must start at a specific sector.
Can anyone guide me how to do this? (it can be through a C program, a line command, or any other way)
Thanks in advance,
Moshe

pixellany 06-28-2010 08:20 AM

How is this file to be accessed? If it must be accessed using a normal filesystem call, then I think you cannot bypass that and put it on a specific sector.

Regardless, you can use dd to put the file **exactly** where you want it. "man dd" for all the details---or go here:
http://www.linuxquestions.org/questi...ommand-362506/

wiener 06-28-2010 08:31 AM

I need the file to start at sector 300, and that the file will appear in a standard way in the directory, so it can be normaly read by file system.
I'll check what the 'dd' utility can offer.
Many thanks.

pixellany 06-28-2010 08:39 AM

Quote:

Originally Posted by wiener (Post 4017172)
I need the file to start at sector 300, and that the file will appear in a standard way in the directory, so it can be normaly read by file system.

My hunch is that this is over-constrained----let us know how it turns out.

syg00 06-28-2010 08:51 AM

Nope, you can only appear to write to a specific sector - dd included.
Think about what hapens if that sector is "bad" and has been remapped by the hardware. Fruitless demand IMHO.

pixellany 06-28-2010 09:00 AM

Assuming that the hardware manages bad sectors independently of how you write to the drive, then there is an unambiguous location at a fixed offset using dd.

Suppose you do this:
dd if=filename of=/dev/sdb1 bs=512 seek=10
This will write the contents of "filename" beginning at what **appears to be** sector 10 of device sdb1. You can unambiguously recover the data with another dd command. My earlier point was that I believe you CANNOT recover the file with a normal filesystem command.

wiener 06-28-2010 04:06 PM

Quote:

Originally Posted by pixellany (Post 4017205)
Assuming that the hardware manages bad sectors independently of how you write to the drive, then there is an unambiguous location at a fixed offset using dd.

Suppose you do this:
dd if=filename of=/dev/sdb1 bs=512 seek=10
This will write the contents of "filename" beginning at what **appears to be** sector 10 of device sdb1. You can unambiguously recover the data with another dd command. My earlier point was that I believe you CANNOT recover the file with a normal filesystem command.

I guess that this will be good as the first step - writing the file at the sector that is assumed to be the sector-number I need. There is no real danger of overriding other staff- as this action I do after formating this device, so this file should be the only file on the disk.
I guess now I need to write a new entry in the directory, which will map the occupied sectores with the file.
Is there an API which I can use for manipulating the directory of the disk?

pixellany 06-28-2010 04:26 PM

I am still very confused about what you are trying to do....

First, why not let the file system do its job?

Second, assuming that a file system is in place, doing a low-level write with something like dd will very likely damage that filesystem---thus what you are calling "directories" will no longer exist. To be precise, the structures used in filesystems to locate data are called "inodes". Directories are used simply to flag files which are to be viewed together, and do not relate to physical storage on the disk.

Third, when you do a low-level write, the data will go on the disk contiguously (unless you write a more complex routine to split it up.) Thus, for example, suppose you have a file with 5270 bytes, and you write is starting at the beginning of the 3rd sector. It will fill (contiguously) sectors 3 thru 12, plus 150 bytes of sector 13. This is almost certainly NOT how a file system would write it.

wiener 06-28-2010 04:59 PM

Quote:

Originally Posted by pixellany (Post 4017615)
I am still very confused about what you are trying to do....

First, why not let the file system do its job?

Second, assuming that a file system is in place, doing a low-level write with something like dd will very likely damage that filesystem---thus what you are calling "directories" will no longer exist. To be precise, the structures used in filesystems to locate data are called "inodes". Directories are used simply to flag files which are to be viewed together, and do not relate to physical storage on the disk.

Third, when you do a low-level write, the data will go on the disk contiguously (unless you write a more complex routine to split it up.) Thus, for example, suppose you have a file with 5270 bytes, and you write is starting at the beginning of the 3rd sector. It will fill (contiguously) sectors 3 thru 12, plus 150 bytes of sector 13. This is almost certainly NOT how a file system would write it.

Let me explain my need:
I have an embaded system (a communication card) which reads a flash memory. When this system powers up, the program that is burned in the ROM on this system starts. It reads the flash memory and looks for a certain file that starts at sector 300. Later, this file starts executing instead of the ROM program. This second program already expects the flash to be formated with DOS file system, and it reads flies in the regular way, by filename.
So, I formated the flash memory, and now I wish to write this particular file on sector 300, and some other files anywhere else on the disk.
If I just use 'dd' to write the first file on sector 300, and then just copy files into this flash, there is a good chance that some file will override my first file - unless I write it in the directory, so any file system that will attempt to add a file to this storage, will see that those sectors starting from 300, are occupied.


All times are GMT -5. The time now is 04:51 PM.