LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-28-2010, 08:13 AM   #1
wiener
LQ Newbie
 
Registered: Jun 2009
Posts: 22

Rep: Reputation: 0
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
 
Old 06-28-2010, 08:20 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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/
 
Old 06-28-2010, 08:31 AM   #3
wiener
LQ Newbie
 
Registered: Jun 2009
Posts: 22

Original Poster
Rep: Reputation: 0
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.
 
Old 06-28-2010, 08:39 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by wiener View Post
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.
 
Old 06-28-2010, 08:51 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,102

Rep: Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982
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.
 
Old 06-28-2010, 09:00 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 06-28-2010, 04:06 PM   #7
wiener
LQ Newbie
 
Registered: Jun 2009
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
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?
 
Old 06-28-2010, 04:26 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 06-28-2010, 04:59 PM   #9
wiener
LQ Newbie
 
Registered: Jun 2009
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
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.
 
  


Reply

Tags
sector


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write to a file with specific indentation through php ? wrapster Programming 1 12-12-2008 05:09 AM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM
Writing to a specific sector in a cd Nicole_Tay Linux - Newbie 10 07-07-2008 03:56 AM
write text to specific location in file mcbenus Linux - Desktop 3 02-28-2008 06:40 AM
I/O error cant write to sector 0 of HDD snake15301 Linux - Hardware 1 03-13-2007 01:28 PM


All times are GMT -5. The time now is 05:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration