homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam

The Geek Culture Forums


Post New Topic  New Poll  Post A Reply
my profile | directory login | | search | faq | forum home
  next oldest topic   next newest topic
» The Geek Culture Forums   » Other Geeky Stuff   » Ask a Geek!   » fstat() not working...

 - UBBFriend: Email this page to someone!    
Author Topic: fstat() not working...
nerdwithnofriends
Uber Geek
Member # 3773

Icon 1 posted October 05, 2005 21:01      Profile for nerdwithnofriends     Send New Private Message       Edit/Delete Post   Reply With Quote 
Hey all, I'm working on OS X 10.4 and, long story short, my calls to fstat() on an open file descriptor aren't working. Well, technically they're working, but I'm getting some unexpected results: the st_size field of the stat structure whose address I pass as the second argument of fstat() always remains at zero, and the same thing with st_blocks. The other fields are populated; the uid and gid field get the correct values, and so does the st_blksize field.

Basically, my code looks like this:

code:
 
struct stat filestats;
int fd = open("somefile", O_RDONLY);
fstat(fd, &filestats);

where 'somefile' is a non-special, plain ol' file that I own.

Any suggestions?

--------------------
"The Buddha, the Godhead, resides quite as comfortably in the circuits of a digital computer or the gears of a cycle transmission as he does at the top of a mountain or in the petals of a flower." - Robert M. Pirsig

Posts: 948 | Registered: Mar 2005  |  IP: Logged
Stereo

Solid Nitrozanium SuperFan!
Member # 748

Member Rated:
5
Icon 1 posted October 06, 2005 10:05      Profile for Stereo     Send New Private Message       Edit/Delete Post   Reply With Quote 
Is your file empty? Are you really accessing it? (I haven't done any serious programming lately [cry baby] but my memory tells me that opening a non-existent file actually creates one.) So my first guess is your access path you give for the file is somehow wrong, thus you are creating/opening an empty file. (Check for typos!)

Other than that, good luck!

--------------------
Eppur, si muove!

Galileo Galilei

Posts: 2289 | From: Gatineau, Quebec, Canada | Registered: Apr 2001  |  IP: Logged
drunkennewfiemidget
BlabberMouth, a Blabber Odyssey
Member # 2814

Member Rated:
4
Icon 1 posted October 06, 2005 11:01      Profile for drunkennewfiemidget     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Stereo:
Is your file empty? Are you really accessing it? (I haven't done any serious programming lately [cry baby] but my memory tells me that opening a non-existent file actually creates one.) So my first guess is your access path you give for the file is somehow wrong, thus you are creating/opening an empty file. (Check for typos!)

Other than that, good luck!

If you do a O_RDONLY it won't create the file if it doesn't exist I don't think, but you might be on to something, since he's not checking to see if the fopen succeeded or not.

if ((fd = fopen("file", O_RDONLY)) != -1)
{
// do stuff here
}
else
{
printf("Failed opening the file!\n");
exit;
}

.. or something.

Posts: 4897 | From: Cambridge, ON, Canada | Registered: Jun 2004  |  IP: Logged
nerdwithnofriends
Uber Geek
Member # 3773

Icon 1 posted October 06, 2005 13:32      Profile for nerdwithnofriends     Send New Private Message       Edit/Delete Post   Reply With Quote 
No, the file exists, the return value of open is a valid file descriptor, and fstat() returns 0. The actual code is actually closer to what DNM typed, I just omitted lots of it to save time. When I run the program, I pass it a filename as its first argument to open. Usually I just pass it the name of the source file of the program itself, which in its entirety looks like this:

code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>


// struct stat {
// dev_t st_dev; /* device inode resides on */
// ino_t st_ino; /* inode's number */
// mode_t st_mode; /* inode protection mode */
// nlink_t st_nlink; /* number or hard links to the file */
// uid_t st_uid; /* user-id of owner */
// gid_t st_gid; /* group-id of owner */
// dev_t st_rdev; /* device type, for special file inode */
// struct timespec st_atimespec; /* time of last access */
// struct timespec st_mtimespec; /* time of last data modification */
// struct timespec st_ctimespec; /* time of last file status change */
// off_t st_size; /* file size, in bytes */
// quad_t st_blocks; /* blocks allocated for file */
// u_long st_blksize;/* optimal file sys I/O ops blocksize */
// u_long st_flags; /* user defined flags for file */
// u_long st_gen; /* file generation number */
// };

int main(int argc, char **argv){
struct stat filestats;
memset(&filestats, 0, sizeof(struct stat));
int fd = open(argv[1], O_RDONLY);
printf("fileno is: %d\n", fd);
printf("fstat return is: %d\n", fstat(fd, &filestats));
//stat("test.c", &filestats);

char str[128];
printf("st_size: %d\n", filestats.st_size);
printf("st_dev: %d\n", filestats.st_dev);
printf("st_ino: %d\n", filestats.st_ino);
printf("st_uid: %d\n", filestats.st_uid);
printf("st_gid: %d\n", filestats.st_gid);
printf("st_blocks: %d\n", filestats.st_blocks);
printf("st_blksize: %lu\n", filestats.st_blksize);
printf("%s\n", str);
printf("%d\n", sizeof(off_t));
printf("%d\n", sizeof(struct stat));
return 0;
}

since I just compile the program with gcc in the same directory as the soruce file is in, I usually just execute it with the commandline:
code:
 ./a.out test.c 



--------------------
"The Buddha, the Godhead, resides quite as comfortably in the circuits of a digital computer or the gears of a cycle transmission as he does at the top of a mountain or in the petals of a flower." - Robert M. Pirsig

Posts: 948 | Registered: Mar 2005  |  IP: Logged
ASM65816
SuperBlabberMouth!
Member # 712

Member Rated:
2
Icon 1 posted October 06, 2005 15:06      Profile for ASM65816   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
Since you've got the file open, go ahead and read some number of bytes, then display them.

Without looking at anything else, I kind of agree with "you've got something but not what you think it is," which has to do with the Finder is not the same as the shells / command line.

.... digging though APUE ..... ow OW OWWW!   [crazy]

You could try using *opendir() and *readdir(), then look to see if d_ino (which is the i-node) is what you expect when you get to d_name (which is your filename). If you find the directory entry for your filename, but the i-node doesn't match -- you've got the wrong file.   [Frown]

I'll have to write some test code on my G4 to look for some unusual behavior.

EDIT: "ls -i" should give you the i-node values quick and easy.

--------------------
Once a proud programmer of Apple II's, he now spends his days and nights in cheap dives fraternizing with exotic dancers....

Posts: 1035 | From: Third rock from sun. | Registered: Mar 2001  |  IP: Logged
nerdwithnofriends
Uber Geek
Member # 3773

Icon 1 posted October 06, 2005 17:07      Profile for nerdwithnofriends     Send New Private Message       Edit/Delete Post   Reply With Quote 
Hrm, the same exact code compiles and runs on linux (2.6) just fine. Here is the output of the program:
code:
fileno is: 3
fstat return is: 0
st_size: 1838
st_dev: 268436752
st_ino: 2044840
st_uid: 500
st_gid: 500
st_blocks: 8
st_blksize: 4096

4
88

Here is the OS X output:

code:
 
fileno is: 3
fstat return is: 0
st_size: 0
st_dev: 234881026
st_ino: 1016294
st_uid: 501
st_gid: 501
st_blocks: 0
st_blksize: 4096

8
96

So why is this working on linux but not OS X? I suppose I'll try the dirent solution, and also try using stat() instead of fstat().

Stay tuned for updates.

--------------------
"The Buddha, the Godhead, resides quite as comfortably in the circuits of a digital computer or the gears of a cycle transmission as he does at the top of a mountain or in the petals of a flower." - Robert M. Pirsig

Posts: 948 | Registered: Mar 2005  |  IP: Logged
The Famous Druid

Gold Hearted SuperFan!
Member # 1769

Member Rated:
4
Icon 1 posted October 06, 2005 18:20      Profile for The Famous Druid     Send New Private Message       Edit/Delete Post   Reply With Quote 
st_size is only defined for 'regular' files (i.e. ST_ISREG() is true). The plain ol' file may not be as plain as you think (OS-X data fork/resource fork sillyness - anyone know how that works?)

Try printing out the value of st_mode (in hex), and check the bit flags against the values in sys/stat.h

btw - does stat() work?

--------------------
If you watch 'The History Of NASA' backwards, it's about a space agency that has no manned spaceflight capability, then does low-orbit flights, then lands on the Moon.

Posts: 10680 | From: Melbourne, Australia | Registered: Oct 2002  |  IP: Logged
ASM65816
SuperBlabberMouth!
Member # 712

Member Rated:
2
Icon 11 posted October 06, 2005 19:03      Profile for ASM65816   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
fstat() is working fine...... look at sizeof() ....

use printf() and %lld for the format....

I tried %ld and got "0" so I said "F"-it, used %lld and was surprised.   [crazy]

Note: I'm not sure what the defined behavior should be for %d, %ld, etc...   [ohwell]
  Also, Big-Endian or Little-Endian may make a difference. If you typecast the value with (int), then %d should accept it.

--------------------
Once a proud programmer of Apple II's, he now spends his days and nights in cheap dives fraternizing with exotic dancers....

Posts: 1035 | From: Third rock from sun. | Registered: Mar 2001  |  IP: Logged
The Famous Druid

Gold Hearted SuperFan!
Member # 1769

Member Rated:
4
Icon 1 posted October 06, 2005 20:39      Profile for The Famous Druid     Send New Private Message       Edit/Delete Post   Reply With Quote 
I believe ASM has hit the nail on the head here.

st_size is declared to be an off_t. If off_t is 64 bit on OS X, then printing it with %d won't work, you'll only see the high-order half, which will usually be 0. %lld will print the whole thing.

On linux, off_t will be 32 bit, unless you specify you want 64 bit support by
#define _LARGEFILE_SOURCE
#define _FILE_OFFSET_BITS 64

--------------------
If you watch 'The History Of NASA' backwards, it's about a space agency that has no manned spaceflight capability, then does low-orbit flights, then lands on the Moon.

Posts: 10680 | From: Melbourne, Australia | Registered: Oct 2002  |  IP: Logged
nerdwithnofriends
Uber Geek
Member # 3773

Icon 1 posted October 06, 2005 21:05      Profile for nerdwithnofriends     Send New Private Message       Edit/Delete Post   Reply With Quote 
Wow, you guys rock my socks. Thanks!

I guess I thought that since I'm working on an iMac G5 and the regular int size changes between 32 and 64 bits depending on how you compile something, a regular old '%d' token would be enough.

Guess I was wrong!!!

--------------------
"The Buddha, the Godhead, resides quite as comfortably in the circuits of a digital computer or the gears of a cycle transmission as he does at the top of a mountain or in the petals of a flower." - Robert M. Pirsig

Posts: 948 | Registered: Mar 2005  |  IP: Logged
garlicguy

Member # 3166

Member Rated:
5
Icon 1 posted October 06, 2005 22:05      Profile for garlicguy   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
Be still my heart! ASM and Druid agreed. Cap'n Vic de-lurked. All within a couple of hours of each other. [Applause] [thumbsup]

--------------------
I don't know what I was thinking... it seemed like a good idea at the time.

Posts: 3752 | From: Pluto, no matter what you call it, is still my home. | Registered: Dec 2004  |  IP: Logged
Rhonwyyn

Solid Gold SuperFan!
Member # 2854

Member Rated:
4
Icon 1 posted October 06, 2005 22:25      Profile for Rhonwyyn   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
What is this, an outbreak of peace, love, and harmony on these boards? [Eek!]

At least some of our elite members are singing different tunes...so charitable of them! [Big Grin]

--------------------
Change the way you SEE, not the way you LOOK!

Posts: 3849 | From: Lancaster, PA | Registered: Jul 2004  |  IP: Logged
ASM65816
SuperBlabberMouth!
Member # 712

Member Rated:
2
Icon 11 posted October 07, 2005 08:32      Profile for ASM65816   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
 - ... must ... destroy ... helmet !

--------------------
Once a proud programmer of Apple II's, he now spends his days and nights in cheap dives fraternizing with exotic dancers....

Posts: 1035 | From: Third rock from sun. | Registered: Mar 2001  |  IP: Logged


All times are Eastern Time  
Post New Topic  New Poll  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:

Contact Us | Geek Culture Home Page

© 2015 Geek Culture

Powered by Infopop Corporation
UBB.classicTM 6.4.0



homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam