9

Someone ask me in other site about this question, i.e. a file named "abc.dat" has 0 file size but 8 blocks, and this is the output I ask him to give me (Some text has been translated from Chinese to English):

$ cp abc.dat abc2.dat; ls -ls abc2.dat #try to copy, it still 8 blocks but 0 byte
8 -rw-rw-r--  1 rokeabbey rokeabbey      0 Feb  27 19:39 abc2.dat 

8 -rw-rw-r-- 1 rokeabbey rokeabbey 0 Sep 18 19:11 abc.dat #sorry, this may be the extra wrong output he added

$ stat abc.dat
  File: 'abc.dat'
  Size: 0           Blocks: 16          IO Block: 4096   regular empty file
Device: 32h/50d      Inode: 3715853    Links: 1
Access: (0664/-rw-rw-r--)  Uid:( 1000/rokeabbey)   Gid:( 1000/rokeabbey)
Access: 2018-02-26 21:13:57.640639992 +0800
Modify: 2017-09-18 19:11:42.221533011 +0800
Change: 2017-09-18 19:11:42.221533011 +0800
 Birth: -

$ touch abc3.dat ; ls -sl | grep abc #try to create new empty file, it still 8 blocks by default
8 -rw-rw-r--  1 rokeabbey rokeabbey      0 Feb  27 19:39 abc2.dat
8 -rw-rw-r--  1 rokeabbey rokeabbey      0 Feb  27 19:40 abc3.dat
8 -rw-rw-r--  1 rokeabbey rokeabbey      0 Sep  18 19:11 abc.dat

I've learned a bit about sparse file, file metadata, symlink cases, but none of that cases will causes 0 byte file size with 8 blocks. Is there any filesystems setup such as minimum block size for ANY file ?

He told me that his systems is Ubuntu 16.04 and ext4.

[UPDATE]

$ df -Th /home/rokeabbey
/home/rokeabbey/.Private ecryptfs  138G   39G   92G   30% /home/rokeabbey

[UPDATE] I can reproduced with ecryptfs

xb@dnxb:/tmp/test$ sudo mkdir /opt/data
xb@dnxb:/tmp/test$ sudo apt-get install ecryptfs-utils
...
xb@dnxb:/tmp/test$ sudo mount -t ecryptfs /opt/data /opt/data
Passphrase: 
...
Selection [aes]: 1
...
Selection [16]: 1
Enable plaintext passthrough (y/n) [n]: y
Enable filename encryption (y/n) [n]: y
...
Would you like to proceed with the mount (yes/no)? : yes
...
in order to avoid this warning in the future (yes/no)? : no 
Not adding sig to user sig cache file; continuing with mount.
Mounted eCryptfs
xb@dnxb:/tmp/test$ l /opt/data
total 8.0K
52953089 drwxr-xr-x 9 root root ? 4.0K Feb  27 23:16 ../
56369402 drwxr-xr-x 2 root root ? 4.0K Feb  27 23:16 ./
xb@dnxb:/tmp/test$ sudo touch /opt/data/testing
xb@dnxb:/tmp/test$ less /opt/data/testing      
xb@dnxb:/tmp/test$ sudo umount /opt/data
xb@dnxb:/tmp/test$ ls -ls /opt/data
total 8
8 -rw-r--r-- 1 root root 8192 Feb  27 23:42 ECRYPTFS_FNEK_ENCRYPTED.FWbECDhE0C37e-Skw2B2pnQpP9gB.b3yDfkVU5wk7WhvMreg8yVnuEaMME--
xb@dnxb:/tmp/test$ less /opt/data/ECRYPTFS_FNEK_ENCRYPTED.FWbECDhE0C37e-Skw2B2pnQpP9gB.b3yDfkVU5wk7WhvMreg8yVnuEaMME-- 
"/opt/data/ECRYPTFS_FNEK_ENCRYPTED.FWbECDhE0C37e-Skw2B2pnQpP9gB.b3yDfkVU5wk7WhvMreg8yVnuEaMME--" may be a binary file.  See it anyway? 
xb@dnxb:/tmp/test$ sudo mount -t ecryptfs /opt/data /opt/data
Passphrase: 
Select cipher: 
...
Selection [aes]: 1   
...
Selection [16]: 1
Enable plaintext passthrough (y/n) [n]: y
Enable filename encryption (y/n) [n]: y
...
Would you like to proceed with the mount (yes/no)? : yes
...
in order to avoid this warning in the future (yes/no)? : no 
Not adding sig to user sig cache file; continuing with mount.
Mounted eCryptfs
xb@dnxb:/tmp/test$ ls -ls /opt/data
total 8
8 -rw-r--r-- 1 root root 0 Feb  27 23:42 testing
xb@dnxb:/tmp/test$
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
林果皞
  • 4,946
  • 2
  • 29
  • 45
  • I'm confused. Is `abc.dat` and `abc2.dat` directories? The `ls -ls abc2.dat` output indicates that it's a directory with two entries, and the `stat abc.dat` indicates that that too is a directory. Or have you edited the output? – Kusalananda Feb 27 '18 at 14:31
  • @Kusalananda Sorry, I translated from Chinese and miss the directory translation. – 林果皞 Feb 27 '18 at 14:34
  • @Kusalananda `ls -ls abc2.dat`, he did shows me this 2 lines, most likey he pasted the wrong output. And the 3rd output did shows the correct output. – 林果皞 Feb 27 '18 at 14:38
  • The only thing I can think of is the extended attributes block. Is he using SELinux? – psusi Feb 27 '18 at 14:42

2 Answers2

16

This happens if the file system is encrypted; the FS needs to store extra metadata for the file, even if it is empty.

As I happen to have a machine handy with a vanilla ecryptfs mount (Ubuntu 12.04-LTS), I can confirm that an empty file will get 8 blocks:

$ touch test
$ ls -ls test

8 -rw-rw-r-- 1 admin admin 0 feb 27 16:45 test
LSerni
  • 4,305
  • 14
  • 20
6

You can get a zero-size file with blocks if you have extended attributes on the file, more than what can fit inside the inode itself:

$ touch abc
$ setfattr -n user.test -v xyz abc        # this doesn't do it
$ ls -s abc                               # since the data fits in the inode
0 abc
$ setfattr -n user.test -v "$(printf %100s " ")"  abc
$ ls -s abc
4 abc

But, I can't see how you'd get 8 kB that way, as according to the xattr man page, the size is limited to the block size on ext2/3/4, and the block size is limited by the system page size, so 4 kB on x86. Also, a newly created file shouldn't have any extended attributes, unless you're running SELinux, but in that case, ls -l should show the dot at the end of the permission bits to indicate the presence of an SELinux tag.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397