On Mon, 07/15 05:42, Xu Wang wrote: > 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 | 109 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 101 insertions(+), 8 deletions(-) > > diff --git a/block.c b/block.c > index c1d060d..a06bb08 100644 > --- a/block.c > +++ b/block.c > @@ -51,6 +51,10 @@ > > #define NOT_DONE 0x7fffffff /* used while emulated sync operation in > progress */ > > +#ifdef _WIN32 > +#define MAX_PATH_LEN 8192 > +#endif > + > typedef enum { > BDRV_REQ_COPY_ON_READ = 0x1, > BDRV_REQ_ZERO_WRITE = 0x2, > @@ -4433,6 +4437,96 @@ 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, offet; > + 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; > + } > + fread(&flag, 4, 1, fd); > + if (flag != 0x4c) { > + error_report("%s is not a lnk file.", lnk_file); > + fclose(fd); > + return -1; > + } > + fseek(fd, 0x14, SEEK_SET); > + fread(&flag, 4, 1, fd); > + fseek(fd, 0x4c, SEEK_SET); > + > + if (flag & 0x01) { > + fread(&sflag, 2, 1, fd); > + fseek(fd, sflag, SEEK_CUR); > + } > + > + offset = ftell(fd); > + fseek(fd, offset + 0x10, SEEK_SET); > + fread(&flag, 4, 1, fd); > + fseek(fd, flag + offset, SEEK_SET); > + > + do { > + fread(&uch, 1, 1, fd); > + filepath[i++] = uch; > + } while ((i <= MAX_PATH_LEN) && (uch != '\0')); > + > + fclose(fd); > + return 0; > +}
You didn't check ret value for fseek and fread, what if they are not succeesful? > +#endif > + > +static long get_inode(const char *filename) > +{ > + #ifdef _WIN32 > + char pbuf[MAX_PATH_LEN], *p; > + long inode; > + struct stat sbuf; > + char path[MAX_PATH_LEN]; > + int len; > + > + /* If filename contains .lnk, it's a shortcuts. Target file > + * need to be parsed. > + */ > + len = strlen(filename); > + if (strcmp(filename + len - 4, ".lnk")) { Don't read out of range: if filename is shorter than 4 chars, (filename + len - 4) points to address before filename. > + 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, MAX_PATH_LEN, 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; > @@ -4457,7 +4551,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]; > @@ -4469,11 +4562,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; > @@ -4482,11 +4575,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 > -- Fam