summaryrefslogtreecommitdiff
path: root/src/gsystem-file-tree-walk.c
blob: 0d424797e4fdbb9f62bc034cdad04025d7e27003 (plain)
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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 *
 * Copyright (C) 2014 Colin Walters <walters@verbum.org>
 *
 * 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 Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:gsfiletreewalk
 * @title: GSFileTreeWalk
 * @short_description: Recurse over a directory tree
 *
 * While #GFileEnumerator provides an API to iterate over one
 * directory, in many cases one wants to operate recursively.
 * This API is designed to do that.
 *
 * In addition, a #GSFileTreeWalk contains Unix-native API such as
 * file descriptor based enumeration.
 */

#include "config.h"

#define _GSYSTEM_NO_LOCAL_ALLOC
#include "libgsystem.h"
#include "gsystem-file-tree-walk.h"
#include "gsystem-enum-types.h"
#include <glib-unix.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>

/* Taken from systemd/src/shared/util.h */
union dirent_storage {
        struct dirent dent;
        guint8 storage[offsetof(struct dirent, d_name) +
                        ((NAME_MAX + 1 + sizeof(long)) & ~(sizeof(long) - 1))];
};

typedef GObjectClass GSFileTreeWalkClass;

struct _GSFileTreeWalk
{
  GObject parent;

  GSFileTreeWalkFlags flags;
  int origin_dfd;
  struct stat origin_stbuf;

  guint owns_dfd : 1;
};

G_DEFINE_TYPE (GSFileTreeWalk, gs_file_tree_walk, G_TYPE_OBJECT);

enum
{
  PROP_0,
  PROP_FLAGS,
  N_PROPS
};

static GParamSpec *gs_file_tree_walk_pspecs[N_PROPS];

static void
gs_file_tree_walk_init (GSFileTreeWalk  *self)
{
  self->origin_dfd = -1;
}

static void
gs_file_tree_walk_finalize (GObject *object)
{
  GSFileTreeWalk *self = GS_FILE_TREE_WALK (object);

  if (self->owns_dfd && self->origin_dfd != -1)
    (void) close (self->origin_dfd);

  if (G_OBJECT_CLASS (gs_file_tree_walk_class)->finalize != NULL)
    G_OBJECT_CLASS (gs_file_tree_walk_class)->finalize (object);
}

static void
gs_file_tree_walk_class_init (GSFileTreeWalkClass *class)
{
  /**
   * GSFileTreeWalk:flags:
   */
  gs_file_tree_walk_pspecs[PROP_FLAGS] = g_param_spec_flags ("flags", "", "",
                                                             GS_TYPE_FILE_TREE_WALK_FLAGS,
                                                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                                             G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (gobject_class, N_PROPS, gs_file_tree_walk_pspecs);
}

static void
gs_file_tree_walk_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  GSFileTreeWalk *self = GS_FILE_TREE_WALK (object);

  switch (prop_id)
    {
    case PROP_FLAGS:
      self->flags = g_value_get_flags (value);
      break;

    default:
      g_assert_not_reached ();
    }
}

static void
gs_file_tree_walk_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  GSFileTreeWalk *self = GS_FILE_TREE_WALK (object);

  switch (prop_id)
    {
    case PROP_FLAGS:
      g_value_set_flags (value, self->flags);
      break;

    default:
      g_assert_not_reached ();
    }
}

/**
 * gs_file_tree_walk_open:
 * @path: Directory to enumerate
 * @flags: Flags controlling enumeration
 * @cancellable: Cancellable
 * @error: Error
 *
 * Returns: (transfer full): A new directory enumerator
 */
GSFileTreeWalk *
gs_file_tree_walk_open (GFile                   *path,
                        GSFileTreeWalkFlags      flags,
                        GCancellable            *cancellable,
                        GError                 **error)
{
  gs_unref_object GSFileTreeWalk *ftw = g_object_new (GS_TYPE_FILE_TREE_WALK, "flags", flags, NULL);

  ftw->owns_dfd = TRUE;
  
  if (!gs_file_open_dir_fd (path, &ftw->origin_dfd,
                            cancellable, error))
    return NULL;

  return g_object_ref (ftw);
}

/**
 * gs_file_tree_walk_open_at:
 * @dfd: Directory file descriptor
 * @flags: Flags controlling enumeration
 * @cancellable: Cancellable
 * @error: Error
 *
 * Returns: (transfer full): A new directory enumerator
 */
GSFileTreeWalk *
gs_file_tree_walk_open_at (int                   dfd,
                           GSFileTreeWalkFlags   flags,
                           GCancellable         *cancellable,
                           GError              **error)
{
  gs_unref_object GSFileTreeWalk *ftw = g_object_new (GS_TYPE_FILE_TREE_WALK, "flags", flags, NULL);

  ftw->origin_dfd = dfd;
  if (fstat (ftw->origin_dfd, &ftw->origin_stbuf) != 0)
    {
      int errsv = errno;
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "fstat: %s", g_strerror (errsv));
      return NULL;
    }

  return g_object_ref (ftw);
}

/**
 * gs_file_tree_walk_next:
 * @self: Self
 * @out_file_type: (out): File type, may be %G_FILE_TYPE_UNKNOWN
 *
 * Returns: %TRUE if there are more files to traverse
 */
gboolean
gs_file_tree_walk_next (GSFileTreeWalk        *self,
                        GFileType             *out_file_type)
{
}

int gs_file_tree_walk_get_dirfd (GSFileTreeWalk   *self);

const char *gs_file_tree_walk_get_name (GSFileTreeWalk   *self);

char *gs_file_tree_walk_get_relpath (GSFileTreeWalk   *self);