Commit c4eabc01bd6b8295d39b65f8a11dea3000dee573

Baptiste 2024-08-06T19:24:50

httpd: wip: file server

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
diff --git a/.ikc3_history b/.ikc3_history
index 8766cd7..d6ec06d 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,23 +1,3 @@
-type(-256 * 256 / 2 + 1)
-type(-256 * 128 + 1)
-type(-256 * 128 + 2)
-type(-256 * 256 / 2 + 1)
-type(-128 * 128 / 2 + 1)
-type(-255 * 256 / 2 + 1)
-type(-255 * 256 + 1)
--255 * 256 + 1
--255 * 256
--256 * 256 / 2
--256 * 256 / 2 + 1
-type(-32767)
-type(-256 * 256 / 2 + 1)
--32767
-type(-32767)
-(S16) -32767
-i = type(-32767)
--32768 * 65536
--32768 * 65536 + 1
--2147483648 * 65536 * 65536 + 1
 q
 %KC3.Operator{}
 %KC3.Operator{sym: :-}
@@ -97,3 +77,24 @@ type(4294967296)
 type( -2147483648)
 type(4294967296)
 type(-9223372036854775808)
+File.exists?("env")
+File.exists?("env4")
+File.exists?("lib/kc3/0.1/file.kc3")
+File.stat("env")
+File.stat("env").st_mode
+def is_directory? = fn (path) { List.has(File.stat(path).st_mode, :directory) }
+File.stat("libkc3").st_mode
+List.has
+def is_directory? = fn (path) { List.has(File.stat(path).st_mode, :directory) }
+List.has
+List.has?
+is_directory?("env")
+def is_directory? = fn (path) { List.has?(File.stat(path).st_mode, :directory) }
+is_directory?("env")
+is_directory?("libkc3")
+def is_directory? = fn (path) { List.has?(File.stat(path).st_mode, :directory) }
+File.is_directory?("env")
+File.is_directory?("libkc3")
+File.is_directory?("plop1234")
+File.is_directory?("env")
+
diff --git a/lib/kc3/0.1/file.kc3 b/lib/kc3/0.1/file.kc3
index 2496ef9..ea0ebaa 100644
--- a/lib/kc3/0.1/file.kc3
+++ b/lib/kc3/0.1/file.kc3
@@ -2,4 +2,15 @@ defmodule File do
 
   def exists? = cfn Bool "file_exists" (Str, Result)
 
+  def is_directory? = fn (path) do
+    sb = File.stat(path)
+    if (sb) do
+      List.has?(sb.st_mode, :directory)
+    else
+      false
+    end
+  end
+
+  def stat = cfn File.Stat "file_stat" (Str, Result)
+
 end
diff --git a/lib/kc3/0.1/file/stat.kc3 b/lib/kc3/0.1/file/stat.kc3
new file mode 100644
index 0000000..113f984
--- /dev/null
+++ b/lib/kc3/0.1/file/stat.kc3
@@ -0,0 +1,16 @@
+defmodule File.Stat do
+
+  defstruct [st_dev: (Uw) 0,     # ID of device containing file
+             st_ino: (Uw) 0,     # Inode number
+             st_mode: [],        # File type and mode
+             st_nlink: (Uw) 0,   # Number of hard links
+             st_uid: (Uw) 0,     # User ID of owner
+             st_gid: (Uw) 0,     # Group ID of owner
+             st_rdev: (Uw) 0,    # Device ID (if special file)
+             st_size: (Uw) 0,    # Total size, in bytes
+             st_blksize: (Uw) 0, # Block size for filesystem I/O
+             st_blocks: (Uw) 0,  # Number of 512B blocks allocated
+             st_atim: %Time{},   # Time of last access
+             st_mtim: %Time{},   # Time of last modification
+             st_ctim: %Time{}]   # Time of last status change
+end
diff --git a/lib/kc3/0.1/list.kc3 b/lib/kc3/0.1/list.kc3
index 6d992c8..962245b 100644
--- a/lib/kc3/0.1/list.kc3
+++ b/lib/kc3/0.1/list.kc3
@@ -2,6 +2,8 @@ defmodule List do
 
   def cast = cfn List "list_init_cast" (Result, Sym, Tag)
 
+  def has? = cfn Bool "list_has" (List, Tag)
+
   def map = fn {
     ([], _) do
       []
diff --git a/lib/kc3/0.1/time.kc3 b/lib/kc3/0.1/time.kc3
new file mode 100644
index 0000000..b7439d5
--- /dev/null
+++ b/lib/kc3/0.1/time.kc3
@@ -0,0 +1,5 @@
+defmodule Time do
+
+  defstruct [tv_sec: (Uw) 0, tv_nsec: (U32) 0]
+
+end
\ No newline at end of file
diff --git a/libkc3/buf.c b/libkc3/buf.c
index 6b5e131..7dabfd5 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -925,11 +925,8 @@ sw buf_refill (s_buf *buf, sw size)
       ! size)
     return r;
   if (buf->rpos + size > buf->wpos) {
-    if ((r = buf_refill_compact(buf)) < 0) {
-      err_puts("buf_refill: buf_refill_compact");
-      assert(! "buf_refill: buf_refill_compact");
+    if ((r = buf_refill_compact(buf)) < 0)
       return r;
-    }
     if (buf->refill)
       while (1) {
         if ((r = buf->refill(buf)) < 0)
diff --git a/libkc3/file.c b/libkc3/file.c
index 05589a2..66e7a25 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -148,7 +148,7 @@ s_tag * file_mtime (const s_str *path, s_tag *dest)
 #if HAVE_STAT_MTIM
   return time_to_tag(&sb.st_mtim, dest);
 #else
-  s_time tmp = {0};
+  s_timespec tmp = {0};
   tmp.tv_sec = sb.st_mtime;
   tmp.tv_nsec = 0;
   return time_to_tag(&tmp, dest);
@@ -223,3 +223,42 @@ s_str * file_search (const s_str *suffix, const s_sym *mode,
   }
   return NULL;
 }
+
+s_file_stat * file_stat (const s_str *path, s_file_stat *dest)
+{
+  s32 e;
+  struct stat sb;
+  s_file_stat tmp = {0};
+  assert(path);
+  assert(dest);
+  if (stat(path->ptr.pchar, &sb)) {
+    e = errno;
+    err_write_1("file_stat: ");
+    err_write_1(strerror(e));
+    err_write_1(": ");
+    err_puts(path->ptr.pchar);
+    return NULL;
+  }
+  tmp.st_dev = sb.st_dev;
+  tmp.st_ino = sb.st_ino;
+  tmp.st_mode = NULL;
+  if (sb.st_mode & S_IFREG)
+    tmp.st_mode = list_new_sym(&g_sym_file, tmp.st_mode);
+  else if (sb.st_mode & S_IFDIR)
+    tmp.st_mode = list_new_sym(&g_sym_directory, tmp.st_mode);
+  tmp.st_nlink = sb.st_nlink;
+  tmp.st_uid = sb.st_uid;
+  tmp.st_gid = sb.st_gid;
+  tmp.st_rdev = sb.st_rdev;
+  tmp.st_size = sb.st_size;
+  tmp.st_blksize = sb.st_blksize;
+  tmp.st_blocks = sb.st_blocks;
+  tmp.st_atim.tv_sec = sb.st_atim.tv_sec;
+  tmp.st_atim.tv_nsec = sb.st_atim.tv_nsec;
+  tmp.st_mtim.tv_sec = sb.st_mtim.tv_sec;
+  tmp.st_mtim.tv_nsec = sb.st_mtim.tv_nsec;
+  tmp.st_ctim.tv_sec = sb.st_ctim.tv_sec;
+  tmp.st_ctim.tv_nsec = sb.st_ctim.tv_nsec;
+  *dest = tmp;
+  return dest;
+}
\ No newline at end of file
diff --git a/libkc3/file.h b/libkc3/file.h
index 1bb1992..7a2e61a 100644
--- a/libkc3/file.h
+++ b/libkc3/file.h
@@ -31,6 +31,7 @@ s_str * file_dirname (const s_str *path, s_str *dest);
 s_tag * file_mtime (const s_str *path, s_tag *dest);
 s_str * file_search (const s_str *suffix, const s_sym *mode,
                      s_str *dest);
+s_file_stat * file_stat (const s_str *path, s_file_stat *dest);
 
 /* Operators. */
 s_str * file_pwd (s_str *dest);
diff --git a/libkc3/list.c b/libkc3/list.c
index 51c30cf..fe8b0ac 100644
--- a/libkc3/list.c
+++ b/libkc3/list.c
@@ -58,10 +58,10 @@ void list_f_clean (s_list **list)
     l = list_delete(l);
 }
 
-bool list_has (const s_list *list, const s_tag *tag)
+bool list_has (const s_list * const *list, const s_tag *tag)
 {
   const s_list *l;
-  l = list;
+  l = *list;
   while (l) {
     if (! compare_tag(tag, &l->tag))
       return true;
diff --git a/libkc3/list.h b/libkc3/list.h
index ce03cd0..33693ed 100644
--- a/libkc3/list.h
+++ b/libkc3/list.h
@@ -50,7 +50,7 @@ s_list * list_new_tag_copy (const s_tag *tag, s_list *next);
 
 /* Observers */
 s_list ** list_cast (const s_tag *tag, s_list **list);
-bool      list_has (const s_list *list, const s_tag *tag);
+bool      list_has (const s_list * const *list, const s_tag *tag);
 bool      list_is_alist (const s_list * const *list);
 bool      list_is_plist (const s_list *list);
 sw        list_length (const s_list *list);
diff --git a/libkc3/sym.c b/libkc3/sym.c
index 6e05ef3..4e15299 100644
--- a/libkc3/sym.c
+++ b/libkc3/sym.c
@@ -80,9 +80,11 @@ const s_sym g_sym_arity           = {{{NULL},  5, {"arity"}}};
 const s_sym g_sym_cast            = {{{NULL},  4, {"cast"}}};
 const s_sym g_sym_clean           = {{{NULL},  5, {"clean"}}};
 const s_sym g_sym_defstruct       = {{{NULL},  9, {"defstruct"}}};
+const s_sym g_sym_directory       = {{{NULL},  9, {"directory"}}};
 const s_sym g_sym_do              = {{{NULL},  2, {"do"}}};
 const s_sym g_sym_else            = {{{NULL},  4, {"else"}}};
 const s_sym g_sym_end             = {{{NULL},  3, {"end"}}};
+const s_sym g_sym_file            = {{{NULL},  4, {"file"}}};
 const s_sym g_sym_fn              = {{{NULL},  2, {"fn"}}};
 const s_sym g_sym_if              = {{{NULL},  2, {"if"}}};
 const s_sym g_sym_if_then_else    = {{{NULL}, 12, {"if_then_else"}}};
@@ -376,9 +378,11 @@ void sym_init_g_sym (void)
   sym_register(&g_sym_cast, NULL);
   sym_register(&g_sym_clean, NULL);
   sym_register(&g_sym_defstruct, NULL);
+  sym_register(&g_sym_directory, NULL);
   sym_register(&g_sym_do, NULL);
   sym_register(&g_sym_else, NULL);
   sym_register(&g_sym_end, NULL);
+  sym_register(&g_sym_file, NULL);
   sym_register(&g_sym_fn, NULL);
   sym_register(&g_sym_if, NULL);
   sym_register(&g_sym_if_then_else, NULL);
diff --git a/libkc3/sym.h b/libkc3/sym.h
index 6260132..37a7af2 100644
--- a/libkc3/sym.h
+++ b/libkc3/sym.h
@@ -84,9 +84,11 @@ extern const s_sym g_sym_arity;
 extern const s_sym g_sym_cast;
 extern const s_sym g_sym_clean;
 extern const s_sym g_sym_defstruct;
+extern const s_sym g_sym_directory;
 extern const s_sym g_sym_do;
 extern const s_sym g_sym_else;
 extern const s_sym g_sym_end;
+extern const s_sym g_sym_file;
 extern const s_sym g_sym_fn;
 extern const s_sym g_sym_if;
 extern const s_sym g_sym_if_then_else;
diff --git a/libkc3/tag.c b/libkc3/tag.c
index 8e34552..9431f7c 100644
--- a/libkc3/tag.c
+++ b/libkc3/tag.c
@@ -534,7 +534,7 @@ s_tag * tag_init_copy (s_tag *tag, const s_tag *src)
 
 s_tag * tag_init_time (s_tag *tag)
 {
-  s_time time;
+  s_timespec time;
   clock_gettime(CLOCK_REALTIME, &time);
   return time_to_tag(&time, tag);
 }
diff --git a/libkc3/time.c b/libkc3/time.c
index c5b438a..79293dd 100644
--- a/libkc3/time.c
+++ b/libkc3/time.c
@@ -14,7 +14,7 @@
 #include "tag.h"
 #include "time.h"
 
-s_time * time_sub (const s_time *a, const s_time *b, s_time *dest)
+s_timespec * time_sub (const s_timespec *a, const s_timespec *b, s_timespec *dest)
 {
   if ((a->tv_nsec - b->tv_nsec) < 0) {
     dest->tv_sec = a->tv_sec - b->tv_sec - 1;
@@ -26,13 +26,13 @@ s_time * time_sub (const s_time *a, const s_time *b, s_time *dest)
   return dest;
 }
 
-f64 * time_to_f64 (const s_time *time, f64 *dest)
+f64 * time_to_f64 (const s_timespec *time, f64 *dest)
 {
   *dest = (f64) time->tv_sec + (f64) time->tv_nsec * 0.000000001;
   return dest;
 }
 
-s_tag * time_to_tag (const s_time *time, s_tag *dest)
+s_tag * time_to_tag (const s_timespec *time, s_tag *dest)
 {
   s_tag tmp = {0};
   assert(time);
diff --git a/libkc3/time.h b/libkc3/time.h
index 5ab5370..3318471 100644
--- a/libkc3/time.h
+++ b/libkc3/time.h
@@ -12,7 +12,7 @@
  */
 /**
  * @file time.h
- * @brief operations on s_time
+ * @brief operations on s_timespec
  *
  */
 #ifndef LIBKC3_TIME_H
@@ -21,10 +21,10 @@
 #include "types.h"
 
 /* Observers */
-f64 *   time_to_f64 (const s_time *time, f64 *dest);
-s_tag * time_to_tag (const s_time *time, s_tag *dest);
+f64 *   time_to_f64 (const s_timespec *time, f64 *dest);
+s_tag * time_to_tag (const s_timespec *time, s_tag *dest);
 
 /* Operators */
-s_time * time_sub (const s_time *a, const s_time *b, s_time *dest);
+s_timespec * time_sub (const s_timespec *a, const s_timespec *b, s_timespec *dest);
 
 #endif /* LIBKC3_TIME_H */
diff --git a/libkc3/types.h b/libkc3/types.h
index b4a5c4d..54c2a81 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -162,6 +162,7 @@ typedef struct facts_cursor            s_facts_cursor;
 typedef struct facts_spec_cursor       s_facts_spec_cursor;
 typedef struct facts_with_cursor       s_facts_with_cursor;
 typedef struct facts_with_cursor_level s_facts_with_cursor_level;
+typedef struct file_stat               s_file_stat;
 typedef struct float_s                 s_float;
 typedef struct fn                      s_fn;
 typedef struct fn_clause               s_fn_clause;
@@ -185,7 +186,8 @@ typedef struct sym                     s_sym;
 typedef struct sym_list                s_sym_list;
 typedef struct tag                     s_tag;
 typedef struct tag_type_list           s_tag_type_list;
-typedef struct timespec                s_time;
+typedef struct time                    s_time;
+typedef struct timespec                s_timespec;
 typedef struct tuple                   s_tuple;
 typedef struct type                    s_type;
 typedef struct unquote                 s_unquote;
@@ -322,6 +324,11 @@ struct tag_type_list {
   s_tag_type_list *next;
 };
 
+struct time {
+  uw tv_sec;
+  u32 tv_nsec;
+};
+
 struct tuple {
   uw count;
   s_tag *tag;
@@ -365,6 +372,22 @@ struct facts_spec_cursor {
   uw pos;
 };
 
+struct file_stat {
+  uw st_dev;      /* ID of device containing file */
+  uw st_ino;      /* Inode number */
+  s_list * st_mode; /* File type and mode */
+  uw st_nlink;    /* Number of hard links */
+  uw st_uid;      /* User ID of owner */
+  uw st_gid;      /* Group ID of owner */
+  uw st_rdev;     /* Device ID (if special file) */
+  uw st_size;     /* Total size, in bytes */
+  uw st_blksize;  /* Block size for filesystem I/O */
+  uw st_blocks;   /* Number of 512B blocks allocated */
+  s_time st_atim; /* Time of last access */
+  s_time st_mtim; /* Time of last modification */
+  s_time st_ctim; /* Time of last status change */
+};
+
 struct fn {
   s_fn_clause *clauses;
   bool macro;
@@ -563,7 +586,7 @@ struct sequence {
   f64 duration;
   u64 frame;
   f64 t;
-  s_time t0;
+  s_timespec t0;
   const char *title;
   void *window;
   f_sequence load;
diff --git a/libkc3_window/cairo/win32/window_cairo_win32.c b/libkc3_window/cairo/win32/window_cairo_win32.c
index 417033b..1c7827c 100644
--- a/libkc3_window/cairo/win32/window_cairo_win32.c
+++ b/libkc3_window/cairo/win32/window_cairo_win32.c
@@ -95,7 +95,7 @@ bool window_cairo_win32_run (s_window_cairo *window)
 {
   HWND hwnd;
   MSG msg;
-  s_time sleep;
+  s_timespec sleep;
   WNDCLASSEX wc;
   wc.cbSize        = sizeof(WNDCLASSEX);
   wc.style         = 0;
diff --git a/libkc3_window/cairo/xcb/window_cairo_xcb.c b/libkc3_window/cairo/xcb/window_cairo_xcb.c
index 5e15333..4b41263 100644
--- a/libkc3_window/cairo/xcb/window_cairo_xcb.c
+++ b/libkc3_window/cairo/xcb/window_cairo_xcb.c
@@ -104,7 +104,7 @@ bool window_cairo_xcb_run (s_window_cairo *window)
   bool r;
   xcb_screen_t *screen;
   xcb_visualtype_t *screen_visual;
-  s_time sleep;
+  s_timespec sleep;
   cairo_surface_t *surface;
   u32 value_mask;
   u32 *value_list;
diff --git a/libkc3_window/window.c b/libkc3_window/window.c
index 76a1ba1..19f60e8 100644
--- a/libkc3_window/window.c
+++ b/libkc3_window/window.c
@@ -15,8 +15,8 @@
 
 bool window_animate (s_window *window)
 {
-  s_time clock_monotonic;
-  s_time delta;
+  s_timespec clock_monotonic;
+  s_timespec delta;
   s_sequence *seq;
   f64 t;
   if (clock_gettime(CLOCK_MONOTONIC, &clock_monotonic)) {