Split DMA controller in two
[qemu] / block.c
diff --git a/block.c b/block.c
index 885b700..82bbf60 100644 (file)
--- a/block.c
+++ b/block.c
@@ -56,21 +56,24 @@ static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
 static BlockDriverState *bdrv_first;
 static BlockDriver *first_drv;
 
-#ifdef _WIN32
-#define PATH_SEP '\\'
-#else
-#define PATH_SEP '/'
-#endif
-
 int path_is_absolute(const char *path)
 {
     const char *p;
+#ifdef _WIN32
+    /* specific case for names like: "\\.\d:" */
+    if (*path == '/' || *path == '\\')
+        return 1;
+#endif
     p = strchr(path, ':');
     if (p)
         p++;
     else
         p = path;
-    return (*p == PATH_SEP);
+#ifdef _WIN32
+    return (*p == '/' || *p == '\\');
+#else
+    return (*p == '/');
+#endif
 }
 
 /* if filename is absolute, just copy it to dest. Otherwise, build a
@@ -93,7 +96,15 @@ void path_combine(char *dest, int dest_size,
             p++;
         else
             p = base_path;
-        p1 = strrchr(base_path, PATH_SEP);
+        p1 = strrchr(base_path, '/');
+#ifdef _WIN32
+        {
+            const char *p2;
+            p2 = strrchr(base_path, '\\');
+            if (!p1 || p2 > p1)
+                p1 = p2;
+        }
+#endif
         if (p1)
             p1++;
         else
@@ -168,7 +179,10 @@ int bdrv_create(BlockDriver *drv,
 #ifdef _WIN32
 void get_tmp_filename(char *filename, int size)
 {
-    tmpnam(filename);
+    char temp_dir[MAX_PATH];
+    
+    GetTempPath(MAX_PATH, temp_dir);
+    GetTempFileName(temp_dir, "qem", 0, filename);
 }
 #else
 void get_tmp_filename(char *filename, int size)
@@ -182,11 +196,17 @@ void get_tmp_filename(char *filename, int size)
 #endif
 
 #ifdef _WIN32
+static int is_windows_drive_prefix(const char *filename)
+{
+    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
+             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
+            filename[1] == ':');
+}
+    
 static int is_windows_drive(const char *filename)
 {
-    if (((filename[0] >= 'a' && filename[0] <= 'z') ||
-         (filename[0] >= 'A' && filename[0] <= 'Z')) &&
-        filename[1] == ':' && filename[2] == '\0')
+    if (is_windows_drive_prefix(filename) && 
+        filename[2] == '\0')
         return 1;
     if (strstart(filename, "\\\\.\\", NULL) ||
         strstart(filename, "//./", NULL))
@@ -203,7 +223,8 @@ static BlockDriver *find_protocol(const char *filename)
     const char *p;
 
 #ifdef _WIN32
-    if (is_windows_drive(filename))
+    if (is_windows_drive(filename) ||
+        is_windows_drive_prefix(filename))
         return &bdrv_raw;
 #endif
     p = strchr(filename, ':');
@@ -301,8 +322,8 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
                BlockDriver *drv)
 {
     int ret, open_flags;
-    char tmp_filename[1024];
-    char backing_filename[1024];
+    char tmp_filename[PATH_MAX];
+    char backing_filename[PATH_MAX];
     
     bs->read_only = 0;
     bs->is_temporary = 0;
@@ -328,8 +349,9 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
         bdrv_delete(bs1);
         
         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
+        realpath(filename, backing_filename);
         if (bdrv_create(&bdrv_qcow2, tmp_filename, 
-                        total_size, filename, 0) < 0) {
+                        total_size, backing_filename, 0) < 0) {
             return -1;
         }
         filename = tmp_filename;
@@ -365,6 +387,8 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
     }
     if (ret < 0) {
         qemu_free(bs->opaque);
+        bs->opaque = NULL;
+        bs->drv = NULL;
         return ret;
     }
     if (drv->bdrv_getlength) {
@@ -381,7 +405,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
         if (!bs->backing_hd) {
         fail:
             bdrv_close(bs);
-            return -1;
+            return -ENOMEM;
         }
         path_combine(backing_filename, sizeof(backing_filename),
                      filename, bs->backing_file);
@@ -858,9 +882,12 @@ void bdrv_info(void)
             term_printf(" locked=%d", bs->locked);
         }
         if (bs->drv) {
-            term_printf(" file=%s", bs->filename);
-            if (bs->backing_file[0] != '\0')
-                term_printf(" backing_file=%s", bs->backing_file);
+            term_printf(" file=");
+           term_print_filename(bs->filename);
+            if (bs->backing_file[0] != '\0') {
+                term_printf(" backing_file=");
+               term_print_filename(bs->backing_file);
+           }
             term_printf(" ro=%d", bs->read_only);
             term_printf(" drv=%s", bs->drv->format_name);
             if (bs->encrypted)
@@ -983,7 +1010,11 @@ char *get_human_readable_size(char *buf, int buf_size, int64_t size)
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
 {
     char buf1[128], date_buf[128], clock_buf[128];
+#ifdef _WIN32
+    struct tm *ptm;
+#else
     struct tm tm;
+#endif
     time_t ti;
     int64_t secs;
 
@@ -993,11 +1024,15 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
     } else {
         ti = sn->date_sec;
-#ifndef _WIN32
+#ifdef _WIN32
+        ptm = localtime(&ti);
+        strftime(date_buf, sizeof(date_buf),
+                 "%Y-%m-%d %H:%M:%S", ptm);
+#else
         localtime_r(&ti, &tm);
-#endif
         strftime(date_buf, sizeof(date_buf),
                  "%Y-%m-%d %H:%M:%S", &tm);
+#endif
         secs = sn->vm_clock_nsec / 1000000000;
         snprintf(clock_buf, sizeof(clock_buf),
                  "%02d:%02d:%02d.%03d",