From: Xu Wang <cngesa...@gmail.com> Method of get_inode is different between Linux and WIN32 plateform. This patch added inode caculate method on Windows plateform so that backing file check could work on Windows plateform.
Signed-off-by: Xu Wang <cngesa...@gmail.com> --- block.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 148 insertions(+), 8 deletions(-) diff --git a/block.c b/block.c index aea653d..1e64665 100644 --- a/block.c +++ b/block.c @@ -4433,6 +4433,147 @@ bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; } +#ifdef _WIN32 +static int get_lnk_target_file(const char *lnk_file, char *filepath) +{ + unsigned int flag, offset; + unsigned int sflag; + char uch; + int i = 0; + + FILE *fd = fopen(lnk_file, "rb"); + if (!fd) { + error_report("Open file %s failed.", lnk_file); + return -1; + } + + /* Check if the file is shortcuts by reading first 4 bytes if it's 0x4c */ + if (fread(&flag, 4, 1, fd) != 1) { + error_report("Read 0x4c field of %s failed.", lnk_file); + goto err; + } + + if (flag != 0x4c) { + error_report("%s is not a lnk file.", lnk_file); + goto err; + } + + /* Parse flags on offset 0x14 */ + if (fseek(fd, 0x14, SEEK_SET)) { + error_report("Seek flags of %s failed.", lnk_file); + goto err; + } + + if (fread(&flag, 4, 1, fd) != 1) { + error_report("Read flags of %s failed.", lnk_file); + goto err; + } + + /* seek to the length of item ID list */ + if (fseek(fd, 0x4c, SEEK_SET)) { + error_report("Seek %s length of item ID list failed.", lnk_file); + goto err; + } + + /* Parse Shell item ID list */ + if (flag & 0x01) { + /* Get the length of SHITEMID */ + if (fread(&sflag, 2, 1, fd) != 1) { + error_report("Read %s length of SHITEMID failed.", lnk_file); + goto err; + } + + /* Skip SHITEMID */ + if (fseek(fd, sflag, SEEK_CUR)) { + error_report("Skip SHITEMID of %s failed.", lnk_file); + goto err; + } + } + + offset = ftell(fd); + if (offset == -1) { + error_report("ftell() of %s failed.", lnk_file); + goto err; + } + /* Read offset of local path */ + if (fseek(fd, offset + 0x10, SEEK_SET)) { + error_report("Seek offset of %s failed.", lnk_file); + goto err; + } + if (fread(&flag, 4, 1, fd) != 1) { + error_report("Read offset of %s failed.", lnk_file); + goto err; + } + /* Seek to the start of target file path */ + if (fseek(fd, flag + offset, SEEK_SET)) { + error_report("Seek target file path of %s failed.", lnk_file); + goto err; + } + + do { + if (fread(&uch, 1, 1, fd) != 1) { + error_report("Read target path of %s failed.", lnk_file); + goto err; + } + filepath[i++] = uch; + } while ((i <= PATH_MAX) && (uch != '\0')); + + fclose(fd); + return 0; + +err: + fclose(fd); + return -1; +} +#endif + +static long get_inode(const char *filename) +{ + #ifdef _WIN32 + char pbuf[PATH_MAX + 1], *p; + long inode; + struct stat sbuf; + char path[PATH_MAX + 1]; + int len; + + /* If filename contains .lnk, it's a shortcuts. Target file + * need to be parsed. + */ + len = strlen(filename); + if (len > 4 && !strcmp(filename + len - 4, ".lnk")) { + if (get_lnk_target_file(filename, path)) { + error_report("Parse .lnk file %s failed.", filename); + return -1; + } + } else { + memcpy(path, filename, sizeof(filename)); + } + + if (stat(path, &sbuf) == -1) { + error_report("get file %s stat error.", path); + return -1; + } + if (GetFullPathName(path, PATH_MAX + 1, pbuf, &p) != 0) { + inode = 11003; + for (p = pbuf; *p != '\0'; p++) { + inode = inode * 31 + *(unsigned char *)p; + } + return (inode * 911) & 0x7FFF; + } + + return -1; + #else + struct stat sbuf; + + if (stat(filename, &sbuf) == -1) { + error_report("get file %s stat error.", filename); + return -1; + } + + return sbuf.st_ino; + #endif +} + static gboolean str_equal_func(gconstpointer a, gconstpointer b) { return strcmp(a, b) == 0; @@ -4456,7 +4597,6 @@ bool bdrv_backing_file_loop_check(const char *filename, const char *fmt, GHashTable *inodes; BlockDriverState *bs; BlockDriver *drv; - struct stat sbuf; long inode = 0; int ret; char fbuf[1024]; @@ -4468,11 +4608,11 @@ bool bdrv_backing_file_loop_check(const char *filename, const char *fmt, if (access(filename, F_OK)) { inode = -1; } else { - if (stat(filename, &sbuf) == -1) { - error_report("Get file %s stat failed.", filename); + inode = get_inode(filename); + if (inode == -1) { + error_report("Get file %s inode failed.", filename); goto err; } - inode = (long)sbuf.st_ino; } filename = backing_file; @@ -4481,11 +4621,11 @@ bool bdrv_backing_file_loop_check(const char *filename, const char *fmt, } while (filename && (filename[0] != '\0')) { - if (stat(filename, &sbuf) == -1) { - error_report("Get file %s stat failed.", filename); - goto err; + inode = get_inode(filename); + if (inode == -1) { + error_report("Get file %s inode failed.", filename); + goto err; } - inode = (long)sbuf.st_ino; if (g_hash_table_lookup_extended(inodes, &inode, NULL, NULL)) { error_report("Backing file '%s' creates an infinite loop.", -- 1.8.1.4