Lseek the difference between CentOS 4.4 and CentOS 6.3.

Source: Internet
Author: User

You can get the size of a file or folder in the following way, where FD is the file (folder) descriptor

Long fileSize = Lseek (FD, 0, Seek_end)

But the following problems exist


CT6.3

If the FD passed to Lseek is a regular file, there is no problem;
If the FD passed to Lseek is a directory, not a file, then a fixed value 9223372036854775807 (0x 7FFF FFFF FFFF FFFF) is returned, because the directory under Linux is also a file;
If the FD passed to Lseek is a non-empty directory, and the following files are not empty, then the values are still returned, rather than the size of all the files in the directory.


CT4.4

If the FD passed to Lseek is a regular file, there is no problem;
if the FD passed to Lseek is a directory (whether or not it is empty) instead of a file, then a fixed value 4096 is returned, because the directory under Linux is also a file


Either the CT4.4 4096 return value or the CT6.3 9223372036854775807 return value is incorrect. In fact, doing Lseek (FD, 0, Seek_end) on a directory is wrong .

If you do a lseek (FD, 0, Seek_end) operation on a directory, and the return value is considered a "file" size, for CT4.4, it's 4 K, but for CT6.3, it's tragic: 9223372036854775807, such a large number, if you load the file as a file size, and allocate so much memory space in advance, memory will burst, because 9223372036854775807 is 0x 7FFF FFFF FFFF FFFF = 2 ^ 63- 1, the equivalent of 2 ^ G.


The " Lseek (FD, 0, Seek_end) operations return Astronomical" scenario under the CT6.3 is a bug in the Lseek () function.

Related Article

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.