File: gnome-vfs-module-shared.c

package info (click to toggle)
gnome-vfs 1%3A2.24.4-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,120 kB
  • ctags: 10,397
  • sloc: ansic: 78,516; sh: 10,341; makefile: 902; perl: 99
file content (225 lines) | stat: -rw-r--r-- 6,120 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <config.h>

#include "gnome-vfs-module-shared.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "gnome-vfs-module.h"
#include "gnome-vfs-ops.h"

/**
 * gnome_vfs_mime_type_from_mode:
 * @mode: value as the st_mode field in the system stat structure.
 *
 * Returns a MIME type based on the @mode if it
 * references a special file (directory, device, fifo, socket or symlink).
 * This function works like gnome_vfs_mime_type_from_mode_or_default(), except
 * that it returns %NULL where gnome_vfs_mime_type_from_mode_or_default()
 * would return a fallback MIME type.
 *
 * Returns: a string containing the MIME type, or %NULL if @mode is not a
 * special file.
 */
const char *
gnome_vfs_mime_type_from_mode (mode_t mode)
{
	const char *mime_type;

	if (S_ISREG (mode))
		mime_type = NULL;
	else if (S_ISDIR (mode))
		mime_type = "x-directory/normal";
	else if (S_ISCHR (mode))
		mime_type = "x-special/device-char";
	else if (S_ISBLK (mode))
		mime_type = "x-special/device-block";
	else if (S_ISFIFO (mode))
		mime_type = "x-special/fifo";
#ifdef S_ISLNK
	else if (S_ISLNK (mode))
		mime_type = "x-special/symlink";
#endif
#ifdef S_ISSOCK
	else if (S_ISSOCK (mode))
		mime_type = "x-special/socket";
#endif
	else
		mime_type = NULL;

	return mime_type;
}

/**
 * gnome_vfs_mime_type_from_mode_or_default:
 * @mode: value as the st_mode field in the system stat structure.
 * @defaultv: default fallback MIME type.
 *
 * Returns a MIME type based on the @mode if it
 * references a special file (directory, device, fifo, socket or symlink).
 * This function works like gnome_vfs_mime_type_from_mode() except that
 * it returns @defaultv instead of %NULL.
 *
 * Returns: a string containing the MIME type, or @defaultv if @mode is not a
 * special file.
 */

const char *
gnome_vfs_mime_type_from_mode_or_default (mode_t mode,
					  const char *defaultv)
{
	const char *mime_type;

	mime_type = gnome_vfs_mime_type_from_mode (mode);
	if (mime_type == NULL) {
		mime_type = defaultv;
	}

	return mime_type;
}

/**
 * gnome_vfs_get_special_mime_type:
 * @uri: a #GnomeVFSURI to get the mime type for.
 *
 * Gets the MIME type for @uri, this function only returns the type
 * when the uri points to a file that can't be sniffed (sockets, 
 * directories, devices, and fifos).
 *
 * Returns: a string containing the mime type or %NULL if the @uri doesn't 
 * present a special file.
 */

const char *
gnome_vfs_get_special_mime_type (GnomeVFSURI *uri)
{
	GnomeVFSResult error;
	GnomeVFSFileInfo *info;
	const char *type;
	
	info = gnome_vfs_file_info_new ();
	type = NULL;
	
	/* Get file info and examine the type field to see if file is 
	 * one of the special kinds. 
	 */
	error = gnome_vfs_get_file_info_uri (uri, info, GNOME_VFS_FILE_INFO_DEFAULT);

	if (error == GNOME_VFS_OK && 
	    info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) {
		
		switch (info->type) {

			case GNOME_VFS_FILE_TYPE_DIRECTORY:
				type = "x-directory/normal";
				break;
				
			case GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE:
				type = "x-special/device-char";
				break;
				
			case GNOME_VFS_FILE_TYPE_BLOCK_DEVICE:
				type = "x-special/device-block";
				break;
				
			case GNOME_VFS_FILE_TYPE_FIFO:
				type = "x-special/fifo";
				break;
				
			case GNOME_VFS_FILE_TYPE_SOCKET:
				type = "x-special/socket";
				break;

			default:
				break;
		}

	}
	
	gnome_vfs_file_info_unref (info);
	return type;	
}

/**
 * gnome_vfs_stat_to_file_info:
 * @file_info: a #GnomeVFSFileInfo which will be filled.
 * @statptr: pointer to a 'stat' structure.
 *
 * Fills the @file_info structure with the values from @statptr structure.
 */
void
gnome_vfs_stat_to_file_info (GnomeVFSFileInfo *file_info,
			     const struct stat *statptr)
{
	if (S_ISDIR (statptr->st_mode))
		file_info->type = GNOME_VFS_FILE_TYPE_DIRECTORY;
	else if (S_ISCHR (statptr->st_mode))
		file_info->type = GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE;
	else if (S_ISBLK (statptr->st_mode))
		file_info->type = GNOME_VFS_FILE_TYPE_BLOCK_DEVICE;
	else if (S_ISFIFO (statptr->st_mode))
		file_info->type = GNOME_VFS_FILE_TYPE_FIFO;
#ifdef S_ISSOCK
	else if (S_ISSOCK (statptr->st_mode))
		file_info->type = GNOME_VFS_FILE_TYPE_SOCKET;
#endif
	else if (S_ISREG (statptr->st_mode))
		file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
#ifdef S_ISLNK
	else if (S_ISLNK (statptr->st_mode))
		file_info->type = GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK;
#endif
	else
		file_info->type = GNOME_VFS_FILE_TYPE_UNKNOWN;

	file_info->permissions
		= statptr->st_mode & (GNOME_VFS_PERM_USER_ALL
				      | GNOME_VFS_PERM_GROUP_ALL
				      | GNOME_VFS_PERM_OTHER_ALL
				      | GNOME_VFS_PERM_SUID
				      | GNOME_VFS_PERM_SGID
				      | GNOME_VFS_PERM_STICKY);

	file_info->device = statptr->st_dev;
	file_info->inode = statptr->st_ino;

	file_info->link_count = statptr->st_nlink;

	file_info->uid = statptr->st_uid;
	file_info->gid = statptr->st_gid;

	file_info->size = statptr->st_size;
#ifndef G_OS_WIN32
	file_info->block_count = statptr->st_blocks;
	file_info->io_block_size = statptr->st_blksize;
	if (file_info->io_block_size > 0 &&
	    file_info->io_block_size < 4096) {
		/* Never use smaller block than 4k,
		   should probably be pagesize.. */
		file_info->io_block_size = 4096;
	}
#else
	file_info->io_block_size = 512;
	file_info->block_count =
	  (file_info->size + file_info->io_block_size - 1) /
	  file_info->io_block_size;
#endif
	

	file_info->atime = statptr->st_atime;
	file_info->ctime = statptr->st_ctime;
	file_info->mtime = statptr->st_mtime;

	file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_TYPE |
	  GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS | GNOME_VFS_FILE_INFO_FIELDS_FLAGS |
	  GNOME_VFS_FILE_INFO_FIELDS_DEVICE | GNOME_VFS_FILE_INFO_FIELDS_INODE |
	  GNOME_VFS_FILE_INFO_FIELDS_LINK_COUNT | GNOME_VFS_FILE_INFO_FIELDS_SIZE |
	  GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT | GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE |
	  GNOME_VFS_FILE_INFO_FIELDS_ATIME | GNOME_VFS_FILE_INFO_FIELDS_MTIME |
	  GNOME_VFS_FILE_INFO_FIELDS_CTIME | GNOME_VFS_FILE_INFO_FIELDS_IDS;
}