0

I have a binary file, where I write the struct with the following fields

typedef struct super_bloque{
    size_t s_filesystem_type;
    size_t s_inodes_count;
    size_t s_blocks_count;
    size_t s_free_blocks_count;
    size_t s_free_inodes_count;
    time_t s_mtime;
    time_t s_umtime;
    size_t s_mnt_count;
    size_t s_magic;
    size_t s_inode_size;
    size_t s_block_size;
    size_t s_first_ino;
    size_t s_first_blo;
    size_t s_bm_inode_start;
    size_t s_bm_block_start;
    size_t s_inode_start;
    size_t s_block_start;    
}SUPER_BLOQUE;

I have the following method where I write the struct inside the file

void Operacion::formatear_ext2(char type, size_t part_start, size_t part_size, FILE *disco){
    MBR mbr;
    fseek(disco, 0, SEEK_SET);
    fread(&mbr, sizeof(mbr), 1, disco);
    size_t n = (size_t)floor((part_size - sizeof(SUPER_BLOQUE)) / (sizeof(I_NODO) + 3 * sizeof(BLOQUE_ARCHIVO) + 4));
    printf("n = %zu\n", n);
    SUPER_BLOQUE super_block;
    memset(&super_block, 0, sizeof(SUPER_BLOQUE));
    super_block.s_filesystem_type = (size_t)2;
    super_block.s_inodes_count = n;
    super_block.s_blocks_count = (size_t)3 * n;
    super_block.s_free_blocks_count = (size_t)3 * n;
    super_block.s_free_inodes_count = n;
    super_block.s_mtime = time(0);
    super_block.s_umtime = time(0);
    super_block.s_mnt_count = 1;
    super_block.s_magic = 0xEF53;
    super_block.s_inode_size = sizeof(I_NODO);
    super_block.s_block_size = sizeof(BLOQUE_ARCHIVO);
    super_block.s_first_ino = 0;
    super_block.s_first_blo = 0;
    super_block.s_bm_inode_start = part_start + sizeof(SUPER_BLOQUE);
    super_block.s_bm_block_start = part_start + sizeof(SUPER_BLOQUE) + n;
    super_block.s_inode_start = part_start + sizeof(SUPER_BLOQUE) + n + 3 * n;
    super_block.s_block_start = part_start + sizeof(SUPER_BLOQUE) + n + 3 * n + n * sizeof(I_NODO);
    fseek(disco, part_start, SEEK_SET);
    fwrite(&super_block, sizeof(SUPER_BLOQUE), 1, disco);

    for(size_t i = 0; i < super_block.s_inodes_count; i++){
       fseek(disco, super_block.s_first_ino + i, SEEK_SET);
       fwrite("0", 1, 1, disco);
    }
    for(size_t i = 0; i < super_block.s_blocks_count; i++){ 
       fseek(disco, super_block.s_first_blo + i, SEEK_SET);
       fwrite("0", 1, 1, disco);
    }
    if(type == '1'){ 
        size_t tam_ino_bloq = super_block.s_block_start + n * sizeof(BLOQUE_ARCHIVO); 
       for(size_t i = super_block.s_inode_start; i < tam_ino_bloq; i++){
        fseek(disco, i, SEEK_SET);
        fwrite("\0", 1, 1, disco);
      }
   }

Then I read the struct inside the disk to check if the data was stored correctly

fseek(disco, part_start, SEEK_SET);
    fread(&tmp_sb, sizeof(SUPER_BLOQUE), 1, disco);
    printf("**************************** SUPERBLOQUE ****************************\n");
    printf("fs: %zu\n", tmp_sb.s_filesystem_type);
    printf("i-nodes count : %zu\n", tmp_sb.s_inodes_count);
    printf("blocks count : %zu\n", tmp_sb.s_blocks_count);
    printf("free blocks count: %zu\n", tmp_sb.s_free_blocks_count);
    printf("free i-nodes count: %zu\n", tmp_sb.s_free_inodes_count);
    printf("i-nodes size: %zu\n", tmp_sb.s_inode_size);
    printf("blocks size: %zu\n", tmp_sb.s_block_size);
    printf("first ino: %zu\n", tmp_sb.s_first_ino);
    printf("first blo: %zu\n", tmp_sb.s_first_blo);
    printf("bm i-node start: %zu\n", tmp_sb.s_bm_inode_start);
    printf("bm block start: %zu\n", tmp_sb.s_bm_block_start);
    printf("i-node start: %zu\n", tmp_sb.s_inode_start);
    printf("block start: %zu\n", tmp_sb.s_block_start);
    printf("***********************************************************************");
} // Finish the method

The struct has the following information when writing it

fs: 2
i-nodes count : 64
blocks count : 192
free blocks count: 192
free i-nodes count: 64
i-nodes size: 120
blocks size: 64
first ino: 0
first blo: 0
bm i-node start: 272
bm block start: 336
i-node start: 528
block start: 8208

But when I read the information I get the following result, information has been lost

fs: 3472328296227680304
i-nodes count : 3472328296227680304
blocks count : 3472328296227680304
free blocks count: 3472328296227680304
free i-nodes count: 3472328296227680304
i-nodes size: 120
blocks size: 64
first ino: 0
first blo: 0
bm i-node start: 272
bm block start: 336
i-node start: 528
block start: 8208

The first five fields contain garbage. Why does this happen? I appreciate any help

NOTE: I have added all the lines of the method. I am working with Ubuntu distribution. To open the file I use the following instructions

FILE *disco;
disco = fopen(path, "r+b");
if(disco){                        
  if(cmd.fs == '2'){
    formatear_ext2(cmd.type, meta->part_start, meta->part_size, disco);
  }else if(cmd.fs == '3'){
    formatear_ext3(cmd.type, meta->part_start, meta->part_size, disco);                
  }else{
    printf("ERROR\n");
  }
}else{
  printf("ERROR: No se pudo acceder al dispositivo.\n");
}
7
  • At first glance I can't see anything wrong but there's an errant "partición" in the code you've shown which makes me think it's not actually the real code. You are also not doing any error checking. Please create a minimal reproducible example. Commented Mar 31, 2020 at 6:40
  • would you put more code so that others can reproduce this? Commented Mar 31, 2020 at 6:42
  • Are you on Windows? Did you open the file as binary file? If not the OS will remove bytes which it guesses to be a carriage-return ('\r') which is, of course, non-sense in your case and has to be prevented. Commented Mar 31, 2020 at 6:51
  • That's my thought too, you must open the file in binary mode. "wb" and "rb" in your call to fopen Commented Mar 31, 2020 at 6:52
  • 2
    Yet another observation: 3472328296227680304 in hex.: 3030303030303030. That looks very suspicious... Commented Mar 31, 2020 at 6:53

1 Answer 1

1

after you adding some code, i still don't know what is cmd,meta,I_NODO,BLOQUE_ARCHIVO... and what are theirs values.
i tried to surmise values from log so that can compile & run, i found the value 3472328296227680304, like @Scheff said, 0x3030303030303030 was wrote by the code below(value of "0" is 0x30)

    for(size_t i = 0; i < super_block.s_inodes_count; i++){
       fseek(disco, super_block.s_first_ino + i, SEEK_SET);
       fwrite("0", 1, 1, disco);
    }
    for(size_t i = 0; i < super_block.s_blocks_count; i++){ 
       fseek(disco, super_block.s_first_blo + i, SEEK_SET);
       fwrite("0", 1, 1, disco);
    }

the odd value won't appear if i comment out the part of code
Because the loss of information, i can't point out the bug precisely, but i think this will be helpful
i suggest you add some debug code, like print out super_block.s_first_ino value, to find the bug.

Sign up to request clarification or add additional context in comments.

3 Comments

Is that supposed to be fwrite("\0", 1, 1, disco); to zero out the area instead of a series of ASCII '0' character values?
@AndrewHenle i think the bug is the incorrect write position. even if fwrite("\0"), the result won't be correct, because values(which was 0x3030...) will change to 0. no matter what value wrote, the write position should be checked.
@AndrewHenle i guess those fwrite("0")(or '\0') was be designed to be wrote into body area after head info, but now they are incorrect wrote into head info area.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.