Changeset: b90cb314931c for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=b90cb314931c
Modified Files:
        common/stream/stdio_stream.c
        common/stream/stream.c
        common/stream/stream_internal.h
Branch: Oct2020
Log Message:

A couple of fixes.
- no unused functions (use ifdef)
- fix memory leak in case of error
- remove file after failed create


diffs (129 lines):

diff --git a/common/stream/stdio_stream.c b/common/stream/stdio_stream.c
--- a/common/stream/stdio_stream.c
+++ b/common/stream/stdio_stream.c
@@ -165,6 +165,7 @@ file_fsetpos(stream *restrict s, fpos_t 
 
 /* convert a string from UTF-8 to wide characters; the return value is
  * freshly allocated */
+#ifdef HAVE__WFOPEN
 static wchar_t *
 utf8towchar(const char *src)
 {
@@ -246,6 +247,8 @@ utf8towchar(const char *src)
        return dest;
 }
 
+#else
+
 static char *
 cvfilename(const char *filename)
 {
@@ -281,6 +284,7 @@ cvfilename(const char *filename)
         * locale's encoding is not UTF-8) */
        return strdup(filename);
 }
+#endif
 
 
 stream *
@@ -297,7 +301,6 @@ open_stream(const char *restrict filenam
        {
                wchar_t *wfname = utf8towchar(filename);
                wchar_t *wflags = utf8towchar(flags);
-               (void)cvfilename;
                if (wfname != NULL && wflags != NULL)
                        fp = _wfopen(wfname, wflags);
                else
@@ -310,7 +313,6 @@ open_stream(const char *restrict filenam
 #else
        {
                char *fname = cvfilename(filename);
-               (void)utf8towchar;
                if (fname) {
                        fp = fopen(fname, flags);
                        free(fname);
@@ -498,3 +500,24 @@ getFileSize(stream *s)
                return (size_t) stb.st_size;
        return 0;               /* unknown */
 }
+
+int
+file_remove(const char *filename)
+{
+       int rc = -1;
+
+#ifdef HAVE__WFOPEN
+       wchar_t *wfname = utf8towchar(filename);
+       if (wfname != NULL) {
+               rc = _wremove(wfname);
+               free(wfname);
+       }
+#else
+       char *fname = cvfilename(filename);
+       if (fname) {
+               rc = remove(fname);
+               free(fname);
+       }
+#endif
+       return rc;
+}
diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -851,7 +851,7 @@ open_rstream(const char *filename)
 
        stream *c = compressed_stream(s, 0);
        if (c == NULL)
-               mnstr_close(s);
+               mnstr_destroy(s);
 
        return c;
 }
@@ -870,8 +870,10 @@ open_wstream(const char *filename)
                return NULL;
 
        stream *c = compressed_stream(s, 0);
-       if (c == NULL)
-               mnstr_close(s);
+       if (c == NULL) {
+               mnstr_destroy(s);
+               file_remove(filename);
+       }
 
        return c;
 }
@@ -890,7 +892,7 @@ open_rastream(const char *filename)
 
        stream *t = create_text_stream(s);
        if (t == NULL)
-               mnstr_close(s);
+               mnstr_destroy(s);
 
        return t;
 }
@@ -908,8 +910,10 @@ open_wastream(const char *filename)
                return NULL;
 
        stream *t = create_text_stream(s);
-       if (t == NULL)
-               mnstr_close(s);
+       if (t == NULL) {
+               mnstr_destroy(s);
+               file_remove(filename);
+       }
 
        return t;
 }
diff --git a/common/stream/stream_internal.h b/common/stream/stream_internal.h
--- a/common/stream/stream_internal.h
+++ b/common/stream/stream_internal.h
@@ -207,6 +207,9 @@ stream *open_stream(const char *restrict
 stream *file_stream(const char *name)
        __attribute__((__visibility__("hidden")));
 
+int file_remove(const char *filename)
+       __attribute__((__visibility__("hidden")));
+
 /* implementation detail of stdio_stream.c which must be public because
  * for example bstream() special cases on it to provide a fast path for file
  * i/o.
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to