diff mbox

[V4,2/5] block: Add WIN32 platform support for backing_file_loop_check()

Message ID 1374480349-11412-3-git-send-email-cngesaint@gmail.com
State New
Headers show

Commit Message

Xu Wang July 22, 2013, 8:05 a.m. UTC
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 <cngesaint@gmail.com>
---
 block.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 152 insertions(+), 8 deletions(-)

Comments

Fam Zheng July 22, 2013, 8:40 a.m. UTC | #1
On Mon, 07/22 04:05, 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 <cngesaint@gmail.com>
> ---
>  block.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 152 insertions(+), 8 deletions(-)
> 
> diff --git a/block.c b/block.c
> index aea653d..3e23b67 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,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 <= MAX_PATH_LEN) && (uch != '\0'));
> +
> +    fclose(fd);
> +    return 0;
> +
> +err:
> +    fclose(fd);
> +    return -1;
> +}
> +#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 ((len > 4) && (strcmp(filename + len - 4, ".lnk"))) {

Parenthesis unnecessary for (len > 4), and I think you want

    && (strcmp() == 0)

> +        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;
> @@ -4456,7 +4601,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 +4612,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 +4625,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
>
diff mbox

Patch

diff --git a/block.c b/block.c
index aea653d..3e23b67 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,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 <= MAX_PATH_LEN) && (uch != '\0'));
+
+    fclose(fd);
+    return 0;
+
+err:
+    fclose(fd);
+    return -1;
+}
+#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 ((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, 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;
@@ -4456,7 +4601,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 +4612,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 +4625,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.",