aboutsummaryrefslogtreecommitdiff
path: root/src/fuse/readdir.c
blob: 9cb914ba721b0ca1d04cf7fa2a1d3a58a44f56f2 (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
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
/*
  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

*/
/*
 * readdir.c - FUSE read directory function
 *
 *  Created on: Mar 14, 2012
 *      Author: MG, Christian Grothoff, Matthias Wachs,
 *      		Krista Bennett, James Blackwell, Igor Wronsky
 *
 * Read directory
 *
 * This supersedes the old getdir() interface.  New applications
 * should use this.
 *
 * The filesystem may choose between two modes of operation:
 *
 * 1) The readdir implementation ignores the offset parameter, and
 * passes zero to the filler function's offset.  The filler
 * function will not return '1' (unless an error happens), so the
 * whole directory is read in a single readdir operation.  This
 * works just like the old getdir() method.
 *
 * 2) The readdir implementation keeps track of the offsets of the
 * directory entries.  It uses the offset parameter and always
 * passes non-zero offset to the filler function.  When the buffer
 * is full (or an error happens) the filler function will return
 * '1'.
 *
 * Introduced in version 2.3
 */
/**
 * @file fuse/readdir.c
 * @brief readdir of fuse
 * @author Christian Grothoff
 */
#include "gnunet-fuse.h"
#include "gfs_download.h"

/**
 * Closure for 'process_directory_entry'.
 */
struct DepContext
{
  /**
   * Function to call on each entry.
   */
  fuse_fill_dir_t filler;

  /**
   * 'buf' argument to give to filler.
   */
  void *buf;

  /**
   * Basepath to add for the entries.
   */
  const char *path;
};


/**
 * Function used to process entries in a directory.
 *
 * @param cls closure
 * @param filename name of the file in the directory
 * @param uri URI of the file
 * @param metadata metadata for the file; metadata for
 *        the directory if everything else is NULL/zero
 * @param length length of the available data for the file
 *           (of type size_t since data must certainly fit
 *            into memory; if files are larger than size_t
 *            permits, then they will certainly not be
 *            embedded with the directory itself).
 * @param data data available for the file (length bytes)
 */
static void 
process_directory_entry (void *cls,
			 const char *filename,
			 const struct GNUNET_FS_Uri *
			 uri,
			 const struct
			 GNUNET_CONTAINER_MetaData *
			 meta, size_t length,
			 const void *data)
{
  struct DepContext *dc = cls;
  struct GNUNET_FUSE_PathInfo *pi;
  char *path;
  int is_directory;

  if (NULL == filename)
    return; /* info about the directory itself */
  GNUNET_asprintf (&path,
		   "%s%s",
		   dc->path,
		   filename);
  is_directory = GNUNET_FS_meta_data_test_for_directory (meta);
  if (GNUNET_SYSERR == is_directory)
    is_directory = GNUNET_NO; /* if in doubt, say no */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Listing filename `%s' in directory `%s' (%s)\n", 
	      filename,
	      dc->path,
	      path);
  pi = GNUNET_FUSE_path_info_create (path, uri, is_directory);
  dc->filler (dc->buf,
	      filename, 
	      &pi->stbuf,
	      0);
  GNUNET_FUSE_path_info_done (pi);
}


int
gn_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
	    off_t offset, struct fuse_file_info *fi)
{
  struct GNUNET_FUSE_PathInfo *path_info;
  struct DepContext dc;
  size_t size;
  int ret;
  void *data;
  struct GNUNET_DISK_MapHandle *mh;
  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, 
						  0,
						  GNUNET_FS_uri_chk_get_file_size (path_info->uri)))
      {
	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 */
      }
    } 

  dc.filler = filler;
  dc.path = path;
  dc.buf = buf;
  size = (size_t) 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;
  }
  data = GNUNET_DISK_file_map (fh, 
			       &mh,
			       GNUNET_DISK_MAP_TYPE_READ,
			       size);
  if (NULL == data)
  {
    GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
    GNUNET_FUSE_path_info_done (path_info);
    return -EBADF;
  }
  filler (buf, ".", NULL, 0);
  filler (buf, "..", NULL, 0);
  ret = 0;
  if (GNUNET_OK !=
      GNUNET_FS_directory_list_contents (size,
					 data, 0LL,
					 &process_directory_entry,
					 &dc))
    ret = - EIO;
  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
  GNUNET_DISK_file_close (fh);
  GNUNET_FUSE_path_info_done (path_info);
  return ret;    
}

/* end of readdir.c */