Imported Upstream version 0.7.5
[platform/upstream/libsolv.git] / ext / solv_xfopen.c
index 343aed8..9aab68b 100644 (file)
 #include <string.h>
 #include <fcntl.h>
 
+#ifdef _WIN32
+  #include "fmemopen.c"
+#endif
+
 #include "solv_xfopen.h"
 #include "util.h"
 
+#ifndef WITHOUT_COOKIEOPEN
 
 static FILE *cookieopen(void *cookie, const char *mode,
        ssize_t (*cread)(void *, char *, size_t),
@@ -602,6 +607,16 @@ static inline FILE *myzchunkfdopen(int fd, const char *mode)
 
 #endif /* ENABLE_ZCHUNK_COMPRESSION */
 
+#else
+/* no cookies no compression */
+#undef ENABLE_ZLIB_COMPRESSION
+#undef ENABLE_LZMA_COMPRESSION
+#undef ENABLE_BZIP2_COMPRESSION
+#undef ENABLE_ZSTD_COMPRESSION
+#undef ENABLE_ZCHUNK_COMPRESSION
+#endif
+
+
 
 FILE *
 solv_xfopen(const char *fn, const char *mode)
@@ -664,7 +679,15 @@ solv_xfopen_fd(const char *fn, int fd, const char *mode)
   suf = fn ? strrchr(fn, '.') : 0;
   if (!mode)
     {
+      #ifndef _WIN32
       int fl = fcntl(fd, F_GETFL, 0);
+      #else
+      HANDLE handle = (HANDLE) _get_osfhandle(fd);
+      BY_HANDLE_FILE_INFORMATION file_info;
+      if (!GetFileInformationByHandle(handle, &file_info))
+        return 0;
+      int fl = file_info.dwFileAttributes;
+      #endif
       if (fl == -1)
        return 0;
       fl &= O_RDONLY|O_WRONLY|O_RDWR;
@@ -759,6 +782,9 @@ solv_xfopen_iscompressed(const char *fn)
   return 0;
 }
 
+
+#ifndef WITHOUT_COOKIEOPEN
+
 struct bufcookie {
   char **bufp;
   size_t *buflp;
@@ -835,3 +861,33 @@ solv_xfopen_buf(const char *fn, char **bufp, size_t *buflp, const char *mode)
     }
   return fp;
 }
+
+#else
+
+FILE *
+solv_xfopen_buf(const char *fn, char **bufp, size_t *buflp, const char *mode)
+{
+  FILE *fp;
+  size_t l;
+  if (*mode != 'r')
+    return 0;
+  l = buflp ? *buflp : strlen(*bufp);
+  if (!strcmp(mode, "rf"))
+    {
+      if (!(fp = fmemopen(0, l, "r+")))
+       return 0;
+      if (l && fwrite(*bufp, l, 1, fp) != 1)
+       {
+         fclose(fp);
+         return 0;
+       }
+      solv_free(*bufp);
+      rewind(fp);
+    }
+  else
+    fp = fmemopen(*bufp, l, "r");
+  return fp;
+}
+
+#endif
+