[racket] Socket and character resp. block device file

From: Manfred Lotz (manfred.lotz at arcor.de)
Date: Mon Jan 6 11:31:25 EST 2014

On Mon, 6 Jan 2014 06:34:11 -0700
Matthew Flatt <mflatt at cs.utah.edu> wrote:

> At Mon, 6 Jan 2014 13:47:30 +0100, Manfred Lotz wrote:
> > On Mon, 6 Jan 2014 10:32:49 +0100
> > Manfred Lotz <manfred.lotz at arcor.de> wrote:
> > 
> > > I was to quick to state this is a solution as it is not a good
> > > idea if for example a socket file will be shown as a duplicate of
> > > normal file having size zero.
> > > 
> > > For the time being I guess I have to look how to implement it
> > > using C stat call from Racket.
> > > 
> > > 
> > 
> > To get into I tried the example in 'Indide Racket C API'/Overview
> > 
> > I got:
> > compiled
> > compiled/native
> > compiled/native/amd64-linux
> > compiled/native/amd64-linux/3m
> > compiled/native/amd64-linux/3m/hi_rkt.so
> > 
> > 
> > Documentation says that it could be loaded like this: 
> > 
> >  (require "hi.rkt")
> > 
> > 
> > Now my question: What would the content of hi.rkt be?
> 
> The analogous source "hi.rkt" would be
> 
>  #lang racket/base
>  (provide greeting)
>  (define greeting "hello")
> 
> but you don't actually create a "hi.rkt". Instead, the module loader
> substitutes "hit_rkt.so".
> 
> 
> Did you consider using `ffi/unsafe` instead of creating an extension?
> Using the FFI library is usually much easier and more portable.
> Unfortunately, the `stat` function, with its structure layout that
> varies across platforms, is more difficult to call via `ffi/unsafe`,
> but I would still do that instead of trying to write an extension.
> 

To be honest, no. I've never did anything like this before. When I
found the extension stuff it looked quite good to me because of the
'hi' example.

Now I messed around with the FFI stuff. I tried out a small C programm
to double check if the stat struct has the right length. I'm not quite
sure if I got the internal name of the stat function right. At least I
still get a -1 from stat.

#lang racket

(require ffi/unsafe
         ffi/unsafe/define)

(define-cstruct _STAT 
  ((dev_t _long)
   (ino_t _long)
   (nlink_t _long)
   (mode_t _int)
   (uid_t _int)
   (gid_t _int)
   (pad0 _int)
   (rdev_t _long)
   (size_t _long)
   (blksize_t _long)
   (blkcnt_t _long)
   (atime_t _long)
   (atime_nsec_t _long)
   (mtime_t _long)
   (mtime_nsec_t _long)
   (ctime_t _long)
   (ctime_nsec_t _long)
   (unused1 _long)
   (unused2 _long)
   (unused3 _long)))


(define-ffi-definer define-stats (ffi-lib "libc.so.6"))


(define-stats __xstat64 (_fun _string _STAT-pointer  -> _int))
(define-stats __xstat (_fun _string _STAT-pointer  -> _int))


(define (socket-exists f)
  (define s (make-STAT 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))
  (__xstat64 f s))

(socket-exists "cstat.c")



However, I agree. If I get the struct stuff straight then for what I
want it is indeed easier to do the FFI stuff


-- 
Manfred










Posted on the users mailing list.