class Gio::FileEnumerator

Overview

#GFileEnumerator allows you to operate on a set of #GFiles, returning a #GFileInfo structure for each file enumerated (e.g. 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 g_file_enumerator_next_file() or its asynchronous version, g_file_enumerator_next_files_async(). Note that the asynchronous version will return a list of #GFileInfos, 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 g_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 g_file_enumerator_close(), or its asynchronous version, g_file_enumerator_close_async(). Once a #GFileEnumerator is closed, no further actions may be performed on it, and it should be freed with g_object_unref().

Defined in:

lib/gi-crystal/src/auto/gio-2.0/file_enumerator.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, cast?(obj : GObject::Object) : self? cast?, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

compat_control(what : UInt64, data : Pointer(Void)?) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Constructor Detail

def self.new #

Initialize a new FileEnumerator.


def self.new(*, container : Gio::File? = nil) #

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


Instance Method Detail

def child(info : Gio::FileInfo) : Gio::File #

Return a new #GFile which refers to the file named by @info in the source directory of @enumerator. This function is primarily intended to be used inside loops with g_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); ]|


def close(cancellable : Gio::Cancellable?) : Bool #

Releases all resources used by this enumerator, making the enumerator return %G_IO_ERROR_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.


def close_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

Asynchronously closes the file enumerator.

If @cancellable is not nil, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be returned in g_file_enumerator_close_finish().


def close_finish(result : Gio::AsyncResult) : Bool #

Finishes closing a file enumerator, started from g_file_enumerator_close_async().

If the file enumerator was already closed when g_file_enumerator_close_async() was called, then this function will report %G_IO_ERROR_CLOSED in @error, and return false. If the file enumerator had pending operation when the close operation was started, then this function will report %G_IO_ERROR_PENDING, and return false. If @cancellable was not nil, then the operation may have been cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and false will be returned.


def container : Gio::File #

Get the #GFile container which is being enumerated.


def container=(value : Gio::File?) : Gio::File? #

def has_pending : Bool #

Checks if the file enumerator has pending operations.


def is_closed : Bool #

Checks if the file enumerator has been closed.


def iterate(cancellable : Gio::Cancellable?) : Bool #

This is a version of g_file_enumerator_next_file() that's easier to use correctly from C programs. With g_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 false return from g_file_enumerator_iterate() always means "error". End of iteration is signaled by @out_info or @out_child being nil.

Another crucial difference is that the references for @out_info and @out_child are owned by @direnum (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 g_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 ]|


def next_file(cancellable : Gio::Cancellable?) : Gio::FileInfo? #

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 nil and sets @error to the error. If the enumerator is at the end, nil will be returned and @error will be unset.


def next_files_async(num_files : Int32, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

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.

The callback can be called with less than @num_files files in case of error or at the end of the enumerator. In case of a partial error the callback will be called with any succeeding items and no error, and on the next request the error will be reported. If a request is cancelled the callback will be called with %G_IO_ERROR_CANCELLED.

During an async request no other sync and async calls are allowed, and will result in %G_IO_ERROR_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.


def next_files_finish(result : Gio::AsyncResult) : GLib::List #

Finishes the asynchronous operation started with g_file_enumerator_next_files_async().


def pending=(pending : Bool) : Nil #

Sets the file enumerator as having pending operations.