-1

Is there some smart way of reading content of file by lines (in python) if this file kinda acts like buffer?

To be more specific I'm implementing simple firewall as kernel module which communicates with user space application through procfs. When userspace application requests to print all firewall rules procf_read function is called and all rules are stored in buffer which is later copied to user (copy_to_user func). If I use simple for loop to read file by lines it loops over file and prints lines just fine (let's say there are 3 of them) but then it loops over printing same lines over and over until whole buffer is read (my guess). I would like to print just these 3 lines ideally without inserting extra symbol to file's end to simulate 'EOF' and then detecting it in user space (python). I'm thinking of reading file by bytes but how to get number of bytes to read in C's fread() way?

mezo
  • 423
  • 1
  • 7
  • 19
  • 2
    Show your code. You should be able to split the buffer into a list and loop over that. – Barmar Apr 29 '15 at 22:11
  • Unless the file is infinite, one inherent quality will always be that it will end, (and therefore exhibit an _end of file_) ***[EOF](http://en.wikipedia.org/wiki/End-of-file)*** is a condition, that in turn precipitates a flag that is set by C libraries to indicate by return value that the end of the file has been reached. – ryyker Apr 29 '15 at 22:26
  • It sounds like there is a problem in your implementation of the procfs module. Are you returning the correct read size from your read callback? – rici Apr 29 '15 at 22:50
  • I'm sending data to userspace by calling copy_to_user(buffer, procf_buffer, procf_buffer_pos), whereprocf_buffer_pos is size of data sent. I'm returning this value in procf_read func – mezo Apr 29 '15 at 23:06
  • @mezo: and do you check and respond to the off parameter? – rici Apr 29 '15 at 23:33
  • If the 'off parameter' stands for return value then no and thats exactly my question - how to check it in python – mezo Apr 30 '15 at 08:13
  • @mezo: the read callback to implement a procfs entry has an off parameter, which tells you which byte position to start reading from. If that byte position is greater than the data size, you need to return 0 from the callback to indicate EOF. Also, if you want to attract my attention, you need to address the comment to me using an @ and my nick. – rici Apr 30 '15 at 15:46

1 Answers1

1

Files never contains EOF, it's not a character, so there is no way of doing what you want because it does not make sense.

When you are reading from a file, the library marks the file handle with EOF when it attempts to read past the end of the file, after that a call to feof(file) would return a non-zero value. But there is no character read from the file that is the EOF character, if there was then how would you be able to store this particular value in a file?

See this for more information.

You can build the following solution

size_t fileSize;

fseek(file, 0L, SEEK_END);
fileSize = ftell(file);
fseek(file, 0L, SEEK_SET);

now you do know what to pass to fread().

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • You're right. I didn't express that in right way, but still my point is clear, so despite of how I'm glad you enlighten me of how EOF trully works that's NOT answer to my question which would be appreciated. – mezo Apr 29 '15 at 22:24
  • Your question is diffuclt to read or understand, you might want to improve it's formatting and I promise that then I will try to give an answer that you accept. – Iharob Al Asimi Apr 29 '15 at 22:25
  • Oh and I thought tag python did it, but obviously not. User space application is written in python. So I'm looking for solution in python. in C I would obviously use fread. – mezo Apr 29 '15 at 22:28
  • AFAIK in python you can just use `.readlines()` method of file objects, and you don't need to care about the rest, why isn't it working? – Iharob Al Asimi Apr 29 '15 at 22:33
  • Because as I stated in question, it loops over printing a few lines sent to proc file over and over again... – mezo Apr 29 '15 at 22:47