[kdbus] Use new API insted of direct call to org.freedesktop.DBus
[platform/upstream/glib.git] / gio / gwin32inputstream.c
index 5ede8d7..179e139 100644 (file)
@@ -13,9 +13,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Alexander Larsson <alexl@redhat.com>
  * Author: Tor Lillqvist <tml@iki.fi>
@@ -31,6 +29,7 @@
 #include "gioerror.h"
 #include "gsimpleasyncresult.h"
 #include "gwin32inputstream.h"
+#include "giowin32-priv.h"
 #include "gcancellable.h"
 #include "gasynchelper.h"
 #include "glibintl.h"
@@ -45,9 +44,9 @@
  * #GWin32InputStream implements #GInputStream for reading from a
  * Windows file handle.
  *
- * Note that <filename>&lt;gio/gwin32inputstream.h&gt;</filename> belongs
- * to the Windows-specific GIO interfaces, thus you have to use the
- * <filename>gio-windows-2.0.pc</filename> pkg-config file when using it.
+ * Note that `<gio/gwin32inputstream.h>` belongs to the Windows-specific GIO
+ * interfaces, thus you have to use the `gio-windows-2.0.pc` pkg-config file
+ * when using it.
  */
 
 enum {
@@ -59,6 +58,7 @@ enum {
 struct _GWin32InputStreamPrivate {
   HANDLE handle;
   gboolean close_handle;
+  gint fd;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GWin32InputStream, g_win32_input_stream, G_TYPE_INPUT_STREAM)
@@ -172,9 +172,10 @@ g_win32_input_stream_get_property (GObject    *object,
 static void
 g_win32_input_stream_init (GWin32InputStream *win32_stream)
 {
-  win32_stream->priv = g_win32_input_stream_get_private (win32_stream);
+  win32_stream->priv = g_win32_input_stream_get_instance_private (win32_stream);
   win32_stream->priv->handle = NULL;
   win32_stream->priv->close_handle = TRUE;
+  win32_stream->priv->fd = -1;
 }
 
 /**
@@ -239,7 +240,7 @@ g_win32_input_stream_set_close_handle (GWin32InputStream *stream,
  * Returns whether the handle of @stream will be
  * closed when the stream is closed.
  *
- * Return value: %TRUE if the handle is closed when done
+ * Returns: %TRUE if the handle is closed when done
  *
  * Since: 2.26
  */
@@ -257,7 +258,7 @@ g_win32_input_stream_get_close_handle (GWin32InputStream *stream)
  *
  * Return the Windows file handle that the stream reads from.
  *
- * Return value: The file handle of @stream
+ * Returns: The file handle of @stream
  *
  * Since: 2.26
  */
@@ -364,19 +365,44 @@ g_win32_input_stream_close (GInputStream  *stream,
   if (!win32_stream->priv->close_handle)
     return TRUE;
 
-  res = CloseHandle (win32_stream->priv->handle);
-  if (!res)
+  if (win32_stream->priv->fd != -1)
     {
-      int errsv = GetLastError ();
-      gchar *emsg = g_win32_error_message (errsv);
-
-      g_set_error (error, G_IO_ERROR,
-                  g_io_error_from_win32_error (errsv),
-                  _("Error closing handle: %s"),
-                  emsg);
-      g_free (emsg);
-      return FALSE;
+      if (close (win32_stream->priv->fd) < 0)
+       {
+         g_set_error_literal (error, G_IO_ERROR,
+                              g_io_error_from_errno (errno),
+                              g_strerror (errno));
+         return FALSE;
+       }
+    }
+  else
+    {
+      res = CloseHandle (win32_stream->priv->handle);
+      if (!res)
+       {
+         int errsv = GetLastError ();
+         gchar *emsg = g_win32_error_message (errsv);
+
+         g_set_error (error, G_IO_ERROR,
+                      g_io_error_from_win32_error (errsv),
+                      _("Error closing handle: %s"),
+                      emsg);
+         g_free (emsg);
+         return FALSE;
+       }
     }
 
   return TRUE;
 }
+
+GInputStream *
+g_win32_input_stream_new_from_fd (gint      fd,
+                                 gboolean  close_fd)
+{
+  GWin32InputStream *win32_stream;
+
+  win32_stream = G_WIN32_INPUT_STREAM (g_win32_input_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
+  win32_stream->priv->fd = fd;
+
+  return (GInputStream*)win32_stream;
+}