Opened 12 years ago

Closed 12 years ago

#4824 closed Bugs (worksforme)

Boost file system function file_size() function is returning 0 for /proc virtual file system files.

Reported by: pmanth2@… Owned by: Beman Dawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.44.0 Severity: Problem
Keywords: Cc:

Description

The tellg function of ifstream object is returning -1 when the position of file pointer is queried for /proc virtual file system files. Below is the example program to demonstrate the problem

#include <iostream #include <fstream> using namespace std;

int main( int argc, char* argv[] ){

long begin,end; ifstream myfile (argv[1]); begin = myfile.tellg(); cout << " begin " << begin << endl; myfile.seekg (0, ios::end); end = myfile.tellg(); myfile.close(); cout << "size is: " << (end-begin) << " bytes.\n"; return 0;

}

pmanth2@cyder:~$ ./fstream partA.cpp ( Normal text file )

begin 0

size is: 6692 bytes.

pmanth2@cyder:~$ ./fstream /proc/cpuinfo ( virtual file system file )

begin 0

size is: -1 bytes.

The tellg() should return the file pointer location for /proc file system files also.

Change History (4)

comment:1 by anonymous, 12 years ago

Summary: tellg () function of ifstream object is returning -1 for /proc virtual file system files.Boost file system function file_size() function is returning 0 for /proc virtual file system files.

The boost filesystem function file_size returning 0 for /proc file system files. Below is the example program to demonstrate the problem.

#include <boost/filesystem/operations.hpp> #include <iostream>

namespace fs = boost::filesystem;

int main( int argc, char* argv[] ) {

if ( argc != 2 ) {

std::cout << "Usage: file_size path\n"; return 1;

}

fs::path p( argv[1], fs::native );

if ( !fs::exists( p ) ) {

std::cout << "not found: " << argv[1] << std::endl; return 1;

}

if ( !fs::is_regular( p ) ) {

std::cout << "not a regular file: " << argv[1] << std::endl; return 1;

}

std::cout << "size of " << argv[1] << " is " << fs::file_size( p ) ---------- boost file system file_size function

<< std::endl;

return 0;

}

Output:

pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ ./new k size of k is 46 pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ wc -c k 46 k pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ wc -c /proc/cpuinfo 10102 /proc/cpuinfo pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ ./new /proc/cpuinfo size of /proc/cpuinfo is 0 --------------------- ( should return actual size of file just as word count )

comment:2 by Jeremiah Willcock, 12 years ago

Are you sure that these are not just the values returned by the kernel? ls -l on /proc/cpuinfo returns a size of 0 as well. I do not know about the tellg behavior, but it could be from a similar issue.

in reply to:  2 comment:3 by anonymous, 12 years ago

Replying to jewillco:

Are you sure that these are not just the values returned by the kernel? ls -l on /proc/cpuinfo returns a size of 0 as well. I do not know about the tellg behavior, but it could be from a similar issue.

Hi!

Thanks for the quick response.

"ls -ltr /proc/cpuinfo" is retruning 0 whereas "wc /proc/cpuinfo" retruning size 10102

pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ ls -ltr /proc/cpuinfo -r--r--r-- 1 root root 0 2010-11-10 10:40 /proc/cpuinfo

pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ wc /proc/cpuinfo 312 1872 10102 /proc/cpuinfo

pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$

I guess file_size should return 10102 in this case.. I think this is special case for proc files.

When normal files are considered both "ls -ltr " and wc give the same result.

thanks pradeep

comment:4 by Beman Dawes, 12 years ago

Resolution: worksforme
Status: newclosed

On my Linux VirtualBox:

$ ls -l /proc/cpuinfo -r--r--r-- 1 root root 0 2010-12-03 15:43 /proc/cpuinfo

so I'm closing this issue as "Works for me".

I think you may be confusing the results returned by various usages of the command line interpreter you are using with the results being reported by your operating system's API (which is POSIX-based, in the case of Linux).

The Boost filesystem library is just reporting whatever the operating system's API tells it. In the case of file size, the ls -l file size is a pretty good surrogate. The results of wc are a very poor surrogate.

"When normal files are considered both "ls -ltr " and wc give the same result."

Check you tests again - the two commands give very different results, as expected.

HTH,

--Beman

Note: See TracTickets for help on using tickets.