Add a helper to eject mountpoints

This commit is contained in:
Pierre Pronchery 2018-04-21 23:50:05 +02:00
parent 4faed79fa1
commit b32f5c0636
2 changed files with 63 additions and 0 deletions

View File

@ -53,6 +53,7 @@ DIR * browser_vfs_opendir(char const * filename, struct stat * st);
int browser_vfs_closedir(DIR * dir);
struct dirent * browser_vfs_readdir(DIR * dir);
int browser_vfs_eject(char const * mountpoint);
int browser_vfs_mount(char const * mountpoint);
int browser_vfs_unmount(char const * mountpoint);

View File

@ -76,6 +76,8 @@ typedef enum _VFSFlag
/* prototypes */
/* accessors */
static String * _browser_vfs_get_device(char const * mountpoint);
static unsigned int _browser_vfs_get_flags_mountpoint(char const * mountpoint);
@ -136,6 +138,29 @@ int browser_vfs_closedir(DIR * dir)
}
/* browser_vfs_eject */
int browser_vfs_eject(char const * mountpoint)
{
int ret = 0;
char * argv[] = { "eject", "--", NULL, NULL };
const unsigned int flags = G_SPAWN_SEARCH_PATH;
GError * error = NULL;
if(mountpoint == NULL)
return error_set_code(-EINVAL, "%s", strerror(EINVAL));
if((argv[2] = _browser_vfs_get_device(mountpoint)) == NULL)
return error_get_code();
if(g_spawn_async(NULL, argv, NULL, flags, NULL, NULL, NULL, &error)
!= TRUE)
{
ret = -error_set_code(1, "%s: %s", mountpoint, error->message);
g_error_free(error);
}
free(argv[2]);
return ret;
}
/* browser_vfs_mime_icon */
static GdkPixbuf * _mime_icon_emblem(GdkPixbuf * pixbuf, int size,
char const * emblem);
@ -492,6 +517,43 @@ int browser_vfs_unmount(char const * mountpoint)
/* private */
/* functions */
/* browser_vfs_get_device */
static String * _browser_vfs_get_device(char const * mountpoint)
{
#if defined(_PATH_FSTAB)
struct fstab * f;
#endif
#if defined(ST_NOWAIT)
struct statvfs * mnt;
int res;
int i;
if((res = getmntinfo(&mnt, ST_NOWAIT)) > 0)
for(i = 0; i < res; i++)
if(strcmp(mnt[i].f_mntfromname, mountpoint) == 0)
return string_new(mnt[i].f_mntfromname);
#elif defined(MNT_NOWAIT)
struct statfs * mnt;
int res;
int i;
if((res = getmntinfo(&mnt, MNT_NOWAIT)) > 0)
for(i = 0; i < res; i++)
if(strcmp(mnt[i].f_mntfromname, mountpoint) == 0)
return string_new(mnt[i].f_mntfromname);
#endif
#if defined(_PATH_FSTAB)
if(setfsent() != 1)
return NULL;
while((f = getfsent()) != NULL)
if(strcmp(f->fs_file, mountpoint) == 0)
return string_new(f->fs_spec);
#endif
error_set_code(1, "%s: %s", mountpoint, "Device not found");
return NULL;
}
/* browser_vfs_get_flags_mountpoint */
static unsigned int _browser_vfs_get_flags_mountpoint(char const * mountpoint)
{