Sumit Paliwal
Sumit Paliwal

Reputation: 395

fstat() return 0, with file size 0 and errno 11

I'm trying the following thing in my code:-

{

   int y,n_bytes;

   struct stat temp_data;

   y = fstat(netdev->queue_fd[class_id],&temp_data);
   printf("Success - %d , size -> %lld , Error- %d \n",y,temp_data.st_size,errno);
   n_bytes = write(netdev->queue_fd[class_id],buffer->data,buffer->size);
   y = fstat(netdev->queue_fd[class_id],&temp_data);
   printf("After write Success - %d , size -> %lld , Error- %d and the value of n_bytes is - %d ",y,temp_data.st_size,errno,n_bytes);

}

and the output that I'm getting is :-

Success - 0, size -> 0 , Error - 11 
After write Success - 0, size -> 0, Error - 11 and the value of n_bytes is - 1526 

What is the reason behind the size being 0 and the error number 11?? Is there any other way to get the size of the file??

Note: here Netdev->queue_fd[class_id] is a file descriptor. The value of n_bytes is varying in between {41,1514,66,..} in different calls. (Always greater than 0)

thanks

Upvotes: 1

Views: 1239

Answers (2)

Jeegar Patel
Jeegar Patel

Reputation: 27210

Netdev->queue_fd[class_id] is a file descriptor of

  • Regular file or
  • Any sys/fs entry or
  • Char/Block/Network device file?

if it is not a regular file then its size will not be updated after write command.

check file tyeps by S_ISREG()

Upvotes: 0

Andrew Henle
Andrew Henle

Reputation: 1

  1. The status of errno after success is irrelevant. The value of errno is only modified upon failure. fstat() returned zero, so the value of errno is doesn't matter.

  2. What does write() return? You're not checking so you don't know that the file should be larger after the write() call.

Upvotes: 2

Related Questions