how to get device block number of a fileHow can I redirect and append both stdout and stderr to a file with Bash?Looping through the content of a file in BashHow to symlink a file in Linux?How do I grep recursively?How do I change permissions for a folder and all of its subfolders and files in one step in Linux?How to get full path of a file?How can I recursively find all files in current and subfolders based on wildcard matching?How do I delete an exported environment variable?How do I copy folder with files to another folder in Unix/Linux?How do I find all files containing specific text on Linux?

Why did Theresa May offer a vote on a second Brexit referendum?

Why would Ryanair allow me to book this journey through a third party, but not through their own website?

role of -られ, -し, and construction of the phrase

Best material to absorb as much light as possible

Why were helmets and other body armour not commonplace in the 1800s?

Using credit/debit card details vs swiping a card in a payment (credit card) terminal

What does $!# mean in Shell scripting?

Should one buy new hardware after a system compromise?

Where's this lookout in Nova Scotia?

Count rotary dial pulses in a phone number (including letters)

Is the Indo-European language family made up?

How to cut a climbing rope?

Parallel fifths in the orchestra

Did this character show any indication of wanting to rule before S8E6?

Have 1.5% of all nuclear reactors ever built melted down?

Value of a binomial series

Is Jon Snow the last of his House?

Why did Jon Snow do this immoral act if he is so honorable?

Count Even Digits In Number

Ethical issue - how can I better document what is happening?

Does pair production happen even when the photon is around a neutron?

Why does Mjolnir fall down in Age of Ultron but not in Endgame?

Does COBRA make sense anymore with the ACA?

Ingress filtering on edge routers and performance concerns



how to get device block number of a file


How can I redirect and append both stdout and stderr to a file with Bash?Looping through the content of a file in BashHow to symlink a file in Linux?How do I grep recursively?How do I change permissions for a folder and all of its subfolders and files in one step in Linux?How to get full path of a file?How can I recursively find all files in current and subfolders based on wildcard matching?How do I delete an exported environment variable?How do I copy folder with files to another folder in Unix/Linux?How do I find all files containing specific text on Linux?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am using the code below to get the block number of a file:



int get_block (int fd, int logical_block)

int ret;
ret = ioctl (fd, FIBMAP, &logical_block);
if (ret <0 )
perror ("ioctl");
return -1;

return logical_block;

int get_nr_blocks (int fd)

struct stat buf;
int ret,blocks_in_4k;
ret = fstat (fd, &buf);
if ( ret < 0 )
perror ("fstat");
return -1;

blocks_in_4k = buf.st_blocks/8;
return blocks_in_4k;

void print_blocks (int fd)

int nr_blocks,i;
int f_phys_block,e_phys_block;
nr_blocks = get_nr_blocks (fd);
if (nr_blocks <0 )
fprintf (stderr, "get_nr_blocks failed!n");
return;

if (nr_blocks == 0)
printf( "no allocated blocksn");
return;
else if ( nr_blocks == 1)
printf ("1 blocknn");
else
printf ("this file has %d blocksnn",nr_blocks);

for (i =0; i <nr_blocks; i++)
int phys_block;
phys_block = get_block (fd, i );
if (phys_block <0 )
fprintf (stderr, "get_block failed!n");
return;

if ( !phys_block)
continue;
if ( i == 0 )
f_phys_block=phys_block;
if ( i == nr_blocks -1 )
e_phys_block=phys_block;
printf ("(%u, %u),",i,phys_block);

if ( nr_blocks != e_phys_block - f_phys_block + 1)
printf ("nthis file is fragmented n");
printf ("total blocks <%u>,first physical block <%u>, the last physical block <%u>n",nr_blocks,f_phys_block,e_phys_block);

putchar ('n');

int main (int argc, char *argv[] )

int fd;
if (argc <2 )
fprintf (stderr, "usage: %s <file>n",argv[0]);

fd = open (argv[1],O_RDONLY);
if (fd <0)
perror ("open");
return 1;

print_blocks(fd);
return 0;



It gives me output like this:
this file has 32 blocks (4kb per block)
(0, 99596),(1, 99597),(2, 99598),(3, 99599)
But after I made the image of the device using command dd, which the file is actually stored in, the block number didn't indicate the real block offset of the file. What should I do to get the real device block offset?










share|improve this question






















  • after I made the image of the device using command dd, which the file is actually stored in how? You can create a function from your program and add system("dd ...") call to make an MCVE.

    – Kamil Cuk
    Mar 24 at 3:12











  • I know the file is on the device "/dev/block/mmcblk0" and I want to get the offset of the file in the image. What is MCVE

    – S. Ezreal
    Mar 24 at 3:40











  • MCVE. "the file is on the device" - there is also filesystem layer in between. So you have a file, within a filesystem, within a partition on a block device (sdcard). You want to know on which byte within the block device the file starts, right? What filesystem are you using?

    – Kamil Cuk
    Mar 24 at 3:43












  • I think the best and most reliable way is to write a long unique string to the file. Then open the /dev/block/mmcblk0 as a normal file and search for the string. A filesystem can compress the file, duplicate it's contents etc. etc.

    – Kamil Cuk
    Mar 24 at 3:50











  • I am doing this on android so the filesystem is ext4. I tried this way but it's a relatively time-costy operation and it want to do it faster. Can you give me a clue?

    – S. Ezreal
    Mar 24 at 4:09


















0















I am using the code below to get the block number of a file:



int get_block (int fd, int logical_block)

int ret;
ret = ioctl (fd, FIBMAP, &logical_block);
if (ret <0 )
perror ("ioctl");
return -1;

return logical_block;

int get_nr_blocks (int fd)

struct stat buf;
int ret,blocks_in_4k;
ret = fstat (fd, &buf);
if ( ret < 0 )
perror ("fstat");
return -1;

blocks_in_4k = buf.st_blocks/8;
return blocks_in_4k;

void print_blocks (int fd)

int nr_blocks,i;
int f_phys_block,e_phys_block;
nr_blocks = get_nr_blocks (fd);
if (nr_blocks <0 )
fprintf (stderr, "get_nr_blocks failed!n");
return;

if (nr_blocks == 0)
printf( "no allocated blocksn");
return;
else if ( nr_blocks == 1)
printf ("1 blocknn");
else
printf ("this file has %d blocksnn",nr_blocks);

for (i =0; i <nr_blocks; i++)
int phys_block;
phys_block = get_block (fd, i );
if (phys_block <0 )
fprintf (stderr, "get_block failed!n");
return;

if ( !phys_block)
continue;
if ( i == 0 )
f_phys_block=phys_block;
if ( i == nr_blocks -1 )
e_phys_block=phys_block;
printf ("(%u, %u),",i,phys_block);

if ( nr_blocks != e_phys_block - f_phys_block + 1)
printf ("nthis file is fragmented n");
printf ("total blocks <%u>,first physical block <%u>, the last physical block <%u>n",nr_blocks,f_phys_block,e_phys_block);

putchar ('n');

int main (int argc, char *argv[] )

int fd;
if (argc <2 )
fprintf (stderr, "usage: %s <file>n",argv[0]);

fd = open (argv[1],O_RDONLY);
if (fd <0)
perror ("open");
return 1;

print_blocks(fd);
return 0;



It gives me output like this:
this file has 32 blocks (4kb per block)
(0, 99596),(1, 99597),(2, 99598),(3, 99599)
But after I made the image of the device using command dd, which the file is actually stored in, the block number didn't indicate the real block offset of the file. What should I do to get the real device block offset?










share|improve this question






















  • after I made the image of the device using command dd, which the file is actually stored in how? You can create a function from your program and add system("dd ...") call to make an MCVE.

    – Kamil Cuk
    Mar 24 at 3:12











  • I know the file is on the device "/dev/block/mmcblk0" and I want to get the offset of the file in the image. What is MCVE

    – S. Ezreal
    Mar 24 at 3:40











  • MCVE. "the file is on the device" - there is also filesystem layer in between. So you have a file, within a filesystem, within a partition on a block device (sdcard). You want to know on which byte within the block device the file starts, right? What filesystem are you using?

    – Kamil Cuk
    Mar 24 at 3:43












  • I think the best and most reliable way is to write a long unique string to the file. Then open the /dev/block/mmcblk0 as a normal file and search for the string. A filesystem can compress the file, duplicate it's contents etc. etc.

    – Kamil Cuk
    Mar 24 at 3:50











  • I am doing this on android so the filesystem is ext4. I tried this way but it's a relatively time-costy operation and it want to do it faster. Can you give me a clue?

    – S. Ezreal
    Mar 24 at 4:09














0












0








0








I am using the code below to get the block number of a file:



int get_block (int fd, int logical_block)

int ret;
ret = ioctl (fd, FIBMAP, &logical_block);
if (ret <0 )
perror ("ioctl");
return -1;

return logical_block;

int get_nr_blocks (int fd)

struct stat buf;
int ret,blocks_in_4k;
ret = fstat (fd, &buf);
if ( ret < 0 )
perror ("fstat");
return -1;

blocks_in_4k = buf.st_blocks/8;
return blocks_in_4k;

void print_blocks (int fd)

int nr_blocks,i;
int f_phys_block,e_phys_block;
nr_blocks = get_nr_blocks (fd);
if (nr_blocks <0 )
fprintf (stderr, "get_nr_blocks failed!n");
return;

if (nr_blocks == 0)
printf( "no allocated blocksn");
return;
else if ( nr_blocks == 1)
printf ("1 blocknn");
else
printf ("this file has %d blocksnn",nr_blocks);

for (i =0; i <nr_blocks; i++)
int phys_block;
phys_block = get_block (fd, i );
if (phys_block <0 )
fprintf (stderr, "get_block failed!n");
return;

if ( !phys_block)
continue;
if ( i == 0 )
f_phys_block=phys_block;
if ( i == nr_blocks -1 )
e_phys_block=phys_block;
printf ("(%u, %u),",i,phys_block);

if ( nr_blocks != e_phys_block - f_phys_block + 1)
printf ("nthis file is fragmented n");
printf ("total blocks <%u>,first physical block <%u>, the last physical block <%u>n",nr_blocks,f_phys_block,e_phys_block);

putchar ('n');

int main (int argc, char *argv[] )

int fd;
if (argc <2 )
fprintf (stderr, "usage: %s <file>n",argv[0]);

fd = open (argv[1],O_RDONLY);
if (fd <0)
perror ("open");
return 1;

print_blocks(fd);
return 0;



It gives me output like this:
this file has 32 blocks (4kb per block)
(0, 99596),(1, 99597),(2, 99598),(3, 99599)
But after I made the image of the device using command dd, which the file is actually stored in, the block number didn't indicate the real block offset of the file. What should I do to get the real device block offset?










share|improve this question














I am using the code below to get the block number of a file:



int get_block (int fd, int logical_block)

int ret;
ret = ioctl (fd, FIBMAP, &logical_block);
if (ret <0 )
perror ("ioctl");
return -1;

return logical_block;

int get_nr_blocks (int fd)

struct stat buf;
int ret,blocks_in_4k;
ret = fstat (fd, &buf);
if ( ret < 0 )
perror ("fstat");
return -1;

blocks_in_4k = buf.st_blocks/8;
return blocks_in_4k;

void print_blocks (int fd)

int nr_blocks,i;
int f_phys_block,e_phys_block;
nr_blocks = get_nr_blocks (fd);
if (nr_blocks <0 )
fprintf (stderr, "get_nr_blocks failed!n");
return;

if (nr_blocks == 0)
printf( "no allocated blocksn");
return;
else if ( nr_blocks == 1)
printf ("1 blocknn");
else
printf ("this file has %d blocksnn",nr_blocks);

for (i =0; i <nr_blocks; i++)
int phys_block;
phys_block = get_block (fd, i );
if (phys_block <0 )
fprintf (stderr, "get_block failed!n");
return;

if ( !phys_block)
continue;
if ( i == 0 )
f_phys_block=phys_block;
if ( i == nr_blocks -1 )
e_phys_block=phys_block;
printf ("(%u, %u),",i,phys_block);

if ( nr_blocks != e_phys_block - f_phys_block + 1)
printf ("nthis file is fragmented n");
printf ("total blocks <%u>,first physical block <%u>, the last physical block <%u>n",nr_blocks,f_phys_block,e_phys_block);

putchar ('n');

int main (int argc, char *argv[] )

int fd;
if (argc <2 )
fprintf (stderr, "usage: %s <file>n",argv[0]);

fd = open (argv[1],O_RDONLY);
if (fd <0)
perror ("open");
return 1;

print_blocks(fd);
return 0;



It gives me output like this:
this file has 32 blocks (4kb per block)
(0, 99596),(1, 99597),(2, 99598),(3, 99599)
But after I made the image of the device using command dd, which the file is actually stored in, the block number didn't indicate the real block offset of the file. What should I do to get the real device block offset?







linux ioctl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 2:45









S. EzrealS. Ezreal

1




1












  • after I made the image of the device using command dd, which the file is actually stored in how? You can create a function from your program and add system("dd ...") call to make an MCVE.

    – Kamil Cuk
    Mar 24 at 3:12











  • I know the file is on the device "/dev/block/mmcblk0" and I want to get the offset of the file in the image. What is MCVE

    – S. Ezreal
    Mar 24 at 3:40











  • MCVE. "the file is on the device" - there is also filesystem layer in between. So you have a file, within a filesystem, within a partition on a block device (sdcard). You want to know on which byte within the block device the file starts, right? What filesystem are you using?

    – Kamil Cuk
    Mar 24 at 3:43












  • I think the best and most reliable way is to write a long unique string to the file. Then open the /dev/block/mmcblk0 as a normal file and search for the string. A filesystem can compress the file, duplicate it's contents etc. etc.

    – Kamil Cuk
    Mar 24 at 3:50











  • I am doing this on android so the filesystem is ext4. I tried this way but it's a relatively time-costy operation and it want to do it faster. Can you give me a clue?

    – S. Ezreal
    Mar 24 at 4:09


















  • after I made the image of the device using command dd, which the file is actually stored in how? You can create a function from your program and add system("dd ...") call to make an MCVE.

    – Kamil Cuk
    Mar 24 at 3:12











  • I know the file is on the device "/dev/block/mmcblk0" and I want to get the offset of the file in the image. What is MCVE

    – S. Ezreal
    Mar 24 at 3:40











  • MCVE. "the file is on the device" - there is also filesystem layer in between. So you have a file, within a filesystem, within a partition on a block device (sdcard). You want to know on which byte within the block device the file starts, right? What filesystem are you using?

    – Kamil Cuk
    Mar 24 at 3:43












  • I think the best and most reliable way is to write a long unique string to the file. Then open the /dev/block/mmcblk0 as a normal file and search for the string. A filesystem can compress the file, duplicate it's contents etc. etc.

    – Kamil Cuk
    Mar 24 at 3:50











  • I am doing this on android so the filesystem is ext4. I tried this way but it's a relatively time-costy operation and it want to do it faster. Can you give me a clue?

    – S. Ezreal
    Mar 24 at 4:09

















after I made the image of the device using command dd, which the file is actually stored in how? You can create a function from your program and add system("dd ...") call to make an MCVE.

– Kamil Cuk
Mar 24 at 3:12





after I made the image of the device using command dd, which the file is actually stored in how? You can create a function from your program and add system("dd ...") call to make an MCVE.

– Kamil Cuk
Mar 24 at 3:12













I know the file is on the device "/dev/block/mmcblk0" and I want to get the offset of the file in the image. What is MCVE

– S. Ezreal
Mar 24 at 3:40





I know the file is on the device "/dev/block/mmcblk0" and I want to get the offset of the file in the image. What is MCVE

– S. Ezreal
Mar 24 at 3:40













MCVE. "the file is on the device" - there is also filesystem layer in between. So you have a file, within a filesystem, within a partition on a block device (sdcard). You want to know on which byte within the block device the file starts, right? What filesystem are you using?

– Kamil Cuk
Mar 24 at 3:43






MCVE. "the file is on the device" - there is also filesystem layer in between. So you have a file, within a filesystem, within a partition on a block device (sdcard). You want to know on which byte within the block device the file starts, right? What filesystem are you using?

– Kamil Cuk
Mar 24 at 3:43














I think the best and most reliable way is to write a long unique string to the file. Then open the /dev/block/mmcblk0 as a normal file and search for the string. A filesystem can compress the file, duplicate it's contents etc. etc.

– Kamil Cuk
Mar 24 at 3:50





I think the best and most reliable way is to write a long unique string to the file. Then open the /dev/block/mmcblk0 as a normal file and search for the string. A filesystem can compress the file, duplicate it's contents etc. etc.

– Kamil Cuk
Mar 24 at 3:50













I am doing this on android so the filesystem is ext4. I tried this way but it's a relatively time-costy operation and it want to do it faster. Can you give me a clue?

– S. Ezreal
Mar 24 at 4:09






I am doing this on android so the filesystem is ext4. I tried this way but it's a relatively time-costy operation and it want to do it faster. Can you give me a clue?

– S. Ezreal
Mar 24 at 4:09













0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320304%2fhow-to-get-device-block-number-of-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320304%2fhow-to-get-device-block-number-of-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?