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;
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
add a comment |
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
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 addsystem("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
add a comment |
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
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
linux ioctl
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 addsystem("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
add a comment |
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 addsystem("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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 addsystem("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