Remove non-existing parameter from docs
[platform/upstream/glib.git] / gio / glocalfileinputstream.c
index a1aa00b..544c568 100644 (file)
  * Author: Alexander Larsson <alexl@redhat.com>
  */
 
-#include <config.h>
+#include "config.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <errno.h>
 
 #include <glib.h>
 #include <glib/gstdio.h>
+#include "gcancellable.h"
 #include "gioerror.h"
 #include "glocalfileinputstream.h"
 #include "glocalfileinfo.h"
 #include "glibintl.h"
 
+#ifdef G_OS_WIN32
+#include <io.h>
+#endif
+
 #include "gioalias.h"
 
 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
@@ -42,6 +49,7 @@ G_DEFINE_TYPE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INP
 
 struct _GLocalFileInputStreamPrivate {
   int fd;
+  guint do_close : 1;
 };
 
 static gssize     g_local_file_input_stream_read       (GInputStream      *stream,
@@ -64,7 +72,7 @@ static gboolean   g_local_file_input_stream_seek       (GFileInputStream  *strea
                                                        GCancellable      *cancellable,
                                                        GError           **error);
 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream  *stream,
-                                                       char              *attributes,
+                                                       const char        *attributes,
                                                        GCancellable      *cancellable,
                                                        GError           **error);
 
@@ -74,9 +82,15 @@ g_local_file_input_stream_finalize (GObject *object)
   GLocalFileInputStream *file;
   
   file = G_LOCAL_FILE_INPUT_STREAM (object);
-  
-  if (G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize)
-    (*G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize) (object);
+
+  G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
+}
+
+void
+_g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
+                                         gboolean do_close)
+{
+  in->priv->do_close = do_close;
 }
 
 static void
@@ -90,9 +104,9 @@ g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
   
   gobject_class->finalize = g_local_file_input_stream_finalize;
 
-  stream_class->read = g_local_file_input_stream_read;
+  stream_class->read_fn = g_local_file_input_stream_read;
   stream_class->skip = g_local_file_input_stream_skip;
-  stream_class->close = g_local_file_input_stream_close;
+  stream_class->close_fn = g_local_file_input_stream_close;
   file_stream_class->tell = g_local_file_input_stream_tell;
   file_stream_class->can_seek = g_local_file_input_stream_can_seek;
   file_stream_class->seek = g_local_file_input_stream_seek;
@@ -105,6 +119,7 @@ g_local_file_input_stream_init (GLocalFileInputStream *info)
   info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
                                            G_TYPE_LOCAL_FILE_INPUT_STREAM,
                                            GLocalFileInputStreamPrivate);
+  info->priv->do_close = TRUE;
 }
 
 /**
@@ -144,13 +159,15 @@ g_local_file_input_stream_read (GInputStream  *stream,
       res = read (file->priv->fd, buffer, count);
       if (res == -1)
        {
-         if (errno == EINTR)
+          int errsv = errno;
+
+         if (errsv == EINTR)
            continue;
          
          g_set_error (error, G_IO_ERROR,
-                      g_io_error_from_errno (errno),
+                      g_io_error_from_errno (errsv),
                       _("Error reading from file: %s"),
-                      g_strerror (errno));
+                      g_strerror (errsv));
        }
       
       break;
@@ -176,20 +193,24 @@ g_local_file_input_stream_skip (GInputStream  *stream,
   start = lseek (file->priv->fd, 0, SEEK_CUR);
   if (start == -1)
     {
+      int errsv = errno;
+
       g_set_error (error, G_IO_ERROR,
-                  g_io_error_from_errno (errno),
+                  g_io_error_from_errno (errsv),
                   _("Error seeking in file: %s"),
-                  g_strerror (errno));
+                  g_strerror (errsv));
       return -1;
     }
   
   res = lseek (file->priv->fd, count, SEEK_CUR);
   if (res == -1)
     {
+      int errsv = errno;
+
       g_set_error (error, G_IO_ERROR,
-                  g_io_error_from_errno (errno),
+                  g_io_error_from_errno (errsv),
                   _("Error seeking in file: %s"),
-                  g_strerror (errno));
+                  g_strerror (errsv));
       return -1;
     }
 
@@ -206,6 +227,9 @@ g_local_file_input_stream_close (GInputStream  *stream,
 
   file = G_LOCAL_FILE_INPUT_STREAM (stream);
 
+  if (!file->priv->do_close)
+    return TRUE;
+
   if (file->priv->fd == -1)
     return TRUE;
 
@@ -213,10 +237,14 @@ g_local_file_input_stream_close (GInputStream  *stream,
     {
       res = close (file->priv->fd);
       if (res == -1)
-        g_set_error (error, G_IO_ERROR,
-                     g_io_error_from_errno (errno),
-                     _("Error closing file: %s"),
-                     g_strerror (errno));
+        {
+          int errsv = errno;
+
+          g_set_error (error, G_IO_ERROR,
+                       g_io_error_from_errno (errsv),
+                       _("Error closing file: %s"),
+                       g_strerror (errsv));
+        }
       break;
     }
 
@@ -250,8 +278,7 @@ g_local_file_input_stream_can_seek (GFileInputStream *stream)
   
   pos = lseek (file->priv->fd, 0, SEEK_CUR);
 
-  if (pos == (off_t)-1 &&
-      errno == ESPIPE)
+  if (pos == (off_t)-1 && errno == ESPIPE)
     return FALSE;
   
   return TRUE;
@@ -290,10 +317,12 @@ g_local_file_input_stream_seek (GFileInputStream  *stream,
 
   if (pos == (off_t)-1)
     {
+      int errsv = errno;
+
       g_set_error (error, G_IO_ERROR,
-                  g_io_error_from_errno (errno),
+                  g_io_error_from_errno (errsv),
                   _("Error seeking in file: %s"),
-                  g_strerror (errno));
+                  g_strerror (errsv));
       return FALSE;
     }
   
@@ -302,7 +331,7 @@ g_local_file_input_stream_seek (GFileInputStream  *stream,
 
 static GFileInfo *
 g_local_file_input_stream_query_info (GFileInputStream  *stream,
-                                     char              *attributes,
+                                     const char        *attributes,
                                      GCancellable      *cancellable,
                                      GError           **error)
 {