~ubuntu-branches/ubuntu/saucy/glib2.0/saucy-proposed

« back to all changes in this revision

Viewing changes to glib/gdir.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-09-18 11:49:30 UTC
  • mfrom: (1.63.26) (172.1.16 experimental)
  • Revision ID: package-import@ubuntu.com-20130918114930-25x6p3ytts9nnlvd
Tags: 2.37.93-1ubuntu1
* Rebase on Debian experimental, remaining changes:
  + Build-Depend on python:any for cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#include "../build/win32/dirent/wdirent.c"
48
48
#endif
49
49
 
 
50
#include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
 
51
 
50
52
/**
51
53
 * GDir:
52
54
 *
65
67
#endif
66
68
};
67
69
 
 
70
/*< private >
 
71
 * g_dir_open_with_errno:
 
72
 * @path: the path to the directory you are interested in.
 
73
 * @flags: Currently must be set to 0. Reserved for future use.
 
74
 *
 
75
 * Opens a directory for reading.
 
76
 *
 
77
 * This function is equivalent to g_dir_open() except in the error case,
 
78
 * errno will be set accordingly.
 
79
 *
 
80
 * This is useful if you want to construct your own error message.
 
81
 *
 
82
 * Returns: a newly allocated #GDir on success, or %NULL on failure,
 
83
 *   with errno set accordingly.
 
84
 *
 
85
 * Since: 2.38
 
86
 */
 
87
GDir *
 
88
g_dir_open_with_errno (const gchar *path,
 
89
                       guint        flags)
 
90
{
 
91
  GDir dir;
 
92
#ifdef G_OS_WIN32
 
93
  gint saved_errno;
 
94
  wchar_t *wpath;
 
95
#endif
 
96
 
 
97
  g_return_val_if_fail (path != NULL, NULL);
 
98
 
 
99
#ifdef G_OS_WIN32
 
100
  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
 
101
 
 
102
  g_return_val_if_fail (wpath != NULL, NULL);
 
103
 
 
104
  dir.wdirp = _wopendir (wpath);
 
105
  saved_errno = errno;
 
106
  g_free (wpath);
 
107
  errno = saved_errno;
 
108
 
 
109
  if (dir.wdirp == NULL)
 
110
    return NULL;
 
111
#else
 
112
  dir.dirp = opendir (path);
 
113
 
 
114
  if (dir.dirp == NULL)
 
115
    return NULL;
 
116
#endif
 
117
 
 
118
  return g_memdup (&dir, sizeof dir);
 
119
}
 
120
 
68
121
/**
69
122
 * g_dir_open:
70
123
 * @path: the path to the directory you are interested in. On Unix
87
140
            guint         flags,
88
141
            GError      **error)
89
142
{
 
143
  gint saved_errno;
90
144
  GDir *dir;
91
 
  int errsv;
92
 
#ifdef G_OS_WIN32
93
 
  wchar_t *wpath;
94
 
#else
95
 
  gchar *utf8_path;
96
 
#endif
97
 
 
98
 
  g_return_val_if_fail (path != NULL, NULL);
99
 
 
100
 
#ifdef G_OS_WIN32
101
 
  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
102
 
 
103
 
  if (wpath == NULL)
104
 
    return NULL;
105
 
 
106
 
  dir = g_new (GDir, 1);
107
 
 
108
 
  dir->wdirp = _wopendir (wpath);
109
 
  g_free (wpath);
110
 
 
111
 
  if (dir->wdirp)
112
 
    return dir;
113
 
 
114
 
  /* error case */
115
 
  errsv = errno;
116
 
 
117
 
  g_set_error (error,
118
 
               G_FILE_ERROR,
119
 
               g_file_error_from_errno (errsv),
120
 
               _("Error opening directory '%s': %s"),
121
 
               path, g_strerror (errsv));
122
 
  
123
 
  g_free (dir);
124
 
      
125
 
  return NULL;
126
 
#else
127
 
  dir = g_new (GDir, 1);
128
 
 
129
 
  dir->dirp = opendir (path);
130
 
 
131
 
  if (dir->dirp)
132
 
    return dir;
133
 
 
134
 
  /* error case */
135
 
  errsv = errno;
136
 
 
137
 
  utf8_path = g_filename_to_utf8 (path, -1,
138
 
                                  NULL, NULL, NULL);
139
 
 
140
 
  g_set_error (error,
141
 
               G_FILE_ERROR,
142
 
               g_file_error_from_errno (errsv),
143
 
               _("Error opening directory '%s': %s"),
144
 
               utf8_path, g_strerror (errsv));
145
 
 
146
 
  g_free (utf8_path);
147
 
  g_free (dir);
148
 
 
149
 
  return NULL;
150
 
#endif
 
145
 
 
146
  dir = g_dir_open_with_errno (path, flags);
 
147
 
 
148
  if (dir == NULL)
 
149
    {
 
150
      gchar *utf8_path;
 
151
 
 
152
      saved_errno = errno;
 
153
 
 
154
      utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
 
155
 
 
156
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
 
157
                   _("Error opening directory '%s': %s"), utf8_path, g_strerror (saved_errno));
 
158
      g_free (utf8_path);
 
159
    }
 
160
 
 
161
  return dir;
151
162
}
152
163
 
153
164
#if defined (G_OS_WIN32) && !defined (_WIN64)
180
191
}
181
192
#endif
182
193
 
 
194
/*< private >
 
195
 * g_dir_new_from_dirp:
 
196
 * @dirp: a #DIR* created by opendir() or fdopendir()
 
197
 *
 
198
 * Creates a #GDir object from the DIR object that is created using
 
199
 * opendir() or fdopendir().  The created #GDir assumes ownership of the
 
200
 * passed-in #DIR pointer.
 
201
 *
 
202
 * @dirp must not be %NULL.
 
203
 *
 
204
 * This function never fails.
 
205
 *
 
206
 * Returns: a newly allocated #GDir, which should be closed using
 
207
 *     g_dir_close().
 
208
 *
 
209
 * Since: 2.38
 
210
 **/
 
211
GDir *
 
212
g_dir_new_from_dirp (gpointer dirp)
 
213
{
 
214
#ifdef G_OS_UNIX
 
215
  GDir *dir;
 
216
 
 
217
  g_return_val_if_fail (dirp != NULL, NULL);
 
218
 
 
219
  dir = g_new (GDir, 1);
 
220
  dir->dirp = dirp;
 
221
 
 
222
  return dir;
 
223
#else
 
224
  g_assert_not_reached ();
 
225
#endif
 
226
}
 
227
 
183
228
/**
184
229
 * g_dir_read_name:
185
230
 * @dir: a #GDir* created by g_dir_open()