file_stream.c 9.29 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2002, 2004, 
   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, 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 3, 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 Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#if HAVE_TERMIOS_H
# include <termios.h>
#endif

#include <mailutils/types.h>
#include <mailutils/alloc.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>
#include <mailutils/stream.h>
#include <mailutils/sys/stream.h>
#include <mailutils/sys/file_stream.h>
#include <mailutils/util.h>

#define MU_FSTR_ERROR_NOREG (MU_ERR_LAST+1)

static int
fd_read (struct _mu_stream *str, char *buf, size_t size, size_t *pret)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  ssize_t n = read (fstr->fd, buf, size);
  if (n == -1)
    return errno;
  *pret = n;
  return 0;
}

static int
fd_write (struct _mu_stream *str, const char *buf, size_t size, size_t *pret)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  ssize_t n = write (fstr->fd, (char*) buf, size);
  if (n == -1)
    return errno;
  *pret = n;
  return 0;
}

static int
fd_close (struct _mu_stream *str)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  if (fstr->fd != -1)
    {
      if ((str->flags & MU_STREAM_AUTOCLOSE) && close (fstr->fd))
	return errno;
      fstr->fd = -1;
    }
  return 0;
}

static int
fd_open (struct _mu_stream *str)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  int oflg;
  int fd;
  
  if (!fstr->filename)
    return EINVAL;
  if (fstr->fd != -1)
    fd_close (str);

  /* Map the flags to the system equivalent.  */
  if ((fstr->stream.flags & MU_STREAM_RDWR) == MU_STREAM_RDWR)
    oflg = O_RDWR;
  else if (fstr->stream.flags & (MU_STREAM_WRITE|MU_STREAM_APPEND))
    oflg = O_WRONLY;
  else /* default */
    oflg = O_RDONLY;

  if (fstr->stream.flags & MU_STREAM_APPEND)
    oflg |= O_APPEND;

  /* Handle CREAT with care, not to follow symlinks.  */
  if (fstr->stream.flags & MU_STREAM_CREAT)
    {
      /* First see if the file already exists.  */
      fd = open (fstr->filename, oflg);
      if (fd == -1)
	{
	  /* Oops bail out.  */
	  if (errno != ENOENT)
	    return errno;
	  /* Race condition here when creating the file ??.  */
	  fd = open (fstr->filename, oflg|O_CREAT|O_EXCL,
		     0600 | mu_stream_flags_to_mode (fstr->stream.flags, 0));
	}
    }
  else
    fd = open (fstr->filename, oflg);

  if (fd == -1)
    return errno;
  
  if (!(fstr->stream.flags & MU_STREAM_ALLOW_LINKS)
      && (fstr->stream.flags & (MU_STREAM_CREAT | MU_STREAM_RDWR |
				MU_STREAM_APPEND)))
    {
      struct stat fdbuf, filebuf;

      /* The following two stats should never fail.  */
      if (fstat (fd, &fdbuf) == -1
	  || lstat (fstr->filename, &filebuf) == -1)
	{
	  close (fd);
	  return errno;
	}

      /* Now check that: file and fd reference the same file,
	 file only has one link, file is plain file.  */
      if ((fdbuf.st_dev != filebuf.st_dev
	   || fdbuf.st_ino != filebuf.st_ino
	   || fdbuf.st_nlink != 1
	   || filebuf.st_nlink != 1
	   || (fdbuf.st_mode & S_IFMT) != S_IFREG))
	{
	  /* FIXME: Be silent */
	  close (fd);
	  return MU_FSTR_ERROR_NOREG;
	}
    }
  
  if (fd < 0)
    return errno;

  /* Make sure it will be closed */
  fstr->flags |= MU_STREAM_AUTOCLOSE;

  fstr->fd = fd;
  return 0;
}

int
fd_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *presult)
{ 
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  off = lseek (fstr->fd, off, SEEK_SET);
  if (off < 0)
    return errno;
  *presult = off;
  return 0;
}

int
fd_size (struct _mu_stream *str, mu_off_t *psize)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  struct stat st;
  if (fstat (fstr->fd, &st))
    return errno;
  *psize = st.st_size;
  return 0;
}

void
fd_done (struct _mu_stream *str)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  if (fstr->fd != -1)
    fd_close (str);
  if (fstr->filename)
    free (fstr->filename);
  if (fstr->echo_state)
    free (fstr->echo_state);
}

const char *
fd_error_string (struct _mu_stream *str, int rc)
{
  if (rc == MU_FSTR_ERROR_NOREG)
    return _("must be a plain file with one link");
  else
    return mu_strerror (rc);
}

#ifndef TCSASOFT
# define TCSASOFT 0
#endif

static int
fd_ioctl (struct _mu_stream *str, int code, void *ptr)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
  mu_transport_t *ptrans;
  
  switch (code)
    {
    case MU_IOCTL_GET_TRANSPORT:
      if (!ptr)
	return EINVAL;
      ptrans = ptr;
      ptrans[0] = (mu_transport_t) fstr->fd;
      ptrans[1] = NULL;
      break;

    case MU_IOCTL_SET_TRANSPORT:
      if (!ptr)
	return EINVAL;
      ptrans = ptr;
      fstr->fd = (int) ptrans[0];
      break;
      
    case MU_IOCTL_GET_TRANSPORT_BUFFER:
      if (!ptr)
	return EINVAL;
      else
	{
	  struct mu_buffer_query *qp = ptr;
	  return mu_stream_get_buffer (str, qp);
	}
      
    case MU_IOCTL_SET_TRANSPORT_BUFFER:
      if (!ptr)
	return EINVAL;
      else
	{
	  struct mu_buffer_query *qp = ptr;
	  return mu_stream_set_buffer (str, qp->buftype, qp->bufsize);
	}

    case MU_IOCTL_SET_ECHO:
      if (!ptr)
	return EINVAL;
      else
	{
	  int status;
	  struct termios t;
	  int state = *(int*)ptr;
#if HAVE_TCGETATTR
	  if (state == 0)
	    {
	      if (fstr->flags & _MU_FILE_STREAM_ECHO_OFF)
		return 0;
	      status = tcgetattr (fstr->fd, &t);
	      if (status == 0)
		{
		  fstr->echo_state = malloc (sizeof (t));
		  if (!fstr->echo_state)
		    return ENOMEM;
		  memcpy (fstr->echo_state, &t, sizeof (t));

		  t.c_lflag &= ~(ECHO | ISIG);
		  status = tcsetattr (fstr->fd, TCSAFLUSH | TCSASOFT, &t);
		  if (status == 0)
		    fstr->flags |= _MU_FILE_STREAM_ECHO_OFF;
		}
	      if (status)
		{
		  status = errno;
		  if (fstr->echo_state)
		    {
		      free (fstr->echo_state);
		      fstr->echo_state = NULL;
		    }
		}
	    }
	  else
	    {
	      if (!(fstr->flags & _MU_FILE_STREAM_ECHO_OFF))
		return 0;
	      if (tcsetattr (fstr->fd, TCSAFLUSH | TCSASOFT, fstr->echo_state))
		status = errno;
	      else
		{
		  status = 0;
		  free (fstr->echo_state);
		  fstr->echo_state = NULL;
		  fstr->flags &= ~_MU_FILE_STREAM_ECHO_OFF;
		}
	    }
	  return status;
#else
	  return ENOSYS;
#endif
	}
      
    case MU_IOCTL_GET_ECHO:
      if (!ptr)
	return EINVAL;
      else
	*(int*)ptr = fstr->flags & _MU_FILE_STREAM_ECHO_OFF;
      break;
	  
    default:
      return ENOSYS;
    }
  return 0;
}

int
fd_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) stream;

  if (fstr->fd == -1)
    return EINVAL;
  return mu_fd_wait (fstr->fd, pflags, tvp);
}

int
fd_truncate (mu_stream_t stream, mu_off_t size)
{
  struct _mu_file_stream *fstr = (struct _mu_file_stream *) stream;
  if (ftruncate (fstr->fd, size))
    return errno;
  return 0;
}

int
_mu_file_stream_create (struct _mu_file_stream **pstream, size_t size,
			const char *filename, int fd, int flags)
{
  struct _mu_file_stream *str =
    (struct _mu_file_stream *) _mu_stream_create (size, flags);
  if (!str)
    return ENOMEM;

  str->stream.read = fd_read;
  str->stream.write = fd_write;
  str->stream.open = fd_open;
  str->stream.close = fd_close;
  str->stream.done = fd_done;
  str->stream.seek = fd_seek;
  str->stream.size = fd_size;
  str->stream.ctl = fd_ioctl;
  str->stream.wait = fd_wait;
  str->stream.truncate = fd_truncate;
  str->stream.error_string = fd_error_string;

  if (filename)
    str->filename = mu_strdup (filename);
  else
    str->filename = NULL;
  str->fd = fd;
  str->flags = 0;
  *pstream = str;
  return 0;
}

int
mu_file_stream_create (mu_stream_t *pstream, const char *filename, int flags)
{
  struct _mu_file_stream *fstr;
  int rc = _mu_file_stream_create (&fstr,
				   sizeof (struct _mu_file_stream),
				   filename, -1,
				   flags | MU_STREAM_SEEK | MU_STREAM_AUTOCLOSE);
  if (rc == 0)
    {
      mu_stream_t stream = (mu_stream_t) fstr;
      mu_stream_set_buffer (stream, mu_buffer_full, 0);
      rc = mu_stream_open (stream);
      if (rc)
	mu_stream_unref (stream);
      else
	*pstream = stream;
    }
  return rc;
}

int
mu_fd_stream_create (mu_stream_t *pstream, char *filename, int fd, int flags)
{
  struct _mu_file_stream *fstr;
  int rc;

  if (flags & MU_STREAM_SEEK)
    {
      if (lseek (fd, 0, SEEK_SET))
	return errno;
    }

  rc = _mu_file_stream_create (&fstr,
			       sizeof (struct _mu_file_stream),
			       filename, fd, flags|_MU_STR_OPEN);
  if (rc == 0)
    {
      mu_stream_t stream = (mu_stream_t) fstr;
      mu_stream_set_buffer (stream, mu_buffer_full, 0);
      *pstream = stream;
    }
  return rc;
}