-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathposix.lisp
112 lines (90 loc) · 3.72 KB
/
posix.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CLPYTHON.MODULE.POSIX; Readtable: PY-USER-READTABLE -*-
;;
;; This software is Copyright (c) Franz Inc. and Willem Broekema.
;; Franz Inc. and Willem Broekema grant you the rights to
;; distribute and use this software as governed by the terms
;; of the Lisp Lesser GNU Public License
;; (http://opensource.franz.com/preamble.html),
;; known as the LLGPL.
(in-package :clpython.module.posix)
(in-syntax *user-readtable*)
;; #:|O_RDONLY| #:|O_WRONLY| #:|O_RDWR| #:|O_APPEND| #:|O_CREAT| #:|O_EXCL|
;; #:|O_TRUNC| #:|O_DSYNC| #:|O_RSYNC| #:|O_SYNC| #:|O_NDELAY| #:|O_NONBLOCK|
;; #:|O_NOCTTY| #:|O_SLOCK| #:|O_EXLOCK| #:|O_BINARY| #:|O_NOINHERIT|
;; #:|O_TEMPORARY| #:|O_RANDOM| #:|O_SEQUENTIAL| #:|O_TEXT| #:|SEEK_SET|
;; #:|SEEK_CUR| #:|SEEK_END|
;; Constants taken from CPython posix module on Mac OS X, check relevance/use.
(defconstant |O_RDONLY| 0)
(defconstant |O_WRONLY| 1)
(defconstant |O_RDWR| 2)
(defconstant |O_NONBLOCK| 4)
(defconstant |O_NDELAY| 4)
(defconstant |O_APPEND| 8)
(defconstant |O_SHLOCK| 16)
(defconstant |O_EXLOCK| 32)
(defconstant |O_SYNC| 128)
(defconstant |O_NOFOLLOW| 256)
(defconstant |O_CREAT| 512)
(defconstant |O_TRUNC| 1024)
(defconstant |O_EXCL| 2048)
(defconstant |O_NOCTTY| 131072)
(defconstant |O_DIRECTORY| 1048576)
(defun |open| (name flags &optional mode)
(when flags
(warn "posix.open(): ignoring flags: ~A" flags))
(py-call (find-class 'clpython:py-file) name (or mode "r")))
(defun |read| (file n)
"Read at most N bytes, returned as string. Empty string for EOF"
(funcall 'clpython:py-file.read file n))
(defun |close| (file)
(funcall 'clpython:py-file.close file))
(defun |remove| (path)
(|unlink| path))
(defun |unlink| (path)
(declare (ignore path))
(cl:error "TODO: posix.unlink(path)"))
(defconstant-once |error| (find-class '{OSError}))
;;; Stat
;; TODO support stat_float_times()
(defclass stat-result (clpython:object)
;; todo make named-tuple?
((stat :accessor sr-stat :initarg :stat))
(:metaclass clpython:py-type))
(defun |stat| (path)
(declare (ignorable path))
#-allegro (error "TODO: stat")
#+allegro
(let ((stat (handler-case (excl.osi:stat path)
(excl.osi:syscall-error (c)
(py-raise '{OSError} (format nil "~A" c))))))
(make-instance 'stat-result :stat stat)))
(def-py-method stat-result.st_mode :attribute (x)
#+allegro (excl.osi:stat-mode (sr-stat x))
#-allegro (error "TODO: stat-result.st_mode"))
(def-py-method stat-result.st_ino :attribute (x)
#+allegro (excl.osi:stat-ino (sr-stat x))
#-allegro (error "TODO: stat-result.st_ino"))
(def-py-method stat-result.st_dev :attribute (x)
#+allegro (excl.osi:stat-dev (sr-stat x))
#-allegro (error "TODO: stat-result.st_dev"))
(def-py-method stat-result.st_nlink :attribute (x)
#+allegro (excl.osi:stat-nlink (sr-stat x))
#-allegro (error "TODO: stat-result.st_nlink"))
(def-py-method stat-result.st_uid :attribute (x)
#+allegro (excl.osi:stat-uid (sr-stat x))
#-allegro (error "TODO: stat-result.st_uid"))
(def-py-method stat-result.st_gid :attribute (x)
#+allegro (excl.osi:stat-gid (sr-stat x))
#-allegro (error "TODO: stat-result.st_gid"))
(def-py-method stat-result.st_size :attribute (x)
#+allegro (excl.osi:stat-size (sr-stat x))
#-allegro (error "TODO: stat-result.st_size"))
(def-py-method stat-result.st_atime :attribute (x)
#+allegro (excl.osi:stat-atime (sr-stat x))
#-allegro (error "TODO: stat-result.st_atime"))
(def-py-method stat-result.st_mtime :attribute (x)
#+allegro (excl.osi:stat-mtime (sr-stat x))
#-allegro (error "TODO: stat-result.st_mtime"))
(def-py-method stat-result.st_ctime :attribute (x)
#+allegro (excl.osi:stat-ctime (sr-stat x))
#-allegro (error "TODO: stat-result.st_ctime"))