;;;; -*- coding: utf-8 -*-
;;;;
;;;; hello.stk  -- A simple demo using fuse
;;;;
;;;; Copyright © 2005-2022 Erick Gallesio - I3S-CNRS/ESSI <eg@essi.fr>
;;;;
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;;; USA.
;;;;
;;;;           Author: Erick Gallesio [eg@essi.fr]
;;;;    Creation date:  7-Dec-2005 22:38 (eg)
;;;;

;;;; Adapt the load-path.
(when-load-and-compile
 (let ((path (load-path)))
   (unless (member "../lib" path)
     (load-path `("../lib"  ,@path)))))

;; to avoid warnings about undefined variables
(%compile-time-define   user-gid user-uid EACCES ENOENT)



(define-module HELLOFS
  (import (stklos fuse)
          (srfi 170))


  (define content "Hello, world!\n")

  (define (hellofs-main args)
    (fuse-mount args
     :getattr (lambda (path)
                (let ((tm (current-seconds)))
                  (cond
                    ((equal? path "/")
                     (vector (+ S_IFDIR #o755)       ;; mode
                             2                       ;; links
                             123                     ;; size (why not this one?)
                             (user-uid)              ;; uid
                             (user-gid)              ;; gid
                             tm tm tm))              ;; atime, mtime, ctime
                    ((equal? path "/hello")
                     (vector (+ S_IFREG #o440)       ;; mode
                             1                       ;; links
                             (string-length content) ;; size
                             (user-uid)              ;; uid
                             (user-gid)              ;; gid
                             tm tm tm))              ;; atime, mtime, ctime
                    (else    (- ENOENT)))))

     :readdir (lambda (path)
                (if (equal? path "/")
                    '("." ".." "hello")
                    (- ENOENT)))

     :open (lambda (path mode fd)
             (cond
               ((not (equal? path "/hello"))  (- ENOENT))
               ((not (equal? mode 0))         (- EACCES))
               (else                          0)))

     :read (lambda (fd size offset)
             (let ((len (string-length content)))
               (if (< offset len)
                   (begin
                     (if (> (+ offset size) len)
                         (set! size (- len offset)))
                     (substring content offset size))
                   0)))))

) ;; End of module HELLOFS

;; ======================================================================
;;      HELLOFS ...
;; ======================================================================
(define main (in-module HELLOFS hellofs-main))
