Android adb “push pull”中文支持解决方案
在windows底下文件(夹)命名所采用的是GBK编码,而在Android中采用的UTF-8编码,所有使用adb 的push和pull命令时就会导致由于编码方式的不同而产生的错误,解决这一问题就只有对adb工具的源代码进行修改,让adb对文件名的编码进行相应的转换。
具体过程如下:使用ubuntu 12.04 下载android的源代码,具体过程参考网络,ubuntu一定要使用64位机,因为最新的android源代码只能在64位机进行编译。http://source.android.com/
下载源代码可能需要比较长的时间,把机器挂在那,放一晚上基本就OK了。下载好源代码以后,就可以开始对adb的源码进行相应的修改。
adb的源码所在的目录是/system/core/adb
pull和push命令进行文件传输的主要的过程是位于file_sync_client.c这个文件中,在文件的开头部分增加,
#ifdef USE_MINGW
#include<windows.h>
#endif
头文件声明,然后增加两个GBK同UTF-8编码相互转换的函数
static int GBKToUTF8(char *lpGBKStr, char *lpUTF8Str, int nUTF8StrLen)
{
wchar_t *lpUnicodeStr = NULL;
int nRetLen = 0;
if (!lpGBKStr) return 0;
nRetLen = MultiByteToWideChar(CP_ACP, 0, (char *)lpGBKStr, -1, NULL, 0);
lpUnicodeStr = (wchar_t *)malloc(sizeof(WCHAR) * (nRetLen + 1));
nRetLen = MultiByteToWideChar(CP_ACP, 0, (char *)lpGBKStr, -1, lpUnicodeStr, nRetLen);
if (!nRetLen) return 0;
nRetLen = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, NULL, 0, NULL, 0);
if (!lpUTF8Str)
{
if (lpUnicodeStr) free(lpUnicodeStr);
return nRetLen;
}
if (nUTF8StrLen < nRetLen)
{
if (lpUnicodeStr) free(lpUnicodeStr);
return 0;
}
nRetLen = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, (char *)lpUTF8Str,nUTF8StrLen, NULL, 0);
if (lpUnicodeStr) free(lpUnicodeStr);
return nRetLen;
}
static int UTF8ToGBK(char *lpGBKStr,char *lpUTF8Str, int nGBKStrLen)
{
wchar_t *lpUnicodeStr=NULL;
int nRetLen =0;
if (!lpUTF8Str)return 0;
nRetLen=MultiByteToWideChar(CP_UTF8,0,(char*)lpUTF8Str,-1,NULL,NULL);
lpUnicodeStr = (wchar_t *)malloc(sizeof(WCHAR) * (nRetLen + 1));
nRetLen = MultiByteToWideChar(CP_UTF8, 0, (char *)lpUTF8Str, -1, lpUnicodeStr, nRetLen);
if (!nRetLen)return 0;
nRetLen = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, NULL, 0, NULL, NULL);
if (!lpGBKStr)
{
if (lpUnicodeStr) free(lpUnicodeStr);
return nRetLen;
}
if (nGBKStrLen < nRetLen)
{
if (lpUnicodeStr) free(lpUnicodeStr);
return 0;
}
nRetLen = WideCharToMultiByte(CP_ACP, 0, lpUnicodeStr, -1, (char *)lpGBKStr,nGBKStrLen, NULL, NULL);
if (lpUnicodeStr) free(lpUnicodeStr);
return nRetLen;
}
再添加了这两个编码的函数后,就需要修改copyinfo *mkcopyinfo这个函数,使这个函数增加对UTF8编码的支持:
copyinfo *mkcopyinfo(const char *spath, const char *dpath,
const char *sname, const char *dname,int isdir)
{
int slen = strlen(spath);
int dlen = strlen(dpath);
int snlen = strlen(sname);
int dnlen = strlen(dname);
int ssize = slen + snlen + 2;
int dsize = dlen + dnlen + 2;
copyinfo *ci = malloc(sizeof(copyinfo) + ssize + dsize);
if(ci == 0) {
fprintf(stderr,"out of memory\n");
abort();
}
ci->next = 0;
ci->time = 0;
ci->mode = 0;
ci->size = 0;
ci->flag = 0;
ci->src = (const char*)(ci + 1);
ci->dst = ci->src + ssize;
snprintf((char*) ci->src, ssize, isdir ? "%s%s/" : "%s%s", spath, sname);
snprintf((char*) ci->dst, dsize, isdir ? "%s%s/" : "%s%s", dpath, dname);
// fprintf(stderr,"mkcopyinfo('%s','%s')\n", ci->src, ci->dst);
return ci;
}
继续修改函数:
local_build_list();让该函数传进去的参数的名称为utf-8编码
static int local_build_list(copyinfo **filelist,
const char *lpath, const char *rpath)
{
DIR *d;
struct dirent *de;
struct stat st;
copyinfo *dirlist = 0;
copyinfo *ci, *next;
// fprintf(stderr,"local_build_list('%s','%s')\n", lpath, rpath);
d = opendir(lpath);
if(d == 0) {
fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
return -1;
}
while((de = readdir(d))) {
char stat_path[PATH_MAX];
char *name = de->d_name;
if(name[0] == '.') {
if(name[1] == 0) continue;
if((name[1] == '.') && (name[2] == 0)) continue;
}
#ifdef USE_MINGW
char utf8name[260];
int name_len=GBKToUTF8(name,NULL,0);
name_len=GBKToUTF8(name,utf8name,name_len);
#endif
if (strlen(lpath) + strlen(de->d_name) + 1 > sizeof(stat_path))
continue;
strcpy(stat_path, lpath);
strcat(stat_path, de->d_name);
stat(stat_path, &st);
if (S_ISDIR(st.st_mode)) {
ci = mkcopyinfo(lpath, rpath, name,name, 1);
#ifdef USE_MINGW
ci = mkcopyinfo(lpath,rpath,name,utf8name,1);
#endif
ci->next = dirlist;
dirlist = ci;
} else {
ci = mkcopyinfo(lpath, rpath, name, name,0);
#ifdef USE_MINGW
ci = mkcopyinfo(lpath,rpath,name,utf8name,0);
#endif
if(lstat(ci->src, &st)) {
fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno));
closedir(d);
return -1;
}
if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
fprintf(stderr, "skipping special file '%s'\n", ci->src);
free(ci);
} else {
ci->time = st.st_mtime;
ci->mode = st.st_mode;
ci->size = st.st_size;
ci->next = *filelist;
*filelist = ci;
}
}
}
closedir(d);
for(ci = dirlist; ci != 0; ci = next) {
next = ci->next;
local_build_list(filelist, ci->src, ci->dst);
free(ci);
}
}
以及相应的一些有关于名称编码的函数,现将修改好的c文件以及编译好的adb文件的下载链接提供如下
提供的下载地址为:
http://download.csdn.net/detail/shuaihj/5353463