在c linux中改变块设备的大小

时间:2016-10-04 11:49:18

标签: c linux file block device

我编写了一个应该改变任何块设备大小的函数 lsblk命令给出了这个输出

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0 457.9G  0 part /
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0   7.9G  0 part [SWAP]

我的功能是 - >

int _some_func(
            int path,
                int size)
{
        struct  stat stat_buf;

        /*check if file is valid*/ 
        if(!((fcntl(path, F_GETFL) != -1) || (errno != EBADF)))
        {
                perror("invalid file descriptor\n");
                return -1;
        }

        /*get file properties in stat_buf*/
        if( -1 == fstat(path, &stat_buf))
        {
                perror("error in stat\n");
                return -1;
        }

        /*return error -1 if file is not block device*/
        if((stat_buf.st_mode & S_IFMT) != S_IFBLK)
        {
                perror("not a block device\n");
                return -1;
        }

        return(ftruncate(path,size) );

}

int main()
{
        long int curr_size = 0;
        struct  stat stat_buf;
        int fd = open("/dev/sda1",O_RDWR,0777);
        if(fd < 0)  perror("error\n");

        /*get file properties in stat_buf*/
        if( -1 == fstat(fd, &stat_buf))
        {
                perror("error in stat\n");
                return -1;
        }

        curr_size = stat_buf.st_size;
        printf("current size =%ld\n",curr_size);
        if(_ss_size(fd,curr_size+50) == 0)
        {
                /*get file properties in stat_buf*/
                if( -1 == fstat(fd, &stat_buf))
                {
                        perror("error in stat\n");
                        return -1;
                }

                curr_size = stat_buf.st_size;
                printf("changed size =%ld\n",curr_size);

        }
        else
        {
                perror("error in _ss_size\n");
        }



}

输出是&gt; 当前大小= 0 _ss_size中的错误

:参数无效

1&gt;为什么当前大小&#34; / dev / sda1&#34;显示为0,但在lsblk中,大小为= 457.9G 2&gt;为什么_ss_size中出现错误 :无效的参数即将到来(这是返回值) ftruncate命令。

如果ftruncate不起作用,那么哪个sys调用会起作用?

0 个答案:

没有答案