1.31 <GFileEnumerator>

Class: <GFileEnumerator>

It derives from <GObject>.

<GFileEnumerator> allows you to operate on a set of <GFile> objects, returning a <GFileInfo> structure for each file enumerated (e.g. file:enumerate-children will return a <GFileEnumerator> for each of the children within a directory).

To get the next file’s information from a <GFileEnumerator>, use file-enumerator:next-file or its asynchronous version, file-enumerator:next-files-async. Note that the asynchronous version will return a list of <GFileInfo> objects, whereas the synchronous will only return the next file in the enumerator.

The ordering of returned files is unspecified for non-Unix platforms; for more information, see dir:read-name. On Unix, when operating on local files, returned files will be sorted by inode number. Effectively you can assume that the ordering of returned files will be stable between successive calls (and applications) assuming the directory is unchanged.

If your application needs a specific ordering, such as by name or modification time, you will have to implement that in your application code.

To close a <GFileEnumerator>, use file-enumerator:close?, or its asynchronous version, file-enumerator:close-async. Once a <GFileEnumerator> is closed, no further actions may be performed on it, and it should be freed with object:unref.

Fields

parent-instance

Properties

Property: file-enumerator:container self

The container that is being enumerated.

Its value is a <GFile>, or #f.

It can only be set when the object is made, with #:container. It cannot be read.

Procedures

Method: file-enumerator:close-async self io-priority [cancellable] [callback]
Method: close-async self io-priority [cancellable] [callback]

Asynchronously closes the file enumerator.

If cancellable is not #f, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error cancelled will be returned in file-enumerator:close-finish?.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

io-priority

Passed: a 32-bit integer.

the I/O priority of the request

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback to call when the request is satisfied

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

the data to pass to callback function

Method: file-enumerator:close-finish? self result ⇒ %return
Method: close-finish? self result ⇒ %return

Finishes closing a file enumerator, started from file-enumerator:close-async.

If the file enumerator was already closed when file-enumerator:close-async was called, then this function will report closed in error, and return #f. If the file enumerator had pending operation when the close operation was started, then this function will report pending, and return #f. If cancellable was not #f, then the operation may have been cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error cancelled will be set, and #f will be returned.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a boolean.

#t if the close operation has finished successfully.

Method: file-enumerator:close? self [cancellable] ⇒ %return
Method: close? self [cancellable] ⇒ %return

Releases all resources used by this enumerator, making the enumerator return closed on all calls.

This will be automatically called when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

%return

Returned: a boolean.

TRUE on success or FALSE on error.

Method: file-enumerator:get-child self info ⇒ %return
Method: get-child self info ⇒ %return

Return a new <GFile> which refers to the file named by info in the source directory of self. This function is primarily intended to be used inside loops with file-enumerator:next-file.

To use this, G_FILE_ATTRIBUTE_STANDARD_NAME must have been listed in the attributes list used when creating the <GFileEnumerator>.

This is a convenience method that’s equivalent to:


  gchar *name = g_file_info_get_name (info);
  GFile *child = g_file_get_child (g_file_enumerator_get_container (enumr),
                                   name);
self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>

info

Passed: a <GFileInfo>.

a <GFileInfo> gotten from file-enumerator:next-file or the async equivalents.

%return

Returned: a <GFile>.

a <GFile> for the <GFileInfo> passed it.

Method: file-enumerator:get-container self ⇒ %return
Method: get-container self ⇒ %return

Get the <GFile> container which is being enumerated.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>

%return

Returned: a <GFile>.

the <GFile> which is being enumerated.

Method: file-enumerator:has-pending? self ⇒ %return
Method: has-pending? self ⇒ %return

Checks if the file enumerator has pending operations.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

%return

Returned: a boolean.

#t if the self has pending operations.

Method: file-enumerator:is-closed? self ⇒ %return
Method: is-closed? self ⇒ %return

Checks if the file enumerator has been closed.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

%return

Returned: a boolean.

#t if the self is closed.

Method: file-enumerator:iterate self [cancellable] ⇒ %return out-info out-child
Method: iterate self [cancellable] ⇒ %return out-info out-child

This is a version of file-enumerator:next-file that’s easier to use correctly from C programs. With file-enumerator:next-file, the gboolean return value signifies "end of iteration or error", which requires allocation of a temporary <GError>.

In contrast, with this function, a #f return from file-enumerator:iterate always means "error". End of iteration is signaled by out-info or out-child being #f.

Another crucial difference is that the references for out-info and out-child are owned by self (they are cached as hidden properties). You must not unref them in your own code. This makes memory management significantly easier for C code in combination with loops.

Finally, this function optionally allows retrieving a <GFile> as well.

You must specify at least one of out-info or out-child.

The code pattern for correctly using file-enumerator:iterate from C is:


direnum = g_file_enumerate_children (file, ...);
while (TRUE)
  {
    GFileInfo *info;
    if (!g_file_enumerator_iterate (direnum, &info, NULL, cancellable, error))
      goto out;
    if (!info)
      break;
    ... do stuff with "info"; do not unref it! ...
  }

out:
  g_object_unref (direnum); // Note: frees the last @info
self

Passed: a <GFileEnumerator>.

an open <GFileEnumerator>

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>

%return

Returned: a boolean.

out-info

Returned: a <GFileInfo>.

Output location for the next <GFileInfo>, or #f

out-child

Returned: a <GFile>.

Output location for the next <GFile>, or #f

Method: file-enumerator:next-file self [cancellable] ⇒ %return
Method: next-file self [cancellable] ⇒ %return

Returns information for the next file in the enumerated object. Will block until the information is available. The <GFileInfo> returned from this function will contain attributes that match the attribute string that was passed when the <GFileEnumerator> was created.

See the documentation of <GFileEnumerator> for information about the order of returned files.

On error, returns #f and sets error to the error. If the enumerator is at the end, #f will be returned and error will be unset.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

%return

Returned: a <GFileInfo>, or #f.

A <GFileInfo> or #f on error or end of enumerator. Free the returned object with object:unref when no longer needed.

Method: file-enumerator:next-files-async self num-files io-priority [cancellable] [callback]
Method: next-files-async self num-files io-priority [cancellable] [callback]

Request information for a number of files from the enumerator asynchronously. When all I/O for the operation is finished the callback will be called with the requested information.

See the documentation of <GFileEnumerator> for information about the order of returned files.

Once the end of the enumerator is reached, or if an error occurs, the callback will be called with an empty list. In this case, the previous call to file-enumerator:next-files-async will typically have returned fewer than num-files items.

If a request is cancelled the callback will be called with cancelled.

This leads to the following pseudo-code usage:


g_autoptr(GFile) dir = get_directory ();
g_autoptr(GFileEnumerator) enumerator = NULL;
g_autolist(GFileInfo) files = NULL;
g_autoptr(GError) local_error = NULL;

enumerator = yield g_file_enumerate_children_async (dir,
                                                    G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                                    G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                                    G_FILE_QUERY_INFO_NONE,
                                                    G_PRIORITY_DEFAULT,
                                                    cancellable,
                                                    …,
                                                    &local_error);
if (enumerator == NULL)
  g_error ("Error enumerating: %s", local_error->message);

// Loop until no files are returned, either because the end of the enumerator
// has been reached, or an error was returned.
do
  {
    files = yield g_file_enumerator_next_files_async (enumerator,
                                                      5,  // number of files to request
                                                      G_PRIORITY_DEFAULT,
                                                      cancellable,
                                                      …,
                                                      &local_error);

    // Process the returned files, but don’t assume that exactly 5 were returned.
    for (GList *l = files; l != NULL; l = l->next)
      {
        GFileInfo *info = l->data;
        handle_file_info (info);
      }
  }
while (files != NULL);

if (local_error != NULL &&
    !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  g_error ("Error while enumerating: %s", local_error->message);

During an async request no other sync and async calls are allowed, and will result in pending errors.

Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is G_PRIORITY_DEFAULT.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

num-files

Passed: a 32-bit integer.

the number of file info objects to request

io-priority

Passed: a 32-bit integer.

the I/O priority of the request

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback to call when the request is satisfied

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

the data to pass to callback function

Method: file-enumerator:next-files-finish self result ⇒ %return
Method: next-files-finish self result ⇒ %return

Finishes the asynchronous operation started with file-enumerator:next-files-async.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a list, each a <GFileInfo>.

a GList of GFileInfos. You must free the list with g_list_free() and unref the infos with object:unref when you’re done with them.

Method: file-enumerator:set-pending self pending
Method: set-pending self pending

Sets the file enumerator as having pending operations.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

pending

Passed: a boolean.

a boolean value.

Virtual methods

Virtual method: file-enumerator:do-close-async implementor self io-priority [cancellable] [callback]

Asynchronously closes the file enumerator.

If cancellable is not #f, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error cancelled will be returned in file-enumerator:close-finish?.

To override it in a new class, give register-type #:vfuncs `((close-async . ,procedure)). The procedure is called as (procedure self io-priority cancellable callback user-data).

implementor

Passed: the class whose implementation to call: <GFileEnumerator>, or a class that derives from it, or its GType number.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

io-priority

Passed: a 32-bit integer.

the I/O priority of the request

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback to call when the request is satisfied

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

the data to pass to callback function

Virtual method: file-enumerator:do-close-finish implementor self result ⇒ %return

Finishes closing a file enumerator, started from file-enumerator:close-async.

If the file enumerator was already closed when file-enumerator:close-async was called, then this function will report closed in error, and return #f. If the file enumerator had pending operation when the close operation was started, then this function will report pending, and return #f. If cancellable was not #f, then the operation may have been cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error cancelled will be set, and #f will be returned.

To override it in a new class, give register-type #:vfuncs `((close-finish . ,procedure)). The procedure is called as (procedure self result) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GFileEnumerator>, or a class that derives from it, or its GType number.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a boolean.

#t if the close operation has finished successfully.

Virtual method: file-enumerator:do-close-fn implementor self [cancellable] ⇒ %return

To override it in a new class, give register-type #:vfuncs `((close-fn . ,procedure)). The procedure is called as (procedure self cancellable) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GFileEnumerator>, or a class that derives from it, or its GType number.

self

Passed: a <GFileEnumerator>.

cancellable

Passed, or left out: a <GCancellable>, or #f.

%return

Returned: a boolean.

Virtual method: file-enumerator:do-next-file implementor self [cancellable] ⇒ %return

Returns information for the next file in the enumerated object. Will block until the information is available. The <GFileInfo> returned from this function will contain attributes that match the attribute string that was passed when the <GFileEnumerator> was created.

See the documentation of <GFileEnumerator> for information about the order of returned files.

On error, returns #f and sets error to the error. If the enumerator is at the end, #f will be returned and error will be unset.

To override it in a new class, give register-type #:vfuncs `((next-file . ,procedure)). The procedure is called as (procedure self cancellable) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GFileEnumerator>, or a class that derives from it, or its GType number.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

%return

Returned: a <GFileInfo>, or #f.

A <GFileInfo> or #f on error or end of enumerator. Free the returned object with object:unref when no longer needed.

Virtual method: file-enumerator:do-next-files-async implementor self num-files io-priority [cancellable] [callback]

Request information for a number of files from the enumerator asynchronously. When all I/O for the operation is finished the callback will be called with the requested information.

See the documentation of <GFileEnumerator> for information about the order of returned files.

Once the end of the enumerator is reached, or if an error occurs, the callback will be called with an empty list. In this case, the previous call to file-enumerator:next-files-async will typically have returned fewer than num-files items.

If a request is cancelled the callback will be called with cancelled.

This leads to the following pseudo-code usage:


g_autoptr(GFile) dir = get_directory ();
g_autoptr(GFileEnumerator) enumerator = NULL;
g_autolist(GFileInfo) files = NULL;
g_autoptr(GError) local_error = NULL;

enumerator = yield g_file_enumerate_children_async (dir,
                                                    G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                                    G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                                    G_FILE_QUERY_INFO_NONE,
                                                    G_PRIORITY_DEFAULT,
                                                    cancellable,
                                                    …,
                                                    &local_error);
if (enumerator == NULL)
  g_error ("Error enumerating: %s", local_error->message);

// Loop until no files are returned, either because the end of the enumerator
// has been reached, or an error was returned.
do
  {
    files = yield g_file_enumerator_next_files_async (enumerator,
                                                      5,  // number of files to request
                                                      G_PRIORITY_DEFAULT,
                                                      cancellable,
                                                      …,
                                                      &local_error);

    // Process the returned files, but don’t assume that exactly 5 were returned.
    for (GList *l = files; l != NULL; l = l->next)
      {
        GFileInfo *info = l->data;
        handle_file_info (info);
      }
  }
while (files != NULL);

if (local_error != NULL &&
    !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  g_error ("Error while enumerating: %s", local_error->message);

During an async request no other sync and async calls are allowed, and will result in pending errors.

Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is G_PRIORITY_DEFAULT.

To override it in a new class, give register-type #:vfuncs `((next-files-async . ,procedure)). The procedure is called as (procedure self num-files io-priority cancellable callback user-data).

implementor

Passed: the class whose implementation to call: <GFileEnumerator>, or a class that derives from it, or its GType number.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

num-files

Passed: a 32-bit integer.

the number of file info objects to request

io-priority

Passed: a 32-bit integer.

the I/O priority of the request

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback to call when the request is satisfied

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

the data to pass to callback function

Virtual method: file-enumerator:do-next-files-finish implementor self result ⇒ %return

Finishes the asynchronous operation started with file-enumerator:next-files-async.

To override it in a new class, give register-type #:vfuncs `((next-files-finish . ,procedure)). The procedure is called as (procedure self result) ⇒ %return.

implementor

Passed: the class whose implementation to call: <GFileEnumerator>, or a class that derives from it, or its GType number.

self

Passed: a <GFileEnumerator>.

a <GFileEnumerator>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a list, each a <GFileInfo>.

a GList of GFileInfos. You must free the list with g_list_free() and unref the infos with object:unref when you’re done with them.