Function not supported - how to test

Hi, ALL,

The documentation on the “g_file_trash” says:

Which file system should be used that is known to not support file trashing thru this call?

And another question:

       GFile *f = g_file_new_for_path( path.c_str() );
        gboolean res = g_file_trash( f, NULL, &error );
       if( error && error->code == G_IO_ERROR_NOT_SUPPORTED )
            wxLogSysError( _( "MoveToRecycleBin: Operation not supported." ) );
       else if( !res )
            wxLogSysError( _( "Failed to move '%s' to Recycle Bin" ), path );
        else
        {
            result = true;
            g_object_unref( f );
       }

The call to “g_object_unref()” should be done unconditionally, right?

Thank you.

You can’t know that before trying. It’s not an issue of file system types: remote file systems might not support a trash folder, or the file system is read only. That’s why it’s a recoverable error.

As a side note, your code example is wrong. You should not only check the error code, but also the error domain, as the code is a mere integer value, and it can be the same across different domains. Use g_error_matches() with the G_IO_ERROR domain and the G_IO_ERROR_NOT_SUPPORTED code, instead.

Correct.

Emmanuelle,

You can’t know that before trying. It’s not an issue of file system types: remote file systems might not
support a trash folder, or the file system is read only. That’s why it’s a recoverable error.

OK.

As a side note, your code example is wrong. You should not only check the error code, but also the error domain, as the code is a mere integer value, and it can be the same across different domains. Use g_error_matches() with the G_IO_ERROR domain and the G_IO_ERROR_NOT_SUPPORTED code, instead.

So what the correct code will look like?

if( g_error_matches( error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED ) )
{
// report an error
}

Also, my understanding is that in this case function returns TRUE as its a recoverable error, correct? All other cases do return FALSE, indicating the actual trashing failure.

Thank you.

Yes, that’s the expected pattern for matching GError.

No: the function returns FALSE on all errors:

  • programmer errors (precondition failure), along with a critical warning
  • recoverable errors

In the case of recoverable errors, the GError* pointer is set to something that is not NULL. In general, you want something like:

GError *error = NULL;

g_file_trash (file, NULL, &error);
if (error != NULL) {
    // error handling
    g_error_free (error);
}

Emmanuelle,
So the user code expects to check both “error” and the “return value”?

Or just checking the error is good enough and will cover all cases?

The docs does say “return FALSE on failure”.
Thank you.

If you’re using just the return value without passing a GError, you will get FALSE on any error, including violations of the precondition checks.

If you’re passing a GError pointer, then you can simply check the GError: in all cases where the GError is set, the return value is FALSE. If the function returns FALSE without setting a GError is, generally speaking, a bug worthy of being filed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.