Why is stat::st_size 0 for devices but at the same time lseek defines the device size correctly?Pyserial persistent configurationWhy is the st_size field in struct stat signed?stat() function not returning correctlycan't create a file larger than 2GB on 64 bit linux system with mmap/malloc/open etcstat to get the size of a file and copy itWhy stat and fstat return the st_size == 0?How to get network device stats?linux c detect attached device, load and link drivers, return the device file system entryCan write(2) return 0 bytes written*, and what to do if it does?Using stat() to determine size for fread()

Does the reader need to like the PoV character?

Is this part of the description of the Archfey warlock's Misty Escape feature redundant?

Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?

Giving feedback to someone without sounding prejudiced

What is the difference between lands and mana?

Creating two special characters

What is Cash Advance APR?

Mimic lecturing on blackboard, facing audience

Non-trope happy ending?

"It doesn't matter" or "it won't matter"?

Does "he squandered his car on drink" sound natural?

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

How does electrical safety system work on ISS?

Did the UK lift the requirement for registering SIM cards?

How much of a Devil Fruit must be consumed to gain the power?

Why is the Sun approximated as a black body at ~ 5800 K?

What is the highest possible scrabble score for placing a single tile

Pre-mixing cryogenic fuels and using only one fuel tank

C++ copy constructor called at return

How much theory knowledge is actually used while playing?

How to make money from a browser who sees 5 seconds into the future of any web page?

Why is it that I can sometimes guess the next note?

awk assign to multiple variables at once

Why do ¬, ∀ and ∃ have the same precedence?



Why is stat::st_size 0 for devices but at the same time lseek defines the device size correctly?


Pyserial persistent configurationWhy is the st_size field in struct stat signed?stat() function not returning correctlycan't create a file larger than 2GB on 64 bit linux system with mmap/malloc/open etcstat to get the size of a file and copy itWhy stat and fstat return the st_size == 0?How to get network device stats?linux c detect attached device, load and link drivers, return the device file system entryCan write(2) return 0 bytes written*, and what to do if it does?Using stat() to determine size for fread()













12















I noticed that when I query the size of a device using open + lseek, everything is OK, but when I stat the device I get zero instead of the real device size. The device is clean without any file system and the first bytes of device start with some text like "1234567890ABC". What is wrong?



The code:



#include <sys/stat.h>
#include <dirent.h>

bool
GetFileSize(const char* pPath, uint64_t& Size)

pPath = "/home/sw/.bashrc";
pPath = "/dev/sda";

struct stat buffer;
if (stat(pPath, &buffer))

printf("Failed to stat file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


printf("File size by stat: %" PRIu64 " WTF?n", buffer.st_size);

//
// Note: It's strange, but stat::st_size from the stat call is zero for devices
//

int File = open(pPath, O_RDONLY);
if (File < 0)

printf("Failed to open file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


long off = lseek(File, 0, SEEK_END);
if (off == (off_t)-1)

printf("Failed to get file size. Error: %s. FilePath: %sn", strerror(errno), pPath);
close(File);
return false;

close(File);

printf("File size by lseek: %" PRIu64 "n", off);
fflush(stdout);

Size = off;
return true;



Output:



File size by stat: 0 WTF?
File size by lseek: 34359738368


If I use stat for a regular file then everything is OK (comment out the line with "/dev/sda"):



File size by stat: 4019 WTF?
File size by lseek: 4019









share|improve this question



















  • 2





    welcome to "stat information is different from seeking+telling information".

    – Jean-François Fabre
    Mar 14 at 14:02






  • 2





    related: unix.stackexchange.com/questions/384488/…

    – Jean-François Fabre
    Mar 14 at 14:05






  • 4





    I don’t trust printing the size using PRIu64 unless you cast the size to uint64_t. That said, you’d probably not get zero if it’s going wrong.

    – Jonathan Leffler
    Mar 14 at 14:08















12















I noticed that when I query the size of a device using open + lseek, everything is OK, but when I stat the device I get zero instead of the real device size. The device is clean without any file system and the first bytes of device start with some text like "1234567890ABC". What is wrong?



The code:



#include <sys/stat.h>
#include <dirent.h>

bool
GetFileSize(const char* pPath, uint64_t& Size)

pPath = "/home/sw/.bashrc";
pPath = "/dev/sda";

struct stat buffer;
if (stat(pPath, &buffer))

printf("Failed to stat file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


printf("File size by stat: %" PRIu64 " WTF?n", buffer.st_size);

//
// Note: It's strange, but stat::st_size from the stat call is zero for devices
//

int File = open(pPath, O_RDONLY);
if (File < 0)

printf("Failed to open file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


long off = lseek(File, 0, SEEK_END);
if (off == (off_t)-1)

printf("Failed to get file size. Error: %s. FilePath: %sn", strerror(errno), pPath);
close(File);
return false;

close(File);

printf("File size by lseek: %" PRIu64 "n", off);
fflush(stdout);

Size = off;
return true;



Output:



File size by stat: 0 WTF?
File size by lseek: 34359738368


If I use stat for a regular file then everything is OK (comment out the line with "/dev/sda"):



File size by stat: 4019 WTF?
File size by lseek: 4019









share|improve this question



















  • 2





    welcome to "stat information is different from seeking+telling information".

    – Jean-François Fabre
    Mar 14 at 14:02






  • 2





    related: unix.stackexchange.com/questions/384488/…

    – Jean-François Fabre
    Mar 14 at 14:05






  • 4





    I don’t trust printing the size using PRIu64 unless you cast the size to uint64_t. That said, you’d probably not get zero if it’s going wrong.

    – Jonathan Leffler
    Mar 14 at 14:08













12












12








12








I noticed that when I query the size of a device using open + lseek, everything is OK, but when I stat the device I get zero instead of the real device size. The device is clean without any file system and the first bytes of device start with some text like "1234567890ABC". What is wrong?



The code:



#include <sys/stat.h>
#include <dirent.h>

bool
GetFileSize(const char* pPath, uint64_t& Size)

pPath = "/home/sw/.bashrc";
pPath = "/dev/sda";

struct stat buffer;
if (stat(pPath, &buffer))

printf("Failed to stat file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


printf("File size by stat: %" PRIu64 " WTF?n", buffer.st_size);

//
// Note: It's strange, but stat::st_size from the stat call is zero for devices
//

int File = open(pPath, O_RDONLY);
if (File < 0)

printf("Failed to open file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


long off = lseek(File, 0, SEEK_END);
if (off == (off_t)-1)

printf("Failed to get file size. Error: %s. FilePath: %sn", strerror(errno), pPath);
close(File);
return false;

close(File);

printf("File size by lseek: %" PRIu64 "n", off);
fflush(stdout);

Size = off;
return true;



Output:



File size by stat: 0 WTF?
File size by lseek: 34359738368


If I use stat for a regular file then everything is OK (comment out the line with "/dev/sda"):



File size by stat: 4019 WTF?
File size by lseek: 4019









share|improve this question
















I noticed that when I query the size of a device using open + lseek, everything is OK, but when I stat the device I get zero instead of the real device size. The device is clean without any file system and the first bytes of device start with some text like "1234567890ABC". What is wrong?



The code:



#include <sys/stat.h>
#include <dirent.h>

bool
GetFileSize(const char* pPath, uint64_t& Size)

pPath = "/home/sw/.bashrc";
pPath = "/dev/sda";

struct stat buffer;
if (stat(pPath, &buffer))

printf("Failed to stat file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


printf("File size by stat: %" PRIu64 " WTF?n", buffer.st_size);

//
// Note: It's strange, but stat::st_size from the stat call is zero for devices
//

int File = open(pPath, O_RDONLY);
if (File < 0)

printf("Failed to open file. Error: %s. FilePath: %sn", strerror(errno), pPath);
return false;


long off = lseek(File, 0, SEEK_END);
if (off == (off_t)-1)

printf("Failed to get file size. Error: %s. FilePath: %sn", strerror(errno), pPath);
close(File);
return false;

close(File);

printf("File size by lseek: %" PRIu64 "n", off);
fflush(stdout);

Size = off;
return true;



Output:



File size by stat: 0 WTF?
File size by lseek: 34359738368


If I use stat for a regular file then everything is OK (comment out the line with "/dev/sda"):



File size by stat: 4019 WTF?
File size by lseek: 4019






c linux posix stat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 12:39









Boann

37.3k1290121




37.3k1290121










asked Mar 14 at 14:00









Alexander SymonenkoAlexander Symonenko

14412




14412







  • 2





    welcome to "stat information is different from seeking+telling information".

    – Jean-François Fabre
    Mar 14 at 14:02






  • 2





    related: unix.stackexchange.com/questions/384488/…

    – Jean-François Fabre
    Mar 14 at 14:05






  • 4





    I don’t trust printing the size using PRIu64 unless you cast the size to uint64_t. That said, you’d probably not get zero if it’s going wrong.

    – Jonathan Leffler
    Mar 14 at 14:08












  • 2





    welcome to "stat information is different from seeking+telling information".

    – Jean-François Fabre
    Mar 14 at 14:02






  • 2





    related: unix.stackexchange.com/questions/384488/…

    – Jean-François Fabre
    Mar 14 at 14:05






  • 4





    I don’t trust printing the size using PRIu64 unless you cast the size to uint64_t. That said, you’d probably not get zero if it’s going wrong.

    – Jonathan Leffler
    Mar 14 at 14:08







2




2





welcome to "stat information is different from seeking+telling information".

– Jean-François Fabre
Mar 14 at 14:02





welcome to "stat information is different from seeking+telling information".

– Jean-François Fabre
Mar 14 at 14:02




2




2





related: unix.stackexchange.com/questions/384488/…

– Jean-François Fabre
Mar 14 at 14:05





related: unix.stackexchange.com/questions/384488/…

– Jean-François Fabre
Mar 14 at 14:05




4




4





I don’t trust printing the size using PRIu64 unless you cast the size to uint64_t. That said, you’d probably not get zero if it’s going wrong.

– Jonathan Leffler
Mar 14 at 14:08





I don’t trust printing the size using PRIu64 unless you cast the size to uint64_t. That said, you’d probably not get zero if it’s going wrong.

– Jonathan Leffler
Mar 14 at 14:08












5 Answers
5






active

oldest

votes


















10














The devil is in the detail... For starters, there is the fundamental principle of Unix design: everything is a file, Nicely explained here.



The second is that the stat(2) call is giving you inode statistics stored on the filesystem about the device-special file which has a size of zero (think of it as lstat(2)). If you have a block-device that has a filesystem on it you get information about it using statfs(2) or getfsstat(2) or statvfs(2) in a filesystem/device independent way.



Dealing with special files (usually residing in /dev) has always been system specific and the manual pages reside in section 4. So if you want to manipulate a device directly you should read up on the specifics there. For instance, in Linux man 4 hd will show you how to programmatically interact with IDE block devices. Whereas man 4 sd will give you how to interact with scsi discs, etc.



Third thing, system calls are not supposed to be inconsistent in their functionality NOR their limitations.



Hope this has helped.






share|improve this answer


















  • 1





    it has, and I have the impression that the 3 existing answers complete each other.

    – Jean-François Fabre
    Mar 14 at 14:49






  • 1





    @Jean-FrançoisFabre they seem to don't they :)

    – Ahmed Masud
    Mar 14 at 15:02











  • @Jean-François Fabre Masud All answers are correct and complete each other. But this think seems is key: stat works with inodes and it gives information from existing FS, not device file. But in other side device file has size, has start and has the end (in case of block device) therefore lseek works without problem and returns correct device size. As Ahmed told "everything is a file", therefore I had used stat in place where it cannot be used. "The devil is in the detail.." - accurately describes this situation. Thank you guys!

    – Alexander Symonenko
    Mar 14 at 16:04


















8














from this Unix Stack Exchange question:




Device files are not files per se. They're an I/O interface to use the devices in Unix-like operating systems. They use no space on disk, however, they still use an inode as reported by the stat command:




$ stat /dev/sda
File: /dev/sda
Size: 0 Blocks: 0 IO Block: 4096 block special file
Device: 6h/6d Inode: 14628 Links: 1 Device type: 8,0


That solves the stat part.



the fact that you can seek in this "file" is not related. This isn't really a file, but you can open it and read from it. You can seek to it too. It allows to read the disk at the lowest level, so seeking is necessary (that's why it works, and why wouldn't it return the new position like any "real" file?).



According to this other UnixSE answer, you can get the device size by reading this /dev/sda/size file.






share|improve this answer
































    5














    The length of a "device" such as /dev/sda is not specifed by the POSIX struct stat:



    off_t st_size For regular files, the file size in bytes. 

    For symbolic links, the length in bytes of the
    pathname contained in the symbolic link.

    For a shared memory object, the length in bytes.

    For a typed memory object, the length in bytes.

    For other file types, the use of this field is
    unspecified.


    So POSIX has no requirement for the "size" of a disk device.



    Linux likewise does not specify that stat() shall return the size of a disk device:




    st_size



    This field gives the size of the file (if it is a regular
    file
    or a symbolic link) in bytes. The size of a symbolic link is
    the length of the pathname it contains, without a terminating
    null byte.







    share|improve this answer






























      0














      On Linux, the documented way to get the size of a raw disk device that you can open is with the BLKGETSIZE ioctl. See the sd(4) manpage.



      Note that this returns the size of the device in sectors. You might think that, for size in bytes, you have to multiply by the value returned by the BLKSSZGET ioctl, but if I'm reading the source code correctly, you actually have to multiply by 512 no matter what BLKSSZGET returns.






      share|improve this answer






























        -1














        lseek is the backbone to C's fseek, so it has similar semantics, matching fseek - and quite detached from other areas of the Unix API. Provenance-wise, you'd expect lseek to act like file-handle-taking fseek, and fseek is a C-library interface that came to be without being Unix-specific.



        stat is Unix-specific, though, and does its own thing. It's a reasonable difference to expect if you think about provenance. Of course the problem is, then, that C APIs have very weak type models because C is one step short of making true type safety possible.



        Why is this important? Because, fundamentally, a seekable_size and a file_object_size are two fundamentally different concepts, and would demand different types – even the C++ standard library gets it wrong.



        But while in C++ and with modern compilers it’s now an entirely gratuitous legacy shortcoming, there’s really no way in C to efficiently wrap integers into incompatible types without killing performance and code readability. And thus you end up with something like offs_t or long being used for wholly incompatible concepts. And this is the source of confusion: just because you get a size-related number out of a file-related function doesn’t mean that the number will have the same meaning. And meanings are usually captured in types… The only meaning a long inherently has is “hey, I’m a number, you can do numeric things with me”… :(






        share|improve this answer
























          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55164462%2fwhy-is-statst-size-0-for-devices-but-at-the-same-time-lseek-defines-the-device%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          10














          The devil is in the detail... For starters, there is the fundamental principle of Unix design: everything is a file, Nicely explained here.



          The second is that the stat(2) call is giving you inode statistics stored on the filesystem about the device-special file which has a size of zero (think of it as lstat(2)). If you have a block-device that has a filesystem on it you get information about it using statfs(2) or getfsstat(2) or statvfs(2) in a filesystem/device independent way.



          Dealing with special files (usually residing in /dev) has always been system specific and the manual pages reside in section 4. So if you want to manipulate a device directly you should read up on the specifics there. For instance, in Linux man 4 hd will show you how to programmatically interact with IDE block devices. Whereas man 4 sd will give you how to interact with scsi discs, etc.



          Third thing, system calls are not supposed to be inconsistent in their functionality NOR their limitations.



          Hope this has helped.






          share|improve this answer


















          • 1





            it has, and I have the impression that the 3 existing answers complete each other.

            – Jean-François Fabre
            Mar 14 at 14:49






          • 1





            @Jean-FrançoisFabre they seem to don't they :)

            – Ahmed Masud
            Mar 14 at 15:02











          • @Jean-François Fabre Masud All answers are correct and complete each other. But this think seems is key: stat works with inodes and it gives information from existing FS, not device file. But in other side device file has size, has start and has the end (in case of block device) therefore lseek works without problem and returns correct device size. As Ahmed told "everything is a file", therefore I had used stat in place where it cannot be used. "The devil is in the detail.." - accurately describes this situation. Thank you guys!

            – Alexander Symonenko
            Mar 14 at 16:04















          10














          The devil is in the detail... For starters, there is the fundamental principle of Unix design: everything is a file, Nicely explained here.



          The second is that the stat(2) call is giving you inode statistics stored on the filesystem about the device-special file which has a size of zero (think of it as lstat(2)). If you have a block-device that has a filesystem on it you get information about it using statfs(2) or getfsstat(2) or statvfs(2) in a filesystem/device independent way.



          Dealing with special files (usually residing in /dev) has always been system specific and the manual pages reside in section 4. So if you want to manipulate a device directly you should read up on the specifics there. For instance, in Linux man 4 hd will show you how to programmatically interact with IDE block devices. Whereas man 4 sd will give you how to interact with scsi discs, etc.



          Third thing, system calls are not supposed to be inconsistent in their functionality NOR their limitations.



          Hope this has helped.






          share|improve this answer


















          • 1





            it has, and I have the impression that the 3 existing answers complete each other.

            – Jean-François Fabre
            Mar 14 at 14:49






          • 1





            @Jean-FrançoisFabre they seem to don't they :)

            – Ahmed Masud
            Mar 14 at 15:02











          • @Jean-François Fabre Masud All answers are correct and complete each other. But this think seems is key: stat works with inodes and it gives information from existing FS, not device file. But in other side device file has size, has start and has the end (in case of block device) therefore lseek works without problem and returns correct device size. As Ahmed told "everything is a file", therefore I had used stat in place where it cannot be used. "The devil is in the detail.." - accurately describes this situation. Thank you guys!

            – Alexander Symonenko
            Mar 14 at 16:04













          10












          10








          10







          The devil is in the detail... For starters, there is the fundamental principle of Unix design: everything is a file, Nicely explained here.



          The second is that the stat(2) call is giving you inode statistics stored on the filesystem about the device-special file which has a size of zero (think of it as lstat(2)). If you have a block-device that has a filesystem on it you get information about it using statfs(2) or getfsstat(2) or statvfs(2) in a filesystem/device independent way.



          Dealing with special files (usually residing in /dev) has always been system specific and the manual pages reside in section 4. So if you want to manipulate a device directly you should read up on the specifics there. For instance, in Linux man 4 hd will show you how to programmatically interact with IDE block devices. Whereas man 4 sd will give you how to interact with scsi discs, etc.



          Third thing, system calls are not supposed to be inconsistent in their functionality NOR their limitations.



          Hope this has helped.






          share|improve this answer













          The devil is in the detail... For starters, there is the fundamental principle of Unix design: everything is a file, Nicely explained here.



          The second is that the stat(2) call is giving you inode statistics stored on the filesystem about the device-special file which has a size of zero (think of it as lstat(2)). If you have a block-device that has a filesystem on it you get information about it using statfs(2) or getfsstat(2) or statvfs(2) in a filesystem/device independent way.



          Dealing with special files (usually residing in /dev) has always been system specific and the manual pages reside in section 4. So if you want to manipulate a device directly you should read up on the specifics there. For instance, in Linux man 4 hd will show you how to programmatically interact with IDE block devices. Whereas man 4 sd will give you how to interact with scsi discs, etc.



          Third thing, system calls are not supposed to be inconsistent in their functionality NOR their limitations.



          Hope this has helped.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 14 at 14:23









          Ahmed MasudAhmed Masud

          15.8k32538




          15.8k32538







          • 1





            it has, and I have the impression that the 3 existing answers complete each other.

            – Jean-François Fabre
            Mar 14 at 14:49






          • 1





            @Jean-FrançoisFabre they seem to don't they :)

            – Ahmed Masud
            Mar 14 at 15:02











          • @Jean-François Fabre Masud All answers are correct and complete each other. But this think seems is key: stat works with inodes and it gives information from existing FS, not device file. But in other side device file has size, has start and has the end (in case of block device) therefore lseek works without problem and returns correct device size. As Ahmed told "everything is a file", therefore I had used stat in place where it cannot be used. "The devil is in the detail.." - accurately describes this situation. Thank you guys!

            – Alexander Symonenko
            Mar 14 at 16:04












          • 1





            it has, and I have the impression that the 3 existing answers complete each other.

            – Jean-François Fabre
            Mar 14 at 14:49






          • 1





            @Jean-FrançoisFabre they seem to don't they :)

            – Ahmed Masud
            Mar 14 at 15:02











          • @Jean-François Fabre Masud All answers are correct and complete each other. But this think seems is key: stat works with inodes and it gives information from existing FS, not device file. But in other side device file has size, has start and has the end (in case of block device) therefore lseek works without problem and returns correct device size. As Ahmed told "everything is a file", therefore I had used stat in place where it cannot be used. "The devil is in the detail.." - accurately describes this situation. Thank you guys!

            – Alexander Symonenko
            Mar 14 at 16:04







          1




          1





          it has, and I have the impression that the 3 existing answers complete each other.

          – Jean-François Fabre
          Mar 14 at 14:49





          it has, and I have the impression that the 3 existing answers complete each other.

          – Jean-François Fabre
          Mar 14 at 14:49




          1




          1





          @Jean-FrançoisFabre they seem to don't they :)

          – Ahmed Masud
          Mar 14 at 15:02





          @Jean-FrançoisFabre they seem to don't they :)

          – Ahmed Masud
          Mar 14 at 15:02













          @Jean-François Fabre Masud All answers are correct and complete each other. But this think seems is key: stat works with inodes and it gives information from existing FS, not device file. But in other side device file has size, has start and has the end (in case of block device) therefore lseek works without problem and returns correct device size. As Ahmed told "everything is a file", therefore I had used stat in place where it cannot be used. "The devil is in the detail.." - accurately describes this situation. Thank you guys!

          – Alexander Symonenko
          Mar 14 at 16:04





          @Jean-François Fabre Masud All answers are correct and complete each other. But this think seems is key: stat works with inodes and it gives information from existing FS, not device file. But in other side device file has size, has start and has the end (in case of block device) therefore lseek works without problem and returns correct device size. As Ahmed told "everything is a file", therefore I had used stat in place where it cannot be used. "The devil is in the detail.." - accurately describes this situation. Thank you guys!

          – Alexander Symonenko
          Mar 14 at 16:04













          8














          from this Unix Stack Exchange question:




          Device files are not files per se. They're an I/O interface to use the devices in Unix-like operating systems. They use no space on disk, however, they still use an inode as reported by the stat command:




          $ stat /dev/sda
          File: /dev/sda
          Size: 0 Blocks: 0 IO Block: 4096 block special file
          Device: 6h/6d Inode: 14628 Links: 1 Device type: 8,0


          That solves the stat part.



          the fact that you can seek in this "file" is not related. This isn't really a file, but you can open it and read from it. You can seek to it too. It allows to read the disk at the lowest level, so seeking is necessary (that's why it works, and why wouldn't it return the new position like any "real" file?).



          According to this other UnixSE answer, you can get the device size by reading this /dev/sda/size file.






          share|improve this answer





























            8














            from this Unix Stack Exchange question:




            Device files are not files per se. They're an I/O interface to use the devices in Unix-like operating systems. They use no space on disk, however, they still use an inode as reported by the stat command:




            $ stat /dev/sda
            File: /dev/sda
            Size: 0 Blocks: 0 IO Block: 4096 block special file
            Device: 6h/6d Inode: 14628 Links: 1 Device type: 8,0


            That solves the stat part.



            the fact that you can seek in this "file" is not related. This isn't really a file, but you can open it and read from it. You can seek to it too. It allows to read the disk at the lowest level, so seeking is necessary (that's why it works, and why wouldn't it return the new position like any "real" file?).



            According to this other UnixSE answer, you can get the device size by reading this /dev/sda/size file.






            share|improve this answer



























              8












              8








              8







              from this Unix Stack Exchange question:




              Device files are not files per se. They're an I/O interface to use the devices in Unix-like operating systems. They use no space on disk, however, they still use an inode as reported by the stat command:




              $ stat /dev/sda
              File: /dev/sda
              Size: 0 Blocks: 0 IO Block: 4096 block special file
              Device: 6h/6d Inode: 14628 Links: 1 Device type: 8,0


              That solves the stat part.



              the fact that you can seek in this "file" is not related. This isn't really a file, but you can open it and read from it. You can seek to it too. It allows to read the disk at the lowest level, so seeking is necessary (that's why it works, and why wouldn't it return the new position like any "real" file?).



              According to this other UnixSE answer, you can get the device size by reading this /dev/sda/size file.






              share|improve this answer















              from this Unix Stack Exchange question:




              Device files are not files per se. They're an I/O interface to use the devices in Unix-like operating systems. They use no space on disk, however, they still use an inode as reported by the stat command:




              $ stat /dev/sda
              File: /dev/sda
              Size: 0 Blocks: 0 IO Block: 4096 block special file
              Device: 6h/6d Inode: 14628 Links: 1 Device type: 8,0


              That solves the stat part.



              the fact that you can seek in this "file" is not related. This isn't really a file, but you can open it and read from it. You can seek to it too. It allows to read the disk at the lowest level, so seeking is necessary (that's why it works, and why wouldn't it return the new position like any "real" file?).



              According to this other UnixSE answer, you can get the device size by reading this /dev/sda/size file.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 14 at 15:05

























              answered Mar 14 at 14:13









              Jean-François FabreJean-François Fabre

              106k1057115




              106k1057115





















                  5














                  The length of a "device" such as /dev/sda is not specifed by the POSIX struct stat:



                  off_t st_size For regular files, the file size in bytes. 

                  For symbolic links, the length in bytes of the
                  pathname contained in the symbolic link.

                  For a shared memory object, the length in bytes.

                  For a typed memory object, the length in bytes.

                  For other file types, the use of this field is
                  unspecified.


                  So POSIX has no requirement for the "size" of a disk device.



                  Linux likewise does not specify that stat() shall return the size of a disk device:




                  st_size



                  This field gives the size of the file (if it is a regular
                  file
                  or a symbolic link) in bytes. The size of a symbolic link is
                  the length of the pathname it contains, without a terminating
                  null byte.







                  share|improve this answer



























                    5














                    The length of a "device" such as /dev/sda is not specifed by the POSIX struct stat:



                    off_t st_size For regular files, the file size in bytes. 

                    For symbolic links, the length in bytes of the
                    pathname contained in the symbolic link.

                    For a shared memory object, the length in bytes.

                    For a typed memory object, the length in bytes.

                    For other file types, the use of this field is
                    unspecified.


                    So POSIX has no requirement for the "size" of a disk device.



                    Linux likewise does not specify that stat() shall return the size of a disk device:




                    st_size



                    This field gives the size of the file (if it is a regular
                    file
                    or a symbolic link) in bytes. The size of a symbolic link is
                    the length of the pathname it contains, without a terminating
                    null byte.







                    share|improve this answer

























                      5












                      5








                      5







                      The length of a "device" such as /dev/sda is not specifed by the POSIX struct stat:



                      off_t st_size For regular files, the file size in bytes. 

                      For symbolic links, the length in bytes of the
                      pathname contained in the symbolic link.

                      For a shared memory object, the length in bytes.

                      For a typed memory object, the length in bytes.

                      For other file types, the use of this field is
                      unspecified.


                      So POSIX has no requirement for the "size" of a disk device.



                      Linux likewise does not specify that stat() shall return the size of a disk device:




                      st_size



                      This field gives the size of the file (if it is a regular
                      file
                      or a symbolic link) in bytes. The size of a symbolic link is
                      the length of the pathname it contains, without a terminating
                      null byte.







                      share|improve this answer













                      The length of a "device" such as /dev/sda is not specifed by the POSIX struct stat:



                      off_t st_size For regular files, the file size in bytes. 

                      For symbolic links, the length in bytes of the
                      pathname contained in the symbolic link.

                      For a shared memory object, the length in bytes.

                      For a typed memory object, the length in bytes.

                      For other file types, the use of this field is
                      unspecified.


                      So POSIX has no requirement for the "size" of a disk device.



                      Linux likewise does not specify that stat() shall return the size of a disk device:




                      st_size



                      This field gives the size of the file (if it is a regular
                      file
                      or a symbolic link) in bytes. The size of a symbolic link is
                      the length of the pathname it contains, without a terminating
                      null byte.








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 14 at 14:16









                      Andrew HenleAndrew Henle

                      20.5k31435




                      20.5k31435





















                          0














                          On Linux, the documented way to get the size of a raw disk device that you can open is with the BLKGETSIZE ioctl. See the sd(4) manpage.



                          Note that this returns the size of the device in sectors. You might think that, for size in bytes, you have to multiply by the value returned by the BLKSSZGET ioctl, but if I'm reading the source code correctly, you actually have to multiply by 512 no matter what BLKSSZGET returns.






                          share|improve this answer



























                            0














                            On Linux, the documented way to get the size of a raw disk device that you can open is with the BLKGETSIZE ioctl. See the sd(4) manpage.



                            Note that this returns the size of the device in sectors. You might think that, for size in bytes, you have to multiply by the value returned by the BLKSSZGET ioctl, but if I'm reading the source code correctly, you actually have to multiply by 512 no matter what BLKSSZGET returns.






                            share|improve this answer

























                              0












                              0








                              0







                              On Linux, the documented way to get the size of a raw disk device that you can open is with the BLKGETSIZE ioctl. See the sd(4) manpage.



                              Note that this returns the size of the device in sectors. You might think that, for size in bytes, you have to multiply by the value returned by the BLKSSZGET ioctl, but if I'm reading the source code correctly, you actually have to multiply by 512 no matter what BLKSSZGET returns.






                              share|improve this answer













                              On Linux, the documented way to get the size of a raw disk device that you can open is with the BLKGETSIZE ioctl. See the sd(4) manpage.



                              Note that this returns the size of the device in sectors. You might think that, for size in bytes, you have to multiply by the value returned by the BLKSSZGET ioctl, but if I'm reading the source code correctly, you actually have to multiply by 512 no matter what BLKSSZGET returns.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 14 at 17:01









                              zwolzwol

                              99.2k24172273




                              99.2k24172273





















                                  -1














                                  lseek is the backbone to C's fseek, so it has similar semantics, matching fseek - and quite detached from other areas of the Unix API. Provenance-wise, you'd expect lseek to act like file-handle-taking fseek, and fseek is a C-library interface that came to be without being Unix-specific.



                                  stat is Unix-specific, though, and does its own thing. It's a reasonable difference to expect if you think about provenance. Of course the problem is, then, that C APIs have very weak type models because C is one step short of making true type safety possible.



                                  Why is this important? Because, fundamentally, a seekable_size and a file_object_size are two fundamentally different concepts, and would demand different types – even the C++ standard library gets it wrong.



                                  But while in C++ and with modern compilers it’s now an entirely gratuitous legacy shortcoming, there’s really no way in C to efficiently wrap integers into incompatible types without killing performance and code readability. And thus you end up with something like offs_t or long being used for wholly incompatible concepts. And this is the source of confusion: just because you get a size-related number out of a file-related function doesn’t mean that the number will have the same meaning. And meanings are usually captured in types… The only meaning a long inherently has is “hey, I’m a number, you can do numeric things with me”… :(






                                  share|improve this answer





























                                    -1














                                    lseek is the backbone to C's fseek, so it has similar semantics, matching fseek - and quite detached from other areas of the Unix API. Provenance-wise, you'd expect lseek to act like file-handle-taking fseek, and fseek is a C-library interface that came to be without being Unix-specific.



                                    stat is Unix-specific, though, and does its own thing. It's a reasonable difference to expect if you think about provenance. Of course the problem is, then, that C APIs have very weak type models because C is one step short of making true type safety possible.



                                    Why is this important? Because, fundamentally, a seekable_size and a file_object_size are two fundamentally different concepts, and would demand different types – even the C++ standard library gets it wrong.



                                    But while in C++ and with modern compilers it’s now an entirely gratuitous legacy shortcoming, there’s really no way in C to efficiently wrap integers into incompatible types without killing performance and code readability. And thus you end up with something like offs_t or long being used for wholly incompatible concepts. And this is the source of confusion: just because you get a size-related number out of a file-related function doesn’t mean that the number will have the same meaning. And meanings are usually captured in types… The only meaning a long inherently has is “hey, I’m a number, you can do numeric things with me”… :(






                                    share|improve this answer



























                                      -1












                                      -1








                                      -1







                                      lseek is the backbone to C's fseek, so it has similar semantics, matching fseek - and quite detached from other areas of the Unix API. Provenance-wise, you'd expect lseek to act like file-handle-taking fseek, and fseek is a C-library interface that came to be without being Unix-specific.



                                      stat is Unix-specific, though, and does its own thing. It's a reasonable difference to expect if you think about provenance. Of course the problem is, then, that C APIs have very weak type models because C is one step short of making true type safety possible.



                                      Why is this important? Because, fundamentally, a seekable_size and a file_object_size are two fundamentally different concepts, and would demand different types – even the C++ standard library gets it wrong.



                                      But while in C++ and with modern compilers it’s now an entirely gratuitous legacy shortcoming, there’s really no way in C to efficiently wrap integers into incompatible types without killing performance and code readability. And thus you end up with something like offs_t or long being used for wholly incompatible concepts. And this is the source of confusion: just because you get a size-related number out of a file-related function doesn’t mean that the number will have the same meaning. And meanings are usually captured in types… The only meaning a long inherently has is “hey, I’m a number, you can do numeric things with me”… :(






                                      share|improve this answer















                                      lseek is the backbone to C's fseek, so it has similar semantics, matching fseek - and quite detached from other areas of the Unix API. Provenance-wise, you'd expect lseek to act like file-handle-taking fseek, and fseek is a C-library interface that came to be without being Unix-specific.



                                      stat is Unix-specific, though, and does its own thing. It's a reasonable difference to expect if you think about provenance. Of course the problem is, then, that C APIs have very weak type models because C is one step short of making true type safety possible.



                                      Why is this important? Because, fundamentally, a seekable_size and a file_object_size are two fundamentally different concepts, and would demand different types – even the C++ standard library gets it wrong.



                                      But while in C++ and with modern compilers it’s now an entirely gratuitous legacy shortcoming, there’s really no way in C to efficiently wrap integers into incompatible types without killing performance and code readability. And thus you end up with something like offs_t or long being used for wholly incompatible concepts. And this is the source of confusion: just because you get a size-related number out of a file-related function doesn’t mean that the number will have the same meaning. And meanings are usually captured in types… The only meaning a long inherently has is “hey, I’m a number, you can do numeric things with me”… :(







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Mar 15 at 13:31

























                                      answered Mar 14 at 19:24









                                      Kuba OberKuba Ober

                                      70.7k1083193




                                      70.7k1083193



























                                          draft saved

                                          draft discarded
















































                                          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.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55164462%2fwhy-is-statst-size-0-for-devices-but-at-the-same-time-lseek-defines-the-device%23new-answer', 'question_page');

                                          );

                                          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







                                          Popular posts from this blog

                                          How should I support this large drywall patch? Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How do I cover large gaps in drywall?How do I keep drywall around a patch from crumbling?Can I glue a second layer of drywall?How to patch long strip on drywall?Large drywall patch: how to avoid bulging seams?Drywall Mesh Patch vs. Bulge? To remove or not to remove?How to fix this drywall job?Prep drywall before backsplashWhat's the best way to fix this horrible drywall patch job?Drywall patching using 3M Patch Plus Primer

                                          random experiment with two different functions on unit interval Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Random variable and probability space notionsRandom Walk with EdgesFinding functions where the increase over a random interval is Poisson distributedNumber of days until dayCan an observed event in fact be of zero probability?Unit random processmodels of coins and uniform distributionHow to get the number of successes given $n$ trials , probability $P$ and a random variable $X$Absorbing Markov chain in a computer. Is “almost every” turned into always convergence in computer executions?Stopped random walk is not uniformly integrable

                                          Lowndes Grove History Architecture References Navigation menu32°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661132°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661178002500"National Register Information System"Historic houses of South Carolina"Lowndes Grove""+32° 48' 6.00", −79° 57' 58.00""Lowndes Grove, Charleston County (260 St. Margaret St., Charleston)""Lowndes Grove"The Charleston ExpositionIt Happened in South Carolina"Lowndes Grove (House), Saint Margaret Street & Sixth Avenue, Charleston, Charleston County, SC(Photographs)"Plantations of the Carolina Low Countrye