Unix Programming - Can not get large file size with LFS

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2007 > Can not get large file size with LFS





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Can not get large file size with LFS
vian1381@china.com.cn

2007-05-16, 1:19 pm

I can get the file (larger than 2GB) size with the following codes
bycompiling with "gcc XX.c -D_LARGE_FILE_SOURCE -
D_FILE_OFFSET_BITS=64".

struct stat buf;
if (stat("file_name",&buf) == 0)
printf("file size is %lld", buf.st_size);

But when I move these codes above to my application (a lots of source
codes), it doesn't work.

I user command "getconf LFS_CFLAGS" to retrieve the flags required for
LFS, and added -D_LARGE_FILE_SOURCE and -D_FILE_OFFSET_BITS=64 to the
makefile.
But I can't get the file size.

Do you have any idea why I can't get the file size?

Thanks in advance!

Fred Kleinschmidt

2007-05-16, 7:18 pm


<vian1381@china.com.cn> wrote in message
news:1179333133.314133.228650@q23g2000hsg.googlegroups.com...
>I can get the file (larger than 2GB) size with the following codes
> bycompiling with "gcc XX.c -D_LARGE_FILE_SOURCE -
> D_FILE_OFFSET_BITS=64".
>
> struct stat buf;
> if (stat("file_name",&buf) == 0)
> printf("file size is %lld", buf.st_size);
>
> But when I move these codes above to my application (a lots of source
> codes), it doesn't work.


What do you mean by "it doesn't work"? It doesn't compile?
It compiles but the program crashes? It can't find the file?
It returns the wrong size?
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing



vian1381@china.com.cn

2007-05-17, 1:19 am

> What do you mean by "it doesn't work"? It doesn't compile?
> It compiles but the program crashes? It can't find the file?
> It returns the wrong size?
> --
> Fred L. Kleinschmidt
> Boeing Associate Technical Fellow
> Aero Stability and Controls Computing


Oh, I should give more detail. It returns a wrong size (negative).
BTW, fopen has no problem to open a more than 2GB file.

Thanks!

Anonymous

2007-05-17, 1:19 am

vian1381@china.com.cn wrote:
> I can get the file (larger than 2GB) size with the following codes
> bycompiling with "gcc XX.c -D_LARGE_FILE_SOURCE -
> D_FILE_OFFSET_BITS=64".
>
> struct stat buf;
> if (stat("file_name",&buf) == 0)
> printf("file size is %lld", buf.st_size);
>
> But when I move these codes above to my application (a lots of source
> codes), it doesn't work.
>
> I user command "getconf LFS_CFLAGS" to retrieve the flags required for
> LFS, and added -D_LARGE_FILE_SOURCE and -D_FILE_OFFSET_BITS=64 to the
> makefile.
> But I can't get the file size.
>
> Do you have any idea why I can't get the file size?
>
> Thanks in advance!
>

Can you set a break after the stat() call, then examine the value set in
buf? I am not familiar with %lld in the printf statement. If
buf.st_size gives you the correct size, then perhaps it's the printf
statement that you need to fix.
David Schwartz

2007-05-17, 7:16 am

On May 16, 6:50 pm, vian1...@china.com.cn wrote:

> Oh, I should give more detail. It returns a wrong size (negative).
> BTW, fopen has no problem to open a more than 2GB file.


*What* returns a wrong size? Post the code. (Perhaps you return the
size in an 'int' or something like that.)

DS

vian1381@china.com.cn

2007-05-17, 1:18 pm

> Can you set a break after the stat() call, then examine the value set in
> buf? I am not familiar with %lld in the printf statement. If
> buf.st_size gives you the correct size, then perhaps it's the printf
> statement that you need to fix.- >


I set a break after stat(), it's correct when the file size is smaller
than 2GB. When the file size is larger than 2GB, the size is a
negative.
Why?

Mark

2007-05-17, 1:18 pm

vian1381@china.com.cn wrote:
> I can get the file (larger than 2GB) size with the following codes
> bycompiling with "gcc XX.c -D_LARGE_FILE_SOURCE -
> D_FILE_OFFSET_BITS=64".
>
> struct stat buf;
> if (stat("file_name",&buf) == 0)
> printf("file size is %lld", buf.st_size);
>
> But when I move these codes above to my application (a lots of source
> codes), it doesn't work.
>
> I user command "getconf LFS_CFLAGS" to retrieve the flags required for
> LFS, and added -D_LARGE_FILE_SOURCE and -D_FILE_OFFSET_BITS=64 to the
> makefile.
> But I can't get the file size.
>
> Do you have any idea why I can't get the file size?
>
> Thanks in advance!
>


Maybe try:

struct stat64 buf;
if (stat64("file_name", &buf) == 0)
printf("file size is %lld", buf.st_size);

in this case, buf.st_size is of type off64_t instead
of the off_t you got from stat()

see
man lf64
and
man lfcompile64
David Schwartz

2007-05-17, 1:18 pm

On May 17, 8:29 am, vian1...@china.com.cn wrote:

> I set a break after stat(), it's correct when the file size is smaller
> than 2GB. When the file size is larger than 2GB, the size is a
> negative.
> Why?


Can you paste the actual line of code, location of the breakpoint, and
debugger output showing the negative size?

DS

vian1381@china.com.cn

2007-05-18, 7:20 am

> Can you paste the actual line of code, location of the breakpoint, and
> debugger output showing the negative size?
>

The debugger output in my application source code is shown below:
(gdb) print buf
$1 = {st_dev = 30932999, st_pad1 = {0, 0, 0}, st_ino = {_d =
9.4608136456494459e-319, _l = {0, 191489}},
st_mode = 33188, st_nlink = 1, st_uid = 1406, st_gid = 227, st_rdev
= 0, st_pad2 = {0, 0}, st_size = {
_d = 1.8360945890051364e-314, _l = {0, -578670475}}, st_atim =
{tv_sec = 1179138644, tv_nsec = 671916000},
st_mtim = {tv_sec = 1179415806, tv_nsec = 504192000}, st_ctim =
{tv_sec = 1179415806, tv_nsec = 504192000},
st_blksize = 8192, st_blocks = {_d = 3.5878889099984655e-317, _l =
{0, 7261968}},
st_fstype = "ufs", '\0' <repeats 12 times>, st_pad4 = {0, 0, 0, 0,
0, 0, 0, 0}}
(gdb)


When I debug the below code (copy to a new file, and compile with gcc -
g XX.c -D_LARGE_FILE_SOURCE -
D_FILE_OFFSET_BITS=64"):

struct stat s;
if (stat("file_name",&s) == 0)
printf("file size is %lld", s.st_size);

The debugger output is:
(gdb) print s
$1 = {st_dev = 30932999, st_pad1 = {0, 0, 0}, st_ino = 191489, st_mode
= 33188, st_nlink = 1, st_uid = 1406, st_gid = 227,
st_rdev = 0, st_pad2 = {0, 0}, st_size = 3716296821, st_atim =
{tv_sec = 1179138644, tv_nsec = 671916000}, st_mtim = {
tv_sec = 1179415806, tv_nsec = 504192000}, st_ctim = {tv_sec =
1179415806, tv_nsec = 504192000}, st_blksize = 8192,
st_blocks = 7261968, st_fstype = "ufs", '\0' <repeats 12 times>,
st_pad4 = {0, 0, 0, 0, 0, 0, 0, 0}}

The file size is correct.

Andrei Voropaev

2007-05-18, 7:20 am

On 2007-05-18, vian1381@china.com.cn <vian1381@china.com.cn> wrote:
> The debugger output in my application source code is shown below:
> (gdb) print buf

[...]
>= 0, st_pad2 = {0, 0}, st_size = {
> _d = 1.8360945890051364e-314, _l = {0, -578670475}}, st_atim =

[...]
>
> When I debug the below code (copy to a new file, and compile with gcc -
> g XX.c -D_LARGE_FILE_SOURCE -
> D_FILE_OFFSET_BITS=64"):

[...]
> The debugger output is:
> st_rdev = 0, st_pad2 = {0, 0}, st_size = 3716296821, st_atim =

[...]

That's interesting. The off_t in your "main" program is presented as
some sort of a structure instead of plain 64-bit integer as it is in
your "test" program. Looks like the flags that you use for compilation
of the main program (or some defines inside of code) are messed up.

--
Minds, like parachutes, function best when open
Andrei Voropaev

2007-05-18, 7:20 am

On 2007-05-17, Mark <none_not@nadaspam.com> wrote:
> vian1381@china.com.cn wrote:
[...][vbcol=seagreen]
>
> Maybe try:
>
> struct stat64 buf;
> if (stat64("file_name", &buf) == 0)
> printf("file size is %lld", buf.st_size);

[...]

No, this is not needed. The -D_FILE_OFFSET_BITS=64 do everything
necessary. All other things are most likely for "mixed" file access,
when you can decide which offset size to use. So at certain points in
the program one can use 32-bit off_t and at other points 64-bit.

--
Minds, like parachutes, function best when open
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com