aboutsummaryrefslogtreecommitdiff
path: root/src/fuse/read.c
blob: d6774e091d1b799be5dd9c7a0ae6ff1927af4186 (plain) (blame)
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
/*
  This file is part of gnunet-fuse.
  (C) 2012 Christian Grothoff (and other contributing authors)
  
  gnunet-fuse is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3, or (at your
  option) any later version.
 
  gnunet-fuse 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 General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
/*
 * read.c - FUSE read function
 *
 *  Created on: Mar 14, 2012
 *      Author: mg
 *
 *		 Read data from an open file
 *
 * Read should return exactly the number of bytes requested except
 * on EOF or error, otherwise the rest of the data will be
 * substituted with zeroes.	 An exception to this is when the
 * 'direct_io' mount option is specified, in which case the return
 * value of the read system call will reflect the return value of
 * this operation.
 *
 * Changed in version 2.2
 */

/**
 * @file fuse/read.c
 * @brief reading files
 * @author Christian Grothoff
 */
#include "gnunet-fuse.h"
#include "gfs_download.h"



int
gn_read (const char *path, char *buf, size_t size, off_t offset,
	 struct fuse_file_info *fi)
{
  struct GNUNET_FUSE_PathInfo *path_info;
  uint64_t fsize;
  struct GNUNET_DISK_FileHandle *fh;

  path_info = GNUNET_FUSE_path_info_get (path);
  if (NULL == path_info)
  {
    /* FIXME: we might need to check which of the ancestors
       exist and possibly download ancestral directories,
       instead of directly giving up here... */
    return - ENOENT;
  }  
  if (NULL == path_info->tmpfile)
  {
    /* store to temporary file */
    path_info->tmpfile = GNUNET_DISK_mktemp ("gnunet-fuse-tempfile");
    if (GNUNET_OK != GNUNET_FUSE_download_file (path_info, 
						offset,
						size))
    {
      UNLINK (path_info->tmpfile);
      GNUNET_free (path_info->tmpfile);
      path_info->tmpfile = NULL;
      GNUNET_FUSE_path_info_done (path_info);
      return - EIO; /* low level IO error */
    }
  } 

  fsize = GNUNET_FS_uri_chk_get_file_size (path_info->uri);
  fh = GNUNET_DISK_file_open (path_info->tmpfile,
			      GNUNET_DISK_OPEN_READ,
			      GNUNET_DISK_PERM_NONE);			      
  if (NULL == fh)
  {
    GNUNET_FUSE_path_info_done (path_info);
    return - EBADF;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
	      "Trying to read bytes %llu-%llu/%llu of file `%s'\n",
	      (unsigned long long) offset,
	      (unsigned long long) offset + size,
	      (unsigned long long) fsize,
	      path);	      
  if (offset != GNUNET_DISK_file_seek (fh, offset, GNUNET_DISK_SEEK_SET))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
		"No data available at offset %llu of file `%s'\n",
		(unsigned long long) offset,
		path);
    GNUNET_DISK_file_close (fh);
    GNUNET_FUSE_path_info_done (path_info);
    return 0; 
  }
  size = GNUNET_MIN (size, fsize - offset);
  if (GNUNET_SYSERR == (size = GNUNET_DISK_file_read (fh, buf, size)))
  {
    int eno = errno;
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
		"Error reading from file `%s': %s\n",
		path,
		STRERROR (errno));
    GNUNET_DISK_file_close (fh);
    GNUNET_FUSE_path_info_done (path_info);
    return - eno; 
  }
  GNUNET_DISK_file_close (fh);
  GNUNET_FUSE_path_info_done (path_info);
  return size;
}

/* end of read.c */