Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / client / fuse / gnomevfsfileinfo.c
1 /* $Id$
2  * Client fuse interface handling of 'GnomeVFSFileInfo' for libcaptive
3  * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <libgnomevfs/gnome-vfs-result.h>
23 #include <glib/gmessages.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include <libgnomevfs/gnome-vfs-file-info.h>
27 #include <time.h>
28
29 #include <captive/client-vfs.h>
30
31 #include "gnomevfsresult.h"     /* self */
32 #include "main.h"
33
34
35 static CaptiveVfsVolumeInfo volume_info;
36 static gboolean volume_info_valid=FALSE;
37
38
39 int gnomevfsfileinfo_to_stat(struct stat *stat,const GnomeVFSFileInfo *file_info)
40 {
41         g_return_val_if_fail(stat!=NULL,-EINVAL);
42         g_return_val_if_fail(file_info!=NULL,-EINVAL);
43
44         if (!volume_info_valid) {
45 GnomeVFSResult errvfsresult;
46
47                 if (GNOME_VFS_OK!=(errvfsresult=captive_vfs_volume_info_get(capfuse_captive_vfs_object,&volume_info)))
48                         return -gnomevfsresult_to_errno(errvfsresult);
49                 volume_info_valid=TRUE;
50                 }
51
52         stat->st_mode=0;
53
54         if (file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_TYPE)
55                 switch (file_info->type) {
56                         case GNOME_VFS_FILE_TYPE_REGULAR:
57                                 stat->st_mode|=S_IFREG;
58                                 if (1 /* !private */)
59                                         stat->st_mode|=0444;
60                                 break;
61                         case GNOME_VFS_FILE_TYPE_DIRECTORY:
62                                 stat->st_mode|=S_IFDIR;
63                                 if (1 /* !private */)
64                                         stat->st_mode|=0555;
65                                 break;
66                         default:
67                                 g_warning("Unknown GnomeVFSFileInfo.type=%d of: %s",(int)file_info->type,file_info->name);
68                                 return -EINVAL;
69                         }
70         if (file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS)
71                 stat->st_mode|=file_info->permissions & 0777;
72         else
73                 stat->st_mode|=0600;
74
75         if (!(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_LINK_COUNT))
76                 stat->st_nlink=1;
77         else
78                 stat->st_nlink=file_info->link_count;
79
80         stat->st_uid=0; /* we own the file */
81         stat->st_gid=0; /* we own the file */
82
83         if (!(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_SIZE))
84                 stat->st_size=0;
85         else
86                 stat->st_size=file_info->size;
87
88         if (!(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_ATIME))
89                 stat->st_atime=time(NULL);
90         else
91                 stat->st_atime=file_info->atime;
92
93         if (!(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_MTIME))
94                 stat->st_mtime=time(NULL);
95         else
96                 stat->st_mtime=file_info->mtime;
97
98         if (!(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_CTIME))
99                 stat->st_ctime=time(NULL);
100         else
101                 stat->st_ctime=file_info->ctime;
102
103         stat->st_blksize=volume_info.block_size;
104
105         if (!(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT)) {
106                 /* Rounding? */
107                 /* 0? */
108                 stat->st_blocks=stat->st_size/volume_info.block_size;
109                 }
110         else
111                 stat->st_blocks=file_info->block_count;
112
113         stat->st_dev=0;
114         stat->st_rdev=0;        /* Never to be used. */
115         stat->st_ino=0;
116
117         return 0;
118 }