summaryrefslogtreecommitdiff
path: root/src/lib/efreet
diff options
context:
space:
mode:
authorCedric BAIL <cedric@osg.samsung.com>2016-11-14 15:16:32 -0800
committerCedric BAIL <cedric@osg.samsung.com>2016-11-14 15:17:12 -0800
commit6da2fbc9c432e3c845cdc0a3e647eeb27d582c33 (patch)
tree7781760fd5001620ffbe796bbbfaec25e88ee1ef /src/lib/efreet
parent8368f6e1d269fb19781ed00d7bba636a703512df (diff)
efreet: make mime type evaluation way faster by using mmap.
Diffstat (limited to 'src/lib/efreet')
-rw-r--r--src/lib/efreet/efreet_mime.c64
1 files changed, 24 insertions, 40 deletions
diff --git a/src/lib/efreet/efreet_mime.c b/src/lib/efreet/efreet_mime.c
index 63e6b15c02..3197c973c8 100644
--- a/src/lib/efreet/efreet_mime.c
+++ b/src/lib/efreet/efreet_mime.c
@@ -1128,34 +1128,23 @@ efreet_mime_magic_check_priority(const char *file,
1128 Efreet_Mime_Magic *m = NULL; 1128 Efreet_Mime_Magic *m = NULL;
1129 Efreet_Mime_Magic_Entry *e = NULL; 1129 Efreet_Mime_Magic_Entry *e = NULL;
1130 Eina_List *l, *ll; 1130 Eina_List *l, *ll;
1131 FILE *f = NULL; 1131 Eina_File *f = NULL;
1132 unsigned int i = 0, offset = 0,level = 0, match = 0, bytes_read = 0; 1132 const char *mem = NULL;
1133 size_t sz;
1134 unsigned int i = 0, offset = 0,level = 0, match = 0;
1133 const char *last_mime = NULL; 1135 const char *last_mime = NULL;
1134 int c; 1136 int c;
1135 char v, buf[EFREET_MIME_MAGIC_BUFFER_SIZE]; 1137 char v;
1136 struct stat s;
1137 1138
1138#ifdef _WIN32 1139 if (!magics) return NULL;
1139 if (stat(file, &s) || s.st_size == 0)
1140#else
1141 if (lstat(file, &s) || s.st_size == 0)
1142#endif
1143 return NULL;
1144 1140
1145 f = fopen(file, "rb"); 1141 f = eina_file_open(file, EINA_FALSE);
1146 if (!f) return NULL; 1142 if (!f) return NULL;
1147 1143
1148 if (!magics) 1144 mem = eina_file_map_all(f, EINA_FILE_RANDOM);
1149 { 1145 if (!mem) goto end;
1150 fclose(f);
1151 return NULL;
1152 }
1153 1146
1154 if ((bytes_read = fread(buf, 1, sizeof(buf), f)) == 0) 1147 sz = eina_file_size_get(f);
1155 {
1156 fclose(f);
1157 return NULL;
1158 }
1159 1148
1160 EINA_LIST_FOREACH(magics, l, m) 1149 EINA_LIST_FOREACH(magics, l, m)
1161 { 1150 {
@@ -1171,30 +1160,25 @@ efreet_mime_magic_check_priority(const char *file,
1171 continue; 1160 continue;
1172 1161
1173 if ((level >= e->indent) && !match) 1162 if ((level >= e->indent) && !match)
1174 level = e->indent; 1163 {
1175 1164 level = e->indent;
1165 }
1176 else if ((level > e->indent) && match) 1166 else if ((level > e->indent) && match)
1177 { 1167 {
1178 fclose(f); 1168 goto end;
1179 return last_mime;
1180 } 1169 }
1181 1170
1182 for (offset = e->offset; offset < e->offset + e->range_len; offset++) 1171 for (offset = e->offset; offset < e->offset + e->range_len; offset++)
1183 { 1172 {
1184 if (((offset + e->value_len) > bytes_read) && 1173 if (offset + e->value_len >= sz) break;
1185 (fseek(f, offset, SEEK_SET) == -1))
1186 break;
1187 1174
1188 match = 1; 1175 match = 1;
1189 for (i = 0; i < e->value_len; ++i) 1176 for (i = 0; i < e->value_len; ++i)
1190 { 1177 {
1191 if (offset + e->value_len > bytes_read) 1178 c = mem[offset + i];
1192 c = fgetc(f);
1193 else
1194 c = buf[offset + i];
1195 1179
1196 v = e->value[i]; 1180 v = e->value[i];
1197 if (e->mask) v &= e->mask[i]; 1181 if (e->mask) c &= e->mask[i];
1198 1182
1199 if (!(c == v)) 1183 if (!(c == v))
1200 { 1184 {
@@ -1212,15 +1196,15 @@ efreet_mime_magic_check_priority(const char *file,
1212 } 1196 }
1213 } 1197 }
1214 1198
1215 if (match) 1199 if (match) break;
1216 {
1217 fclose(f);
1218 return last_mime;
1219 }
1220 } 1200 }
1221 fclose(f);
1222 1201
1223 return NULL; 1202 end:
1203 if (mem) eina_file_map_free(f, (void*) mem);
1204
1205 eina_file_close(f);
1206
1207 return last_mime;
1224} 1208}
1225 1209
1226/** 1210/**