Commit 2870f94be1fcb6a65d8b9f75dda0a8614b20d224

Thomas de Grivel 2024-09-03T19:41:27

fix file server

diff --git a/http/http_response.c b/http/http_response.c
index 1e8a8b0..6547a69 100644
--- a/http/http_response.c
+++ b/http/http_response.c
@@ -10,7 +10,9 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <errno.h>
 #include <string.h>
+#include <unistd.h>
 #include <libkc3/kc3.h>
 #include "http_response.h"
 
@@ -95,6 +97,7 @@ sw http_response_buf_write (const s_http_response *response,
   sw    content_length = -1;
   s_str content_length_str = {0};
   s_tag default_messages = {0};
+  s32 e;
   s_ident ident = {0};
   s_buf *in;
   s_tag *key = NULL;
@@ -105,8 +108,10 @@ sw http_response_buf_write (const s_http_response *response,
   s_str str;
   s_tag tag_code = {0};
   s_tag tag_message = {0};
+  s_buf tmp;
   const s_sym *type;
   s_tag *value = NULL;
+  sw w = 0;
   assert(response);
   assert(buf);
   if (! response->protocol.size)
@@ -224,6 +229,7 @@ sw http_response_buf_write (const s_http_response *response,
     if (type == &g_sym_Str) {
       if ((r = buf_write_str(buf, &response->body.data.str)) < 0)
         return r;
+      result += r;
     }
     else if (type == &g_sym_Buf) {
       in = response->body.data.struct_.data;
@@ -234,16 +240,40 @@ sw http_response_buf_write (const s_http_response *response,
         err_inspect_str(&str);
         if ((r = buf_write(buf, str.ptr.pchar, str.size)) <= 0)
           return r;
+        result += r;
         str_clean(&str);
       }
     }
+    else if (type == &g_sym_S32) {
+      buf_init_alloc(&tmp, BUF_SIZE);
+      while ((r = read(response->body.data.s32,
+                       tmp.ptr.p, tmp.size)) > 0) {
+        tmp.rpos = 0;
+        tmp.wpos = r;
+        while (tmp.rpos < (uw) r) {
+          if ((w = buf_write(buf, tmp.ptr.ps8 + tmp.rpos,
+                             r - tmp.rpos)) <= 0)
+            return w;
+          result += w;
+          tmp.rpos += w;
+        }
+      }
+      if (r < 0) {
+        e = errno;
+        err_write_1("http_response_buf_write: ");
+        err_inspect_s32(&response->body.data.s32);
+        err_write_1(": ");
+        err_puts(strerror(e));
+        return r;
+      }
+      close(response->body.data.s32);
+    }
     else {
       err_write_1("http_response_buf_write: unknown body type: ");
       err_inspect_sym(&type);
       err_write_1("\n");
       return -1;
     }
-    result += r;
   }
   return result;
 }
diff --git a/lib/kc3/0.1/file.kc3 b/lib/kc3/0.1/file.kc3
index a35830f..bb37b40 100644
--- a/lib/kc3/0.1/file.kc3
+++ b/lib/kc3/0.1/file.kc3
@@ -17,9 +17,7 @@ defmodule File do
 
   def list = cfn List "file_list" (Str, Result)
 
-  def open_r = cfn Buf "file_open_r" (Str, Result)
-
-  def open_w = cfn Buf "file_open_w" (Str, Result)
+  def open_r = cfn S32 "file_open_r" (Str, Result)
 
   def stat = cfn File.Stat "file_stat" (Str, Result)
 
diff --git a/libkc3/file.c b/libkc3/file.c
index afc5e7d..f4bbb27 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -254,11 +254,10 @@ FILE * file_open (const char *path, const char *mode)
   return fp;
 }
 
-s_buf * file_open_r (const s_str *path, s_buf *dest)
+s32 * file_open_r (const s_str *path, s32 *dest)
 {
   sw e;
-  sw fd;
-  s_buf tmp;
+  s32 fd;
   assert(path);
   assert(dest);
   if ((fd = open(path->ptr.pchar, O_RDONLY | O_BINARY)) < 0) {
@@ -269,59 +268,7 @@ s_buf * file_open_r (const s_str *path, s_buf *dest)
     err_puts(strerror(e));
     return NULL;
   }
-  if (! buf_init_alloc(&tmp, BUF_SIZE)) {
-    close(fd);
-    return NULL;
-  }
-  if (! buf_fd_open_r(&tmp, fd)) {
-    buf_clean(&tmp);
-    close(fd);
-    return NULL;
-  }
-  *dest = tmp;
-  return dest;
-}
-
-s_buf_rw * file_open_rw (const s_str *path, s_buf_rw *dest)
-{
-  s_buf_rw tmp = {0};
-  assert(path);
-  assert(dest);
-  tmp.r = alloc(sizeof(s_buf));
-  if (! file_open_r(path, tmp.r)) {
-    free(tmp.r);
-    return NULL;
-  }
-  tmp.w = alloc(sizeof(s_buf));
-  if (! file_open_w(path, tmp.w)) {
-    free(tmp.w);
-    buf_file_close(tmp.r);
-    free(tmp.r);
-    return NULL;
-  }
-  *dest = tmp;
-  return dest;
-}
-
-s_buf * file_open_w (const s_str *path, s_buf *dest)
-{
-  FILE *fp;
-  s_buf tmp;
-  assert(path);
-  assert(dest);
-  fp = file_open(path->ptr.pchar, "wb");
-  if (! fp)
-    return NULL;
-  if (! buf_init_alloc(&tmp, BUF_SIZE)) {
-    fclose(fp);
-    return NULL;
-  }
-  if (! buf_file_open_w(&tmp, fp)) {
-    buf_clean(&tmp);
-    fclose(fp);
-    return NULL;
-  }
-  *dest = tmp;
+  *dest = fd;
   return dest;
 }
 
diff --git a/libkc3/file.h b/libkc3/file.h
index 33678c4..2069e0d 100644
--- a/libkc3/file.h
+++ b/libkc3/file.h
@@ -38,8 +38,6 @@ s_file_stat * file_stat (const s_str *path, s_file_stat *dest);
 /* Operators. */
 s_str *    file_pwd (s_str *dest);
 FILE *     file_open (const char *path, const char *mode);
-s_buf *    file_open_r (const s_str *path, s_buf *dest);
-s_buf_rw * file_open_rw (const s_str *path, s_buf_rw *dest);
-s_buf *    file_open_w (const s_str *path, s_buf *dest);
+s32 *      file_open_r (const s_str *path, s32 *dest);
 
 #endif /* LIBKC3_FILE_H */