Ejemplo n.º 1
0
/**
 * get mime type according to URL
 */
const char* mime_get_type(const char* url)
{
	rt_uint32_t index;

	index = 0;
	if (url == RT_NULL) return mime_tables[0].type;

	while (mime_tables[index].name != RT_NULL)
	{
		if (str_end_with(url, mime_tables[index].name))
			return mime_tables[index].type;

		index ++;
	}

	/* return text/html as default */
	return mime_tables[0].type;
}
Ejemplo n.º 2
0
/**
 * 查找.apk文件
 */
int findApkFile(const char* dirpath)
{
	int file_count = 0;
	DIR* d = opendir(dirpath);
	size_t path_len = strlen(dirpath);
	if(d){
		struct dirent* p;
		while((p = readdir(d))){
			char* buf;
			size_t len;
			/*skip the names "." and ".." */
			if(!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")){
				continue;
			}

			if(str_end_with(p->d_name, ".apk") == -1){
				continue;
			}

			len = path_len + strlen(p->d_name) + 2;
			buf = malloc(len);
			if(buf){
				snprintf(buf, len, "%s/%s", dirpath, p->d_name);
				struct stat st;
				if(!stat(buf, &st)){
					if(S_ISDIR(st.st_mode)){//dir
						file_count += findFile(buf);
					}else{//file
						file_count++;
					}
					free(buf);
				}
			}
		}
		closedir(d);
	}else{
		LOGE("xxx open dirpath failed :%s\n", dirpath);
	}
	return file_count;
}
Ejemplo n.º 3
0
static int _map_fd_path(watch_entry_t *pentry, int file_fd, 
			const char *fullpath, int parent_fd)
{
	int len, index;
	path_entry_t *ppath = pentry->normal_entry;

	dbg("in file_fd=%d parent_fd=%d fullpath=%s\n", file_fd, parent_fd, fullpath);
	len = strlen(fullpath) + 1;
	/* find the valid paht_entry */
	do{
		/* didnot in this path_entry, try next */
		if( (file_fd >= ppath->min) && (file_fd <= ppath->max)
				&& (ppath->start + len < ppath->end ) )
		{
			/* can use */
			break;
		}
		/* this path_entry no memory, try next */
		ppath = ppath->next;
	}while(ppath != NULL);

	if( NULL == ppath )
	{
		/* get new path_entry */
		ppath = _get_path_entry();
		if( NULL == ppath )
		{
			/* no memory */
			return -1;
		}

		/* insert to list */
		ppath->next = pentry->normal_entry;
		pentry->normal_entry = ppath;
		ppath->min = (file_fd/PATH_ENTRY_NUM) * PATH_ENTRY_NUM;
		ppath->max = ppath->min + PATH_ENTRY_NUM;
		dbg("min=%d max = %d\n", ppath->min, ppath->max);
	}
	dbg("find path_entry\n");

	/* set the data */
	index = file_fd - ppath->min;
	ppath->path_hash[index].fd = file_fd;
	ppath->path_hash[index].prefix_fd = parent_fd;
	dbg("index=%d max=%d min=%d\n", index, ppath->max, ppath->min);
	if(-1 == parent_fd )
	{
		/* if not parrent, this fullpath is it's path */
		int len = strlen(fullpath);
		unsigned int hash = hash_gen(fullpath,len);
		
		dbg("find this_path: %s hash=%u\n", fullpath, hash);
		if( (len > 2) && str_end_with(fullpath, '/') )
			len -= 1;
		ppath->end -= (len+1);
		strncpy(ppath->end, fullpath, len);
		ppath->end[len] = 0;
		if( NULL == hash_insert(&pentry->hash_table, hash, ppath->end) )
		{
			dbg("watch: map fd path failed: hash:%u path:%s\n", 
					hash, ppath->end);
			return -1;
		}

		ppath->path_hash[index].hash = hash;
	}else{
		/* split this_data & prefix_data , get them's hash_data */
		char *this_path;
		unsigned int this_hash;
		int this_len = 0;

		len = strlen(fullpath);
		this_path = strrchr(fullpath, '/');
		if( this_path )
		{
			/* the fullpath is start with "/", and no other level */
			this_path++;
			/* end with '/' */
			if( ! *this_path )
			{
				this_path = (char *)fullpath;
			}
		}

		if( NULL == this_path )
		{
			/* fullpath is no prefix path */
			this_path = (char *)fullpath;
		}

		/* save the this path to the memory */
		this_len = strlen(this_path) + 1;
		ppath->end -= this_len;
		strncpy(ppath->end, this_path, this_len);
		if( (this_len > 2) && str_end_with(this_path, '/') )
		{
			ppath->end[this_len-2] = 0;
		}

		this_hash = hash_gen(this_path, this_len-1);
		dbg("find this_path: %s hash=%u\n", this_path, this_hash);
		if( NULL == hash_insert(&pentry->hash_table, this_hash, ppath->end) )
		{
			dbg("watch: map fd path failed: hash:%u path:%s\n", 
					this_hash, ppath->end);
			return -1;
		}

		ppath->path_hash[index].hash = this_hash;
	}

#if 0
	/* incarse max */
	if( file_fd > ppath->max )
	{
		//ppath->max = file_fd;
	}
#endif

	/* modify start point */
	ppath->start += sizeof(path_hash_t);
	return 0;
}
Ejemplo n.º 4
0
static int _watch_on_dir_recur(watch_entry_t *pentry, const char *dir_path, int parent_fd)
{
	/* cause of may recursive many levels, so the Stack_Var replace with Static_Var */
	static struct stat fstat;
	static int _watch_fd = 0;
	static int ret = 0;
	char fullpath[128];

	_watch_fd = inotify_add_watch(pentry->fd, dir_path, WATCH_EVENTS);
	if( -1 == _watch_fd )
	{
		return -1;
	}
	_map_fd_path(pentry, _watch_fd, dir_path, parent_fd);
	printf("\tadd:%d %d %s\n", pentry->fd, _watch_fd, dir_path);
	parent_fd = _watch_fd;

	if( -1 == lstat(dir_path, &fstat) )
	{
		dbg("\tget %s stat error.\n", dir_path);
		return -1;
	}
	if( S_ISDIR(fstat.st_mode) )
	{
		/* the two cannot define with static, must be Stack_Var */
		DIR *dir;
		struct dirent *dir_ent;
		dir = opendir(dir_path);
		while( NULL != (dir_ent = readdir(dir)) )
		{
			/* skip . && .. */
			if( (0 == strcmp(dir_ent->d_name, ".")) \
				|| (0 == strcmp(dir_ent->d_name, "..")) )
				continue;

			/* is directory then recursive */
			if( str_end_with(dir_path, '/') )
				ret = sprintf(fullpath, "%s%s", dir_path, dir_ent->d_name);
			else
				ret = sprintf(fullpath, "%s/%s", dir_path, dir_ent->d_name);
			fullpath[ret] = 0;
#if 0
			printf("\nd_name=%s\ndir_path=%s\nfullpath: %s\n",
					dir_ent->d_name, dir_path, fullpath);
#endif
			if( -1 == lstat(fullpath, &fstat) )
				continue;
			if( S_ISDIR(fstat.st_mode) )
			{
				dbg("%s is dir\n", dir_ent->d_name);
				_watch_on_dir_recur(pentry, fullpath, parent_fd);
			}
		}

		/* 2013-06-02 14:25:49 zhaocq */
		if( dir )
			closedir(dir);
	}

	return 0;
}