glocalfileinputstream.c 9.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23 24 25 26 27 28 29

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <glib.h>
#include <glib/gstdio.h>
30
#include "gcancellable.h"
31 32 33 34 35
#include "gioerror.h"
#include "glocalfileinputstream.h"
#include "glocalfileinfo.h"
#include "glibintl.h"

36
#ifdef G_OS_UNIX
37
#include <unistd.h>
Colin Walters's avatar
Colin Walters committed
38
#include "glib-unix.h"
39 40 41
#include "gfiledescriptorbased.h"
#endif

42 43 44 45
#ifdef G_OS_WIN32
#include <io.h>
#endif

46 47 48 49
struct _GLocalFileInputStreamPrivate {
  int fd;
  guint do_close : 1;
};
50

51
#ifdef G_OS_UNIX
52
static void       g_file_descriptor_based_iface_init   (GFileDescriptorBasedIface *iface);
53 54
#endif

55
#define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
56
#ifdef G_OS_UNIX
57
G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
58
                         G_ADD_PRIVATE (GLocalFileInputStream)
59
			 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
60
						g_file_descriptor_based_iface_init))
61
#else
62 63
G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
                         G_ADD_PRIVATE (GLocalFileInputStream))
64
#endif
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

static gssize     g_local_file_input_stream_read       (GInputStream      *stream,
							void              *buffer,
							gsize              count,
							GCancellable      *cancellable,
							GError           **error);
static gssize     g_local_file_input_stream_skip       (GInputStream      *stream,
							gsize              count,
							GCancellable      *cancellable,
							GError           **error);
static gboolean   g_local_file_input_stream_close      (GInputStream      *stream,
							GCancellable      *cancellable,
							GError           **error);
static goffset    g_local_file_input_stream_tell       (GFileInputStream  *stream);
static gboolean   g_local_file_input_stream_can_seek   (GFileInputStream  *stream);
static gboolean   g_local_file_input_stream_seek       (GFileInputStream  *stream,
							goffset            offset,
							GSeekType          type,
							GCancellable      *cancellable,
							GError           **error);
static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream  *stream,
86
							const char        *attributes,
87 88
							GCancellable      *cancellable,
							GError           **error);
89
#ifdef G_OS_UNIX
90
static int        g_local_file_input_stream_get_fd     (GFileDescriptorBased *stream);
91
#endif
92

93 94 95 96 97 98 99
void
_g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
					  gboolean do_close)
{
  in->priv->do_close = do_close;
}

100 101 102 103 104 105
static void
g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
{
  GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
  GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);

106
  stream_class->read_fn = g_local_file_input_stream_read;
107
  stream_class->skip = g_local_file_input_stream_skip;
108
  stream_class->close_fn = g_local_file_input_stream_close;
109 110 111 112 113 114
  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;
  file_stream_class->query_info = g_local_file_input_stream_query_info;
}

115
#ifdef G_OS_UNIX
116 117 118 119 120
static void
g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
{
  iface->get_fd = g_local_file_input_stream_get_fd;
}
121
#endif
122

123 124 125
static void
g_local_file_input_stream_init (GLocalFileInputStream *info)
{
126
  info->priv = g_local_file_input_stream_get_instance_private (info);
127
  info->priv->do_close = TRUE;
128 129 130
}

GFileInputStream *
131
_g_local_file_input_stream_new (int fd)
132 133 134 135 136 137 138 139 140 141
{
  GLocalFileInputStream *stream;

  stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
  stream->priv->fd = fd;
  
  return G_FILE_INPUT_STREAM (stream);
}

static gssize
142 143 144 145 146
g_local_file_input_stream_read (GInputStream  *stream,
				void          *buffer,
				gsize          count,
				GCancellable  *cancellable,
				GError       **error)
147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
  GLocalFileInputStream *file;
  gssize res;

  file = G_LOCAL_FILE_INPUT_STREAM (stream);

  res = -1;
  while (1)
    {
      if (g_cancellable_set_error_if_cancelled (cancellable, error))
	break;
      res = read (file->priv->fd, buffer, count);
      if (res == -1)
	{
161 162 163
          int errsv = errno;

	  if (errsv == EINTR)
164 165 166
	    continue;
	  
	  g_set_error (error, G_IO_ERROR,
167
		       g_io_error_from_errno (errsv),
168
		       _("Error reading from file: %s"),
169
		       g_strerror (errsv));
170 171 172 173 174 175 176 177 178
	}
      
      break;
    }
  
  return res;
}

static gssize
179 180 181 182
g_local_file_input_stream_skip (GInputStream  *stream,
				gsize          count,
				GCancellable  *cancellable,
				GError       **error)
183
{
184
  off_t start, end;
185 186 187 188 189 190 191 192 193 194
  GLocalFileInputStream *file;

  file = G_LOCAL_FILE_INPUT_STREAM (stream);
  
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return -1;
  
  start = lseek (file->priv->fd, 0, SEEK_CUR);
  if (start == -1)
    {
195 196
      int errsv = errno;

197
      g_set_error (error, G_IO_ERROR,
198
		   g_io_error_from_errno (errsv),
199
		   _("Error seeking in file: %s"),
200
		   g_strerror (errsv));
201 202 203
      return -1;
    }
  
204 205
  end = lseek (file->priv->fd, 0, SEEK_END);
  if (end == -1)
206
    {
207 208
      int errsv = errno;

209
      g_set_error (error, G_IO_ERROR,
210
		   g_io_error_from_errno (errsv),
211
		   _("Error seeking in file: %s"),
212
		   g_strerror (errsv));
213 214 215
      return -1;
    }

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  if (end - start > count)
    {
      end = lseek (file->priv->fd, count - (end - start), SEEK_CUR);
      if (end == -1)
	{
	  int errsv = errno;

	  g_set_error (error, G_IO_ERROR,
		       g_io_error_from_errno (errsv),
		       _("Error seeking in file: %s"),
		       g_strerror (errsv));
	  return -1;
	}
    }

  return end - start;
232 233 234
}

static gboolean
235 236 237
g_local_file_input_stream_close (GInputStream  *stream,
				 GCancellable  *cancellable,
				 GError       **error)
238 239 240 241 242
{
  GLocalFileInputStream *file;

  file = G_LOCAL_FILE_INPUT_STREAM (stream);

243 244 245
  if (!file->priv->do_close)
    return TRUE;

246 247 248
  if (file->priv->fd == -1)
    return TRUE;

Colin Walters's avatar
Colin Walters committed
249
  if (!g_close (file->priv->fd, NULL))
250
    {
Colin Walters's avatar
Colin Walters committed
251 252 253 254 255 256 257
      int errsv = errno;
      
      g_set_error (error, G_IO_ERROR,
                   g_io_error_from_errno (errsv),
                   _("Error closing file: %s"),
                   g_strerror (errsv));
      return FALSE;
258 259
    }

Colin Walters's avatar
Colin Walters committed
260
  return TRUE;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}


static goffset
g_local_file_input_stream_tell (GFileInputStream *stream)
{
  GLocalFileInputStream *file;
  off_t pos;

  file = G_LOCAL_FILE_INPUT_STREAM (stream);
  
  pos = lseek (file->priv->fd, 0, SEEK_CUR);

  if (pos == (off_t)-1)
    return 0;
  
  return pos;
}

static gboolean
g_local_file_input_stream_can_seek (GFileInputStream *stream)
{
  GLocalFileInputStream *file;
  off_t pos;

  file = G_LOCAL_FILE_INPUT_STREAM (stream);
  
  pos = lseek (file->priv->fd, 0, SEEK_CUR);

Matthias Clasen's avatar
Matthias Clasen committed
290
  if (pos == (off_t)-1 && errno == ESPIPE)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    return FALSE;
  
  return TRUE;
}

static int
seek_type_to_lseek (GSeekType type)
{
  switch (type)
    {
    default:
    case G_SEEK_CUR:
      return SEEK_CUR;
      
    case G_SEEK_SET:
      return SEEK_SET;
      
    case G_SEEK_END:
      return SEEK_END;
    }
}

static gboolean
314 315 316 317 318
g_local_file_input_stream_seek (GFileInputStream  *stream,
				goffset            offset,
				GSeekType          type,
				GCancellable      *cancellable,
				GError           **error)
319 320 321 322 323 324 325 326 327 328
{
  GLocalFileInputStream *file;
  off_t pos;

  file = G_LOCAL_FILE_INPUT_STREAM (stream);

  pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));

  if (pos == (off_t)-1)
    {
329 330
      int errsv = errno;

331
      g_set_error (error, G_IO_ERROR,
332
		   g_io_error_from_errno (errsv),
333
		   _("Error seeking in file: %s"),
334
		   g_strerror (errsv));
335 336 337 338 339 340 341
      return FALSE;
    }
  
  return TRUE;
}

static GFileInfo *
342
g_local_file_input_stream_query_info (GFileInputStream  *stream,
343
				      const char        *attributes,
344 345
				      GCancellable      *cancellable,
				      GError           **error)
346 347 348 349 350 351 352 353 354 355 356 357
{
  GLocalFileInputStream *file;

  file = G_LOCAL_FILE_INPUT_STREAM (stream);

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return NULL;
  
  return _g_local_file_info_get_from_fd (file->priv->fd,
					 attributes,
					 error);
}
358

359
#ifdef G_OS_UNIX
360 361 362 363 364 365
static int
g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
{
  GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
  return stream->priv->fd;
}
366
#endif