下面是想使用写文件方式来调试HAL的so文件,但是遇到一个问题,就是这个文件不能被创建,必须是原来就有的文件,写进去。
而同样的代码放入到一个可执行文件的代码中去,就可以创建文件。参考http://blog.csdn.net/tankaro/article/details/9147401
extern "C" {
static int write_file(const char *path, const char *value)
{
int fd, ret, len;
int open_flag = O_RDWR;
int open_mode = 0;
ret = access(path, 0);
if(ret != 0){
LOGE("access:%s ret != 0\n", path);
open_flag = O_RDWR;
fd = creat(path, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd < 0) {
LOGE("creat: can't creat file:%s\n", path);
return -errno;
}
close(fd);
}else{
LOGE("access:%s ret == 0\n", path);
open_flag = O_CREAT;
open_mode = S_IWUSR;
}
fd = open(path, O_WRONLY);
if (fd < 0) {
LOGE("write_file: can't open file:%s\n", path);
return -errno;
}
len = strlen(value);
do {
ret = write(fd, value, len);
} while (ret < 0 && errno == EINTR);
close(fd);
if (ret < 0) {
LOGE("write file failed:%s,%s\n",path, value);
return -errno;
}
return 0;
}
}// end extern "C"