The Official GNOME 2 Developers Guide

8.3 Synchronous Access

The easiest way to interact with GnomeVFS is with synchronous access. After obtaining a file descriptor by opening a file, you can read, write, and seek. During synchronous access, read and write data is passed in an allocated buffer, and function calls return only when they have finished their work, blocking the rest of the program.

The Unix stream system is the model for synchronous mode in GnomeVFS, and the function calls are similar, as shown here.

Unix Function

GnomeVFS Equivalent

open ()

gnome_vfs_open*()

creat ()

gnome_vfs_create*()

close()

gnome_vfs_close()

read()

gnome_vfs_read()

write()

gnome_vfs_write()

lseek ()

gnome_vfs_seek()

ftell ()

gnome_vfs_tell()

truncate()

gnome_vfs_truncate*()

unlink()

gnome_vfs_unlink*()

symlink ()

gnome_vfs_create_symbolic_link()

stat(), lstat ()

gnome_vfs_get_file_info*()

rename(), chown(), chmod()

gnome_vfs_set_file_info()

mkdir()

gnome_vfs_make_directory*()

rmdir()

gnome_vfs_remove_directory*()

Before you see the long, drawn-out description of these GnomeVFS functions, you should see them in use. The program's initial section includes a small utility function to print an appropriate error message when there is a problem with file access:

/* -*-coding: utf-8;-*- */ /* vfsdemo.c -- synchronous operations */ #include <string.h> #include <libgnome/libgnome.h> #include <libgnomevfs/gnome-vfs.h> /* print a VFS error code, its description, and the problem URI */ int print_error(GnomeVFSResult code, const gchar *uri) { const gchar *error_desc; error_desc = gnome_vfs_result_to_string(code); g_printerr("error %d when accessing %s: %s\n", code, uri, error_desc); return (code); }

The main program creates or opens a file and writes a test string:

const gchar filename[] = "gnome-vfsdemo-testfile"; const gchar test_text[] = "This is a test."; int main(int argc, char **argv) { gchar *file_path; gchar *uri; guint permissions; GnomeVFSHandle *fd; GnomeVFSResult result; GnomeVFSFileSize bytes_written; /* initialize GnomeVFS */ if (!gnome_vfs_init()) { g_error("could not initialize GnomeVFS\n"); } /* get full pathname and URI */ file_path = gnome_util_prepend_user_home(filename); g_print("filename is %s\n", file_path); uri = gnome_vfs_get_uri_from_local_path(file_path); g_print("URI is %s\n", uri); g_free(file_path); /* permissions: user has read-write access, group, and other read-only */ permissions = GNOME_VFS_PERM_USER_READ GNOME_VFS_PERM_USER_WRITE GNOME_VFS_PERM_GROUP_READ GNOME_VFS_PERM_OTHER_READ; /* create and open URI */ g_print("creating/opening URI...\n"); result = gnome_vfs_create(&fd, uri, GNOME_VFS_OPEN_WRITE, FALSE, permissions); if (result != GNOME_VFS_OK) { return (print_error(result, uri)); } g_print(" success.\n"); /* write test_text into file descriptor */ g_print("writing \"%s\" into file descriptor...\n", test_text); result = gnome_vfs_write(fd, test_text, strlen(test_text), &bytes_written); if (result != GNOME_VFS_OK) { return (print_error(result, uri)); } g_print(" success. %d bytes written.\n", (guint) bytes_written); /* close file */ g_print("closing file descriptor...\n"); result = gnome_vfs_close(fd); if (result != GNOME_VFS_OK) { return (print_error(result, uri)); } g_print(" success.\n"); g_free(uri); /* shut down GnomeVFS */ gnome_vfs_shutdown(); return (0); }

If you'd like to see an error message, run this program, run chmod 000 gnome-vfsdemo-testfile in your home directory, and then run the program again.

As you can see from this example, synchronous GnomeVFS operations are similar to those of traditional Unix utilities, but the implementation is slightly different. A URI as a string or a GnomeVFSURI structure takes the place of a filename (see Section 8.8 for more information on GnomeVFSURI ). Because there are two ways to specify URIs, there are two versions of many functions (for example, gnome_vfs_open() and gnome_vfs_open_uri() ).

GnomeVFS does not use GError, opting instead for a system very similar to errno / strerror() in Unix. All access functions return a GnomeVFSResult code. The preceding example illustrates how you might work with this error code; see Section 8.10 for a full description of the error codes.

GnomeVFSFileHandle is a full data structure. Because functions such as gnome_vfs_open() return GnomeVFSResult , they take the addresses of pointers to a GnomeVFSFileHandle as parameters. Addresses of pointers and memory pervade the GnomeVFS API.

Note  

GnomeVFSFileHandle structures are not GObject objects. There are special management functions for file descriptors.

The next few subsections form a reference to synchronous GnomeVFS file operations.

8.3.1 Opening, Creating, and Closing Files

The functions in this section use these parameters:

All of these functions return GnomeVFSResult :

Specify the GnomeVFSOpenMode mode parameter as a bitwise OR of any of the following values:

Note  

Make sure that you set the options that you need; GNOME_VFS_OPEN_WRITE does not imply GNOME_VFS_OPEN_READ .

The permissions mode parameter is a Unix-style unsigned integer. Set it to a bitwise OR of these values:

8.3.2 Reading, Writing, and Seeking

The functions in this section use these parameters:

All of these functions return GnomeVFSResult :

8.3.3 Extracting and Changing File Information

The most important function to retrieving information on a file is

GnomeVFSResult result ; result = gnome_vfs_get_file_info( uri_string , info , options );

Here, uri_string is the URI to query, info is a GnomeVFSFileInfo structure to fill, and options is a bitwise OR of any of the following:

A GnomeVFSFileInfo result usually has invalid fields because not all files exhibit every characteristic in the structure (for example, a file from the Web does not have a hard link count). Furthermore, you can turn off permissions fields. At the very least, though, you can count on this field being valid:

Admittedly, the name alone isn't much to go on. These three fields are usually also available:

Here are the other fields along with their validity bits:

You can change some of a file's information with

result = gnome_vfs_set_file_info( uri_string , info , fields );

Here, uri_string is the target URI, info is the GnomeVFSFileInfo structure containing the information, and fields is a bitwise OR for the fields to change:

The following functions are similar to the preceding basic functions and return GnomeVFSResult :

You can manage GnomeVFSFileInfo structures with these functions:

8.3.4 File Management

Functions in this section manage existing files and return GnomeVFSResult :

Now that you can work with files, you are ready to navigate and operate on GnomeVFS directories.

Категории