Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slurping /proc/cpuinfo with clojure

(Clojure newbie)

On my linux machine, slurping /proc/cpuinfo raises an error:

user=> (slurp "/proc/cpuinfo")
java.io.IOException: Invalid argument (NO_SOURCE_FILE:0)

Anybody knows why that is? (is the /proc filesystem some kind of second-class citizen in Java?)

Edit: the following code, adapted from nakkaya.com, works flawlessly:

(with-open [rdr (java.io.BufferedReader. 
             (java.io.FileReader. "/proc/cpuinfo"))]
   (let [seq (line-seq rdr)]
   (apply print seq)))

I wonder why this difference ?

like image 564
Rom1 Avatar asked Oct 28 '25 23:10

Rom1


2 Answers

I've had a similar problem with files in /proc. The solution is simple though:

(slurp (java.io.FileReader. "/proc/cpuinfo"))
like image 165
MHOOO Avatar answered Oct 30 '25 15:10

MHOOO


the problem is that java cannot open a DataInputStream on /proc so the slurp function isnt going to help you here, sorry :(

/proc/cpuinfo is a little strange because it has a file size of zero and produces bytes when read. this upsets the smarter java file handling classes.

ls -l /proc/cpuinfo
-r--r--r-- 1 root root 0 2012-01-20 00:10 /proc/cpuinfo

see this thread for more http://www.velocityreviews.com/forums/t131093-java-cannot-access-proc-filesystem-on-linux.html

you are going to have to open it with a FileReader. I'll add an example in a bit

like image 30
Arthur Ulfeldt Avatar answered Oct 30 '25 15:10

Arthur Ulfeldt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!