How does the total of the linux command ls-l be calculated?

Source: Internet
Author: User

own handwriting Implementation ls-l command tangled for a long time, just finally figured out. Directly on the code important part

Long Ntotalblocks = 0;dir* DP = opendir (dirpathname); struct dirent* dirp;while ((DIRP = Readdir (DP)) = NULL) {Lstat (FULLP Ath, &sbuf); Long nblocks = sbuf.st_size/4096;if (sbuf.st_size% 4096 > 0) nblocks++;if (S_islnk (sbuf.st_mode)) NBlo cks = 0;ntotalblocks + = nblocks;} Closedir (DP), cout << "Total" << ntotalblocks * 4 << Endl;

4096 is the default size of the block, and since Total is in K (1024), it is multiplied by 4.

Total is actually all the files accounted for the sum of the size of the block, because the file size is not necessarily a multiple of the standard 4096, such as a file size of 10000, you need to occupy 10000/4096+1=3 block, the size of the occupied block is 3*4096, K is the unit is 3*4096 /1024=12.

A lot of people on the net say all right, but missing a key part, that is ls-l is not calculated symbolic link, because the symbolic link st_size represents the length of the address of the symbolic links, a closer look, APUEv3 Chinese version of the P97 4.17 symbolic link of the example can be seen.

How does the total of the linux command ls-l be calculated?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.