LCOV - code coverage report
Current view: top level - glib/gio - glocalfileinputstream.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 68 88 77.3 %
Date: 2024-04-16 05:15:53 Functions: 17 17 100.0 %
Branches: 17 31 54.8 %

           Branch data     Line data    Source code
       1                 :            : /* GIO - GLib Input, Output and Streaming Library
       2                 :            :  * 
       3                 :            :  * Copyright (C) 2006-2007 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Author: Alexander Larsson <alexl@redhat.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #include <sys/types.h>
      26                 :            : #include <sys/stat.h>
      27                 :            : #include <fcntl.h>
      28                 :            : #include <errno.h>
      29                 :            : 
      30                 :            : #include <glib.h>
      31                 :            : #include <glib/gstdio.h>
      32                 :            : #include "gcancellable.h"
      33                 :            : #include "gioerror.h"
      34                 :            : #include "glocalfileinputstream.h"
      35                 :            : #include "glocalfileinfo.h"
      36                 :            : #include "glibintl.h"
      37                 :            : 
      38                 :            : #ifdef G_OS_UNIX
      39                 :            : #include <unistd.h>
      40                 :            : #include "glib-unix.h"
      41                 :            : #include "gfiledescriptorbased.h"
      42                 :            : #endif
      43                 :            : 
      44                 :            : #ifdef G_OS_WIN32
      45                 :            : #include <io.h>
      46                 :            : #endif
      47                 :            : 
      48                 :            : struct _GLocalFileInputStreamPrivate {
      49                 :            :   int fd;
      50                 :            :   guint do_close : 1;
      51                 :            : };
      52                 :            : 
      53                 :            : #ifdef G_OS_UNIX
      54                 :            : static void       g_file_descriptor_based_iface_init   (GFileDescriptorBasedIface *iface);
      55                 :            : #endif
      56                 :            : 
      57                 :            : #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
      58                 :            : #ifdef G_OS_UNIX
      59   [ +  +  +  -  :       1109 : G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
                   +  + ]
      60                 :            :                          G_ADD_PRIVATE (GLocalFileInputStream)
      61                 :            :                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
      62                 :            :                                                 g_file_descriptor_based_iface_init))
      63                 :            : #else
      64                 :            : G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
      65                 :            :                          G_ADD_PRIVATE (GLocalFileInputStream))
      66                 :            : #endif
      67                 :            : 
      68                 :            : static gssize     g_local_file_input_stream_read       (GInputStream      *stream,
      69                 :            :                                                         void              *buffer,
      70                 :            :                                                         gsize              count,
      71                 :            :                                                         GCancellable      *cancellable,
      72                 :            :                                                         GError           **error);
      73                 :            : static gboolean   g_local_file_input_stream_close      (GInputStream      *stream,
      74                 :            :                                                         GCancellable      *cancellable,
      75                 :            :                                                         GError           **error);
      76                 :            : static goffset    g_local_file_input_stream_tell       (GFileInputStream  *stream);
      77                 :            : static gboolean   g_local_file_input_stream_can_seek   (GFileInputStream  *stream);
      78                 :            : static gboolean   g_local_file_input_stream_seek       (GFileInputStream  *stream,
      79                 :            :                                                         goffset            offset,
      80                 :            :                                                         GSeekType          type,
      81                 :            :                                                         GCancellable      *cancellable,
      82                 :            :                                                         GError           **error);
      83                 :            : static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream  *stream,
      84                 :            :                                                         const char        *attributes,
      85                 :            :                                                         GCancellable      *cancellable,
      86                 :            :                                                         GError           **error);
      87                 :            : #ifdef G_OS_UNIX
      88                 :            : static int        g_local_file_input_stream_get_fd     (GFileDescriptorBased *stream);
      89                 :            : #endif
      90                 :            : 
      91                 :            : void
      92                 :        108 : _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
      93                 :            :                                           gboolean do_close)
      94                 :            : {
      95                 :        108 :   in->priv->do_close = do_close;
      96                 :        108 : }
      97                 :            : 
      98                 :            : static void
      99                 :         15 : g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
     100                 :            : {
     101                 :         15 :   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
     102                 :         15 :   GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
     103                 :            : 
     104                 :         15 :   stream_class->read_fn = g_local_file_input_stream_read;
     105                 :         15 :   stream_class->close_fn = g_local_file_input_stream_close;
     106                 :         15 :   file_stream_class->tell = g_local_file_input_stream_tell;
     107                 :         15 :   file_stream_class->can_seek = g_local_file_input_stream_can_seek;
     108                 :         15 :   file_stream_class->seek = g_local_file_input_stream_seek;
     109                 :         15 :   file_stream_class->query_info = g_local_file_input_stream_query_info;
     110                 :         15 : }
     111                 :            : 
     112                 :            : #ifdef G_OS_UNIX
     113                 :            : static void
     114                 :         15 : g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
     115                 :            : {
     116                 :         15 :   iface->get_fd = g_local_file_input_stream_get_fd;
     117                 :         15 : }
     118                 :            : #endif
     119                 :            : 
     120                 :            : static void
     121                 :        227 : g_local_file_input_stream_init (GLocalFileInputStream *info)
     122                 :            : {
     123                 :        227 :   info->priv = g_local_file_input_stream_get_instance_private (info);
     124                 :        227 :   info->priv->do_close = TRUE;
     125                 :        227 : }
     126                 :            : 
     127                 :            : GFileInputStream *
     128                 :        227 : _g_local_file_input_stream_new (int fd)
     129                 :            : {
     130                 :            :   GLocalFileInputStream *stream;
     131                 :            : 
     132                 :        227 :   stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
     133                 :        227 :   stream->priv->fd = fd;
     134                 :            :   
     135                 :        227 :   return G_FILE_INPUT_STREAM (stream);
     136                 :            : }
     137                 :            : 
     138                 :            : static gssize
     139                 :         97 : g_local_file_input_stream_read (GInputStream  *stream,
     140                 :            :                                 void          *buffer,
     141                 :            :                                 gsize          count,
     142                 :            :                                 GCancellable  *cancellable,
     143                 :            :                                 GError       **error)
     144                 :            : {
     145                 :            :   GLocalFileInputStream *file;
     146                 :            :   gssize res;
     147                 :            : 
     148                 :         97 :   file = G_LOCAL_FILE_INPUT_STREAM (stream);
     149                 :            : 
     150                 :         97 :   res = -1;
     151                 :            :   while (1)
     152                 :            :     {
     153         [ -  + ]:         97 :       if (g_cancellable_set_error_if_cancelled (cancellable, error))
     154                 :          0 :         break;
     155                 :         97 :       res = read (file->priv->fd, buffer, count);
     156         [ -  + ]:         97 :       if (res == -1)
     157                 :            :         {
     158                 :          0 :           int errsv = errno;
     159                 :            : 
     160         [ #  # ]:          0 :           if (errsv == EINTR)
     161                 :          0 :             continue;
     162                 :            :           
     163                 :          0 :           g_set_error (error, G_IO_ERROR,
     164                 :          0 :                        g_io_error_from_errno (errsv),
     165                 :            :                        _("Error reading from file: %s"),
     166                 :            :                        g_strerror (errsv));
     167                 :            :         }
     168                 :            :       
     169                 :         97 :       break;
     170                 :            :     }
     171                 :            :   
     172                 :         97 :   return res;
     173                 :            : }
     174                 :            : 
     175                 :            : static gboolean
     176                 :        227 : g_local_file_input_stream_close (GInputStream  *stream,
     177                 :            :                                  GCancellable  *cancellable,
     178                 :            :                                  GError       **error)
     179                 :            : {
     180                 :            :   GLocalFileInputStream *file;
     181                 :            : 
     182                 :        227 :   file = G_LOCAL_FILE_INPUT_STREAM (stream);
     183                 :            : 
     184         [ +  + ]:        227 :   if (!file->priv->do_close)
     185                 :        108 :     return TRUE;
     186                 :            : 
     187         [ -  + ]:        119 :   if (file->priv->fd == -1)
     188                 :          0 :     return TRUE;
     189                 :            : 
     190         [ -  + ]:        119 :   if (!g_close (file->priv->fd, NULL))
     191                 :            :     {
     192                 :          0 :       int errsv = errno;
     193                 :            :       
     194                 :          0 :       g_set_error (error, G_IO_ERROR,
     195                 :          0 :                    g_io_error_from_errno (errsv),
     196                 :            :                    _("Error closing file: %s"),
     197                 :            :                    g_strerror (errsv));
     198                 :          0 :       return FALSE;
     199                 :            :     }
     200                 :            : 
     201                 :        119 :   return TRUE;
     202                 :            : }
     203                 :            : 
     204                 :            : 
     205                 :            : static goffset
     206                 :         49 : g_local_file_input_stream_tell (GFileInputStream *stream)
     207                 :            : {
     208                 :            :   GLocalFileInputStream *file;
     209                 :            :   off_t pos;
     210                 :            : 
     211                 :         49 :   file = G_LOCAL_FILE_INPUT_STREAM (stream);
     212                 :            :   
     213                 :         49 :   pos = lseek (file->priv->fd, 0, SEEK_CUR);
     214                 :            : 
     215         [ -  + ]:         49 :   if (pos == (off_t)-1)
     216                 :          0 :     return 0;
     217                 :            :   
     218                 :         49 :   return pos;
     219                 :            : }
     220                 :            : 
     221                 :            : static gboolean
     222                 :          8 : g_local_file_input_stream_can_seek (GFileInputStream *stream)
     223                 :            : {
     224                 :            :   GLocalFileInputStream *file;
     225                 :            :   off_t pos;
     226                 :            : 
     227                 :          8 :   file = G_LOCAL_FILE_INPUT_STREAM (stream);
     228                 :            :   
     229                 :          8 :   pos = lseek (file->priv->fd, 0, SEEK_CUR);
     230                 :            : 
     231   [ -  +  -  - ]:          8 :   if (pos == (off_t)-1 && errno == ESPIPE)
     232                 :          0 :     return FALSE;
     233                 :            :   
     234                 :          8 :   return TRUE;
     235                 :            : }
     236                 :            : 
     237                 :            : static int
     238                 :         13 : seek_type_to_lseek (GSeekType type)
     239                 :            : {
     240      [ -  +  + ]:         13 :   switch (type)
     241                 :            :     {
     242                 :          0 :     default:
     243                 :            :     case G_SEEK_CUR:
     244                 :          0 :       return SEEK_CUR;
     245                 :            :       
     246                 :          5 :     case G_SEEK_SET:
     247                 :          5 :       return SEEK_SET;
     248                 :            :       
     249                 :          8 :     case G_SEEK_END:
     250                 :          8 :       return SEEK_END;
     251                 :            :     }
     252                 :            : }
     253                 :            : 
     254                 :            : static gboolean
     255                 :         13 : g_local_file_input_stream_seek (GFileInputStream  *stream,
     256                 :            :                                 goffset            offset,
     257                 :            :                                 GSeekType          type,
     258                 :            :                                 GCancellable      *cancellable,
     259                 :            :                                 GError           **error)
     260                 :            : {
     261                 :            :   GLocalFileInputStream *file;
     262                 :            :   off_t pos;
     263                 :            : 
     264                 :         13 :   file = G_LOCAL_FILE_INPUT_STREAM (stream);
     265                 :            : 
     266                 :         13 :   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
     267                 :            : 
     268         [ -  + ]:         13 :   if (pos == (off_t)-1)
     269                 :            :     {
     270                 :          0 :       int errsv = errno;
     271                 :            : 
     272                 :          0 :       g_set_error (error, G_IO_ERROR,
     273                 :          0 :                    g_io_error_from_errno (errsv),
     274                 :            :                    _("Error seeking in file: %s"),
     275                 :            :                    g_strerror (errsv));
     276                 :          0 :       return FALSE;
     277                 :            :     }
     278                 :            :   
     279                 :         13 :   return TRUE;
     280                 :            : }
     281                 :            : 
     282                 :            : static GFileInfo *
     283                 :         67 : g_local_file_input_stream_query_info (GFileInputStream  *stream,
     284                 :            :                                       const char        *attributes,
     285                 :            :                                       GCancellable      *cancellable,
     286                 :            :                                       GError           **error)
     287                 :            : {
     288                 :            :   GLocalFileInputStream *file;
     289                 :            : 
     290                 :         67 :   file = G_LOCAL_FILE_INPUT_STREAM (stream);
     291                 :            : 
     292         [ -  + ]:         67 :   if (g_cancellable_set_error_if_cancelled (cancellable, error))
     293                 :          0 :     return NULL;
     294                 :            :   
     295                 :         67 :   return _g_local_file_info_get_from_fd (file->priv->fd,
     296                 :            :                                          attributes,
     297                 :            :                                          error);
     298                 :            : }
     299                 :            : 
     300                 :            : #ifdef G_OS_UNIX
     301                 :            : static int
     302                 :         56 : g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
     303                 :            : {
     304                 :         56 :   GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
     305                 :         56 :   return stream->priv->fd;
     306                 :            : }
     307                 :            : #endif

Generated by: LCOV version 1.14