1

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

Sumit Paliwal
  • 385
  • 1
  • 3
  • 14

2 Answers2

2
  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.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • As per your suggestion, I've now checked for the returned values from n_bytes, it is 90,66 and 1523 in different calls randomly. So it is writing something. But still the rest output is the same. – Sumit Paliwal Apr 29 '15 at 11:05
  • `temp_data.st_size` is an `off_t`, not a `long long int`... so providing it as an argument corresponding to `%lld` invokes undefined behaviour. Perhaps you could use `%llu` and cast that argument like so: `(unsigned long long) temp_data.st_size`... – autistic Apr 29 '15 at 11:41
0

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()

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • I've made changes in the question now. The return value of n_bytes is always greater than 0. So it is always writing something at least. – Sumit Paliwal Apr 29 '15 at 11:29
  • @SumitPaliwal i also updated my answer. Now check for type of file? – Jeegar Patel Apr 29 '15 at 11:30
  • Thanks for the reply.. I've checked for the file type.. it is not a regular file, it is SOCKET. So is there any way to know that how much data is written in this file?? – Sumit Paliwal Apr 29 '15 at 11:46
  • So whats the type of your file descriptor? If my answer helped you then you can accept my answer by clicking right mark – Jeegar Patel Apr 29 '15 at 11:48
  • So its not a regular file so size can NOT be derived by fstate. For this socket you can maintain counter. After each write() you can increment it by its return value. – Jeegar Patel Apr 29 '15 at 11:53
  • I'll also have to update the counter on recv or read from the same socket. Which is not feasible in my case. So is there any other alternate way to find the size of the socket file?? – Sumit Paliwal Apr 29 '15 at 12:19
  • Why would you want the "size of socket file"? It's not possible to do it this way, so tell us what's your purpose and we might try to help you. – zoska Apr 29 '15 at 12:20
  • My purpose is to find the current data in the socket file, so when its size reaches a certain threshold, I'll change the socket, on which the further data will be written. – Sumit Paliwal Apr 29 '15 at 12:28