Search code examples
cmacosscalafilesystemslsof

lsof always report offset equal to file size in OSX


I've tried to estimate, how much of the file my program has processed, and for me an obvious solution was to use lsof -o. But surprisingly, OFFSET in lsof's output was always equal to SIZE (like in lsof -s --), so I decided to write some simple programs to test that behaviour, and...

C:

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main(void) {
  int filedesc = open("path/to/file", O_RDONLY);
  printf("%i\n", filedesc);
  while(1) {};
}

Scala:

io.Source.fromFile(path)

Python:

open(path)

OFFSET was always at the end of file under OS X:

$ lsof -o /path/to/file
COMMAND  PID  USER   FD   TYPE DEVICE    OFFSET     NODE NAME
a.out   5390 folex    3r   REG    1,4 631302648 48453926 /path/to/file

$ lsof -s -- /path/to/file
COMMAND  PID  USER   FD   TYPE DEVICE      SIZE     NODE NAME
a.out   5390 folex    3r   REG    1,4 631302648 48453926 /path/to/file

Any explanations for each of these languages would be much appreciated.

UPDATE: Works as expected under Ubuntu. Wrong offset only under OS X.


Solution

  • Here's what the OSX man page says about the size/offset column for lsof (emphasis added):

    SIZE, SIZE/OFF, or OFFSET is the size of the file or the file offset in bytes. A value is displayed in this column only if it is available. Lsof displays whatever value - size or offset - is appropriate for the type of the file and the version of lsof. On some UNIX dialects lsof can't obtain accurate or consistent file offset information from its kernel data sources, [...]

    The file size is displayed in decimal; the offset is normally displayed in decimal with a leading '0t' if it contains 8 digits or less; in hexadecimal with a leading '0x' if it is longer than 8 digits. [...]

    Thus the leading '0t' and '0x' identify an offset when the column may contain both a size and an offset (i.e., its title is SIZE/OFF).

    Although the column heading says OFFSET, the number doesn't have the leading '0t' or '0x', so I would conclude that the file offset information is simply not available from the OSX kernel.