1

LBA(論理ブロックアドレス)からデータを読み取るこのプログラムがありますが、提供するLBA番号に関係なく、同じ出力が得られます。

どうすれば検証できますか?

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <linux/fs.h>
//#include "common.h"

typedef unsigned long long int var64;


int getSectorSize(int handle)
{
        int sectorSize = 0;

        //get the physical sector size of the disk
        if (ioctl(handle, BLKSSZGET, &sectorSize)) {

                printf("getSectorSize: Reading physical sector size failed.\n");

                sectorSize = 512;
        }

        return sectorSize;
}



var64 readLBA(int handle, var64 lba, void* buf, var64 bytes)
{
        int ret = 0;
        int sectorSize = getSectorSize(handle);
        var64 offset = lba * sectorSize;

        printf("readFromLBA: entered.\n");

        lseek64(handle, offset, SEEK_SET);
        ret = read(handle, buf, bytes);
        if(ret != bytes) {

              printf("read LBA: read failed.\n");

                return -1;
        }

        printf("read LBA: retval: %lld.\n", ret);
        return ret;
}

int main()
{
  int sectorSize, fd;
  char buff[100];
  printf("Calling getSectorSize\n");

  fd = open("/dev/sda1", O_RDONLY);

  if(fd == -1)
  {
    printf("open /dev/sda1 failed");
    exit(1);
  }
  sectorSize = getSectorSize(fd);
  printf("Sector size = %u\n", sectorSize);
  memset(buff, 0, sizeof(buff)); 
  readLBA(fd, 1, buff, 2); // if i put the 2nd arg as -12378 gives same answer
}

出力は次のとおりです。

sles10-sp3:~ # gcc getSectorSizeMain.c
getSectorSizeMain.c: In function ‘main’:
getSectorSizeMain.c:75: warning: incompatible implicit declaration of built-in function ‘memset’
sles10-sp3:~ # ./a.out
Calling getSectorSize
Sector size = 512
read LBA: entered.
read LBA: retval: 8589934594. // This is always constant, how to validate? If i tell to read an invalid LBA number like -123456 the answer remains same. How to validate?
4

1 に答える 1

3

retval関心のあるデータは含まれていませんが、read()のバイト数がバッファに格納されているため、常に同じ値が含まれているのは当然です。ただし、テスト出力では、単なるintであっても、 "%lld"(long long int)を使用して出力しようとします。そのため、printfは、その値をスタック上の隣にあるものと組み合わせます(8589934594 = = 0x200000002-最後の桁はあなたの値であり、最初の桁はおそらくゴミです)。

チェック/使用/配列バフ内にあるものは何でもしたいデータ。

于 2012-07-06T10:43:08.930 に答える