Blame gio/glocalfileinputstream.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "glocalfileinputstream.h"
Packit ae235b
#include "glocalfileinfo.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#include "glib-unix.h"
Packit ae235b
#include "gfiledescriptorbased.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <io.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
struct _GLocalFileInputStreamPrivate {
Packit ae235b
  int fd;
Packit ae235b
  guint do_close : 1;
Packit ae235b
};
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static void       g_file_descriptor_based_iface_init   (GFileDescriptorBasedIface *iface);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
Packit ae235b
                         G_ADD_PRIVATE (GLocalFileInputStream)
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
Packit ae235b
						g_file_descriptor_based_iface_init))
Packit ae235b
#else
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
Packit ae235b
                         G_ADD_PRIVATE (GLocalFileInputStream))
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gssize     g_local_file_input_stream_read       (GInputStream      *stream,
Packit ae235b
							void              *buffer,
Packit ae235b
							gsize              count,
Packit ae235b
							GCancellable      *cancellable,
Packit ae235b
							GError           **error);
Packit ae235b
static gssize     g_local_file_input_stream_skip       (GInputStream      *stream,
Packit ae235b
							gsize              count,
Packit ae235b
							GCancellable      *cancellable,
Packit ae235b
							GError           **error);
Packit ae235b
static gboolean   g_local_file_input_stream_close      (GInputStream      *stream,
Packit ae235b
							GCancellable      *cancellable,
Packit ae235b
							GError           **error);
Packit ae235b
static goffset    g_local_file_input_stream_tell       (GFileInputStream  *stream);
Packit ae235b
static gboolean   g_local_file_input_stream_can_seek   (GFileInputStream  *stream);
Packit ae235b
static gboolean   g_local_file_input_stream_seek       (GFileInputStream  *stream,
Packit ae235b
							goffset            offset,
Packit ae235b
							GSeekType          type,
Packit ae235b
							GCancellable      *cancellable,
Packit ae235b
							GError           **error);
Packit ae235b
static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream  *stream,
Packit ae235b
							const char        *attributes,
Packit ae235b
							GCancellable      *cancellable,
Packit ae235b
							GError           **error);
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static int        g_local_file_input_stream_get_fd     (GFileDescriptorBased *stream);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
Packit ae235b
					  gboolean do_close)
Packit ae235b
{
Packit ae235b
  in->priv->do_close = do_close;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
Packit ae235b
{
Packit ae235b
  GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
Packit ae235b
  GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
Packit ae235b
Packit ae235b
  stream_class->read_fn = g_local_file_input_stream_read;
Packit ae235b
  stream_class->skip = g_local_file_input_stream_skip;
Packit ae235b
  stream_class->close_fn = g_local_file_input_stream_close;
Packit ae235b
  file_stream_class->tell = g_local_file_input_stream_tell;
Packit ae235b
  file_stream_class->can_seek = g_local_file_input_stream_can_seek;
Packit ae235b
  file_stream_class->seek = g_local_file_input_stream_seek;
Packit ae235b
  file_stream_class->query_info = g_local_file_input_stream_query_info;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static void
Packit ae235b
g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
Packit ae235b
{
Packit ae235b
  iface->get_fd = g_local_file_input_stream_get_fd;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_local_file_input_stream_init (GLocalFileInputStream *info)
Packit ae235b
{
Packit ae235b
  info->priv = g_local_file_input_stream_get_instance_private (info);
Packit ae235b
  info->priv->do_close = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
GFileInputStream *
Packit ae235b
_g_local_file_input_stream_new (int fd)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *stream;
Packit ae235b
Packit ae235b
  stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
Packit ae235b
  stream->priv->fd = fd;
Packit ae235b
  
Packit ae235b
  return G_FILE_INPUT_STREAM (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_local_file_input_stream_read (GInputStream  *stream,
Packit ae235b
				void          *buffer,
Packit ae235b
				gsize          count,
Packit ae235b
				GCancellable  *cancellable,
Packit ae235b
				GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *file;
Packit ae235b
  gssize res;
Packit ae235b
Packit ae235b
  file = G_LOCAL_FILE_INPUT_STREAM (stream);
Packit ae235b
Packit ae235b
  res = -1;
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      if (g_cancellable_set_error_if_cancelled (cancellable, error))
Packit ae235b
	break;
Packit ae235b
      res = read (file->priv->fd, buffer, count);
Packit ae235b
      if (res == -1)
Packit ae235b
	{
Packit ae235b
          int errsv = errno;
Packit ae235b
Packit ae235b
	  if (errsv == EINTR)
Packit ae235b
	    continue;
Packit ae235b
	  
Packit ae235b
	  g_set_error (error, G_IO_ERROR,
Packit ae235b
		       g_io_error_from_errno (errsv),
Packit ae235b
		       _("Error reading from file: %s"),
Packit ae235b
		       g_strerror (errsv));
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_local_file_input_stream_skip (GInputStream  *stream,
Packit ae235b
				gsize          count,
Packit ae235b
				GCancellable  *cancellable,
Packit ae235b
				GError       **error)
Packit ae235b
{
Packit ae235b
  off_t start, end;
Packit ae235b
  GLocalFileInputStream *file;
Packit ae235b
Packit ae235b
  file = G_LOCAL_FILE_INPUT_STREAM (stream);
Packit ae235b
  
Packit ae235b
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
Packit ae235b
    return -1;
Packit ae235b
  
Packit ae235b
  start = lseek (file->priv->fd, 0, SEEK_CUR);
Packit ae235b
  if (start == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
		   g_io_error_from_errno (errsv),
Packit ae235b
		   _("Error seeking in file: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  end = lseek (file->priv->fd, 0, SEEK_END);
Packit ae235b
  if (end == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
		   g_io_error_from_errno (errsv),
Packit ae235b
		   _("Error seeking in file: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (end - start > count)
Packit ae235b
    {
Packit ae235b
      end = lseek (file->priv->fd, count - (end - start), SEEK_CUR);
Packit ae235b
      if (end == -1)
Packit ae235b
	{
Packit ae235b
	  int errsv = errno;
Packit ae235b
Packit ae235b
	  g_set_error (error, G_IO_ERROR,
Packit ae235b
		       g_io_error_from_errno (errsv),
Packit ae235b
		       _("Error seeking in file: %s"),
Packit ae235b
		       g_strerror (errsv));
Packit ae235b
	  return -1;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return end - start;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_input_stream_close (GInputStream  *stream,
Packit ae235b
				 GCancellable  *cancellable,
Packit ae235b
				 GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *file;
Packit ae235b
Packit ae235b
  file = G_LOCAL_FILE_INPUT_STREAM (stream);
Packit ae235b
Packit ae235b
  if (!file->priv->do_close)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (file->priv->fd == -1)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (!g_close (file->priv->fd, NULL))
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
                   g_io_error_from_errno (errsv),
Packit ae235b
                   _("Error closing file: %s"),
Packit ae235b
                   g_strerror (errsv));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static goffset
Packit ae235b
g_local_file_input_stream_tell (GFileInputStream *stream)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *file;
Packit ae235b
  off_t pos;
Packit ae235b
Packit ae235b
  file = G_LOCAL_FILE_INPUT_STREAM (stream);
Packit ae235b
  
Packit ae235b
  pos = lseek (file->priv->fd, 0, SEEK_CUR);
Packit ae235b
Packit ae235b
  if (pos == (off_t)-1)
Packit ae235b
    return 0;
Packit ae235b
  
Packit ae235b
  return pos;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_input_stream_can_seek (GFileInputStream *stream)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *file;
Packit ae235b
  off_t pos;
Packit ae235b
Packit ae235b
  file = G_LOCAL_FILE_INPUT_STREAM (stream);
Packit ae235b
  
Packit ae235b
  pos = lseek (file->priv->fd, 0, SEEK_CUR);
Packit ae235b
Packit ae235b
  if (pos == (off_t)-1 && errno == ESPIPE)
Packit ae235b
    return FALSE;
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
seek_type_to_lseek (GSeekType type)
Packit ae235b
{
Packit ae235b
  switch (type)
Packit ae235b
    {
Packit ae235b
    default:
Packit ae235b
    case G_SEEK_CUR:
Packit ae235b
      return SEEK_CUR;
Packit ae235b
      
Packit ae235b
    case G_SEEK_SET:
Packit ae235b
      return SEEK_SET;
Packit ae235b
      
Packit ae235b
    case G_SEEK_END:
Packit ae235b
      return SEEK_END;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_input_stream_seek (GFileInputStream  *stream,
Packit ae235b
				goffset            offset,
Packit ae235b
				GSeekType          type,
Packit ae235b
				GCancellable      *cancellable,
Packit ae235b
				GError           **error)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *file;
Packit ae235b
  off_t pos;
Packit ae235b
Packit ae235b
  file = G_LOCAL_FILE_INPUT_STREAM (stream);
Packit ae235b
Packit ae235b
  pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
Packit ae235b
Packit ae235b
  if (pos == (off_t)-1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
		   g_io_error_from_errno (errsv),
Packit ae235b
		   _("Error seeking in file: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileInfo *
Packit ae235b
g_local_file_input_stream_query_info (GFileInputStream  *stream,
Packit ae235b
				      const char        *attributes,
Packit ae235b
				      GCancellable      *cancellable,
Packit ae235b
				      GError           **error)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *file;
Packit ae235b
Packit ae235b
  file = G_LOCAL_FILE_INPUT_STREAM (stream);
Packit ae235b
Packit ae235b
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
Packit ae235b
    return NULL;
Packit ae235b
  
Packit ae235b
  return _g_local_file_info_get_from_fd (file->priv->fd,
Packit ae235b
					 attributes,
Packit ae235b
					 error);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static int
Packit ae235b
g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
Packit ae235b
{
Packit ae235b
  GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
Packit ae235b
  return stream->priv->fd;
Packit ae235b
}
Packit ae235b
#endif