0

I want to get file block size of two files, using stat struct.

In internet, they say file block size is affected by which device is using.

If that is right, does block size of all files in same partition equal?

I am using blockcmp function, it always return same size. I wonder it is right or i miss something.

Here is my code.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>


struct stat stat1, stat2;
struct tm *time1, *time2;

void filestat1(void);
void filestat2(void);
void filetime1(void);
void filetime2(void);
void sizecmp(void);
void blockcmp(void);
void datecmp(void);
void timecmp(void);

int main(void)
{
    filestat1();
    filestat2();
    filetime1();
    filetime2();
    sizecmp();
    blockcmp();
    datecmp();
    timecmp();
}

void filestat1(void)
{
    // check if there is no text1
    int check = 0;
    check = stat("text1", &stat1);

    if(check != 0)
    {
        printf("Error : there is no text1\n");
    }
    return;
}

void filestat2(void)
{
    // check if there is no text2
    int check = 0;
    check = stat("text2", &stat2);

    if(check != 0)
    {
        printf("Error : there is no text2\n");
    }
    return;
}

void filetime1(void)
{
    time1 = localtime(&stat1.st_mtime);
    return;
}

void filetime2(void)
{
    time2 = localtime(&stat2.st_mtime);
    return;
}


void sizecmp(void)
{
    printf("size compare\n");
    //variable declare
    long long int text1_size;
    long long int text2_size;

    //variable initialize
    text1_size = stat1.st_size;
    text2_size = stat2.st_size;
    if(text1_size > text2_size)
        printf("text1 is bigger\n");
    else if(text1_size < text2_size)
        printf("text2 is bigger\n");
    else
        printf("same size\n");

    printf("\n");
    return;
}

void blockcmp(void)
{
    printf("block compare\n");
    //variable declare
    long long int text1_block_size;
    long long int text2_block_size;
    //variable initialize
    text1_block_size = stat1.st_blocks;
    text2_block_size = stat2.st_blocks;

    if(text1_block_size > text2_block_size)
        printf("text1 is bigger\n");
    else if(text1_block_size < text2_block_size)
        printf("text2 is bigger\n");
    else
        printf("same size\n");

    printf("\n");
    return;
}

void datecmp(void)
{
    printf("date compare\n");
    // compare tm_mon
    if(time1->tm_mon > time2->tm_mon)
        printf("time1 is early \n");
    else if(time1->tm_mon < time2->tm_mon)
        printf("time2 is early \n");
    else{
        // compare tm_mday
        if(time1->tm_mday > time2->tm_mday)
            printf("time1 is early \n");
        else if(time1->tm_mday < time2->tm_mday)
            printf("time2 is early \n");
        // same date
        else
            printf("same time \n");
    }
    printf("\n");
}

void timecmp(void)
{
    printf("time compare\n");
    // compare hour
    if(time1->tm_hour > time2->tm_hour)
        printf("time1 is early \n");
    else if(time1->tm_hour < time2->tm_hour)
        printf("time2 is early \n");
    else{
        // compare minutes
        if(time1->tm_min > time2->tm_min)
            printf("time1 is early \n");
        else if(time1->tm_min < time2->tm_min)
            printf("time2 is early \n");
        // same time
        ;else
            printf("same time \n")
    }
}

`

  • You can check if your C code is doing well by comparing the returned values to: blockdev --getbsz /dev/sda1 (or the device) – Luciano Andress Martini Apr 26 '18 at 17:05
4

The st_blksize field returned by stat() gives the "the preferred blocksize for efficient file system I/O." (from the stat man page). The value should be interpreted as a hint that you should read and write data to files in multiples of this size for efficient I/O. It is, as you say, dependent on the storage device and can also be dependent on the file system. It can be bigger than the logical block size of the hardware device, but is typically limited by the memory page size of the CPU. The logical block size is normally 512 or 4096 bytes on hard disks and 2048 bytes on optical media.

By the way, your code uses the st_blocks field of the stat structure. This field tells how many 512-byte blocks the file occupies. I guess you wanted to use the st_blksize field in you program instead.

  • It is not 512-byte blocks but rather multiples of DEV_BSIZE which is 1024 on HP-UX. – schily Jun 14 '18 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.