3

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.

folex
  • 5,122
  • 1
  • 28
  • 48
  • None of your snippets move the offset from position 0. Are you sure you're interpreting the output of `lsof` correctly? Copy-paste it into your question. Also look at the data reported by the kernel (and copy that into your question as well), it's what `lsof` uses: `/proc///fdinfo` – Gilles 'SO- stop being evil' Aug 18 '15 at 21:00
  • just found out that under linux it works fine :( So it's OS X and lsof interop fault. – folex Aug 18 '15 at 21:10
  • The C example: `open()` returns a file descriptor for the opened file. It tells you nothing about the file size. I recommend reading manual pages. – Weather Vane Aug 18 '15 at 21:59
  • `lsof -s --` gives me file size and `lsof -o` gives descriptor offset. Snippets have nothing to do with file size. – folex Aug 18 '15 at 22:03
  • Can you show a small sample of the output that you're seeing? – user3386109 Aug 18 '15 at 22:50
  • @user3386109 added `lsof -o` and `lsof -s --` output to question. – folex Aug 19 '15 at 08:36
  • It's the same under WSL which makes me suspect that sort of data is not available due to kernel limitations – elig Nov 05 '21 at 23:51

1 Answers1

4

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.

user3386109
  • 34,287
  • 7
  • 49
  • 68