]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - lib/blkid/save.c
AOSP: build mke2fs for windows using android mingw library
[thirdparty/e2fsprogs.git] / lib / blkid / save.c
index 5a8771e78286da4b6e26a1c1a7dd008715e5d1f2..036f07a4a5a5b09b95fefcb1f534e2502f0df96d 100644 (file)
@@ -10,6 +10,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #endif
 #include "blkidP.h"
 
-#ifdef DEBUG_SAVE
-#define DBG(x) x
-#else
-#define DBG(x)
+#ifdef _WIN32
+#include "windows.h"
 #endif
 
 static int save_dev(blkid_dev dev, FILE *file)
 {
        struct list_head *p;
 
-       if (!dev)
+       if (!dev || dev->bid_name[0] != '/')
                return 0;
 
-       DBG(printf("device %s, type %s\n", dev->bid_name, dev->bid_type));
+       DBG(DEBUG_SAVE,
+           printf("device %s, type %s\n", dev->bid_name, dev->bid_type ?
+                  dev->bid_type : "(null)"));
 
        fprintf(file,
-               "<device TYPE=\"%s\" DEVNO=\"0x%04lx\" ID=\"%d\" TIME=\"%lu\"",
-               dev->bid_type, (unsigned long) dev->bid_devno,
-               dev->bid_id, dev->bid_time);
+               "<device DEVNO=\"0x%04lx\" TIME=\"%ld\"",
+               (unsigned long) dev->bid_devno, (long) dev->bid_time);
+       if (dev->bid_pri)
+               fprintf(file, " PRI=\"%d\"", dev->bid_pri);
        list_for_each(p, &dev->bid_tags) {
                blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
-               if (strcmp(tag->bit_name, "TYPE"))
-                       fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
+               fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
        }
        fprintf(file, ">%s</device>\n", dev->bid_name);
 
@@ -66,61 +67,65 @@ int blkid_flush_cache(blkid_cache cache)
        const char *filename;
        FILE *file = NULL;
        int fd, ret = 0;
+       struct stat st;
 
        if (!cache)
                return -BLKID_ERR_PARAM;
 
        if (list_empty(&cache->bic_devs) ||
            !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
-               DBG(printf("empty cache, not saving\n"));
+               DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
                return 0;
        }
 
        filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
 
-       if (!strcmp(filename, "-"))
-               file = stdout;
-       else {
-               struct stat st;
-
-               /* If we can't write to the cache file, then don't even try */
-               if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
-                   (ret == 0 && access(filename, W_OK) < 0)) {
-                       DBG(printf("can't write to cache file %s\n", filename));
-                       return 0;
-               }
+       /* If we can't write to the cache file, then don't even try */
+       if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
+           (ret == 0 && access(filename, W_OK) < 0)) {
+               DBG(DEBUG_SAVE,
+                   printf("can't write to cache file %s\n", filename));
+               return 0;
+       }
 
-               /*
-                * Try and create a temporary file in the same directory so
-                * that in case of error we don't overwrite the cache file.
-                * If the cache file doesn't yet exist, it isn't a regular
-                * file (e.g. /dev/null or a socket), or we couldn't create
-                * a temporary file then we open it directly.
-                */
-               if (ret == 0 && S_ISREG(st.st_mode)) {
-                       tmp = malloc(strlen(filename) + 8);
-                       if (tmp) {
-                               sprintf(tmp, "%s-XXXXXX", filename);
-                               fd = mkstemp(tmp);
-                               if (fd >= 0) {
-                                       file = fdopen(fd, "w");
-                                       opened = tmp;
-                               }
-                               fchmod(fd, 0644);
+       /*
+        * Try and create a temporary file in the same directory so
+        * that in case of error we don't overwrite the cache file.
+        * If the cache file doesn't yet exist, it isn't a regular
+        * file (e.g. /dev/null or a socket), or we couldn't create
+        * a temporary file then we open it directly.
+        */
+       if (ret == 0 && S_ISREG(st.st_mode)) {
+               tmp = malloc(strlen(filename) + 8);
+               if (tmp) {
+                       mode_t save_umask = umask(022);
+                       sprintf(tmp, "%s-XXXXXX", filename);
+                       fd = mkstemp(tmp);
+                       umask(save_umask);
+                       if (fd >= 0) {
+                               file = fdopen(fd, "w");
+                               opened = tmp;
                        }
+#ifndef _WIN32
+                       fchmod(fd, 0644);
+#else
+                       chmod(tmp, 0644);
+#endif
                }
+       }
 
-               if (!file) {
-                       file = fopen(filename, "w");
-                       opened = filename;
-               }
+       if (!file) {
+               file = fopen(filename, "w");
+               opened = filename;
+       }
 
-               DBG(printf("cache file %s (really %s)\n", filename, opened));
+       DBG(DEBUG_SAVE,
+           printf("writing cache file %s (really %s)\n",
+                  filename, opened));
 
-               if (!file) {
-                       ret = errno;
-                       goto errout;
-               }
+       if (!file) {
+               ret = errno;
+               goto errout;
        }
 
        list_for_each(p, &cache->bic_devs) {
@@ -136,31 +141,31 @@ int blkid_flush_cache(blkid_cache cache)
                ret = 1;
        }
 
-       if (file != stdout) {
-               fclose(file);
-               if (opened != filename) {
-                       if (ret < 0) {
-                               unlink(opened);
-                               DBG(printf("unlinked temp cache %s\n", opened));
-                       } else {
-                               char *backup;
-
-                               backup = malloc(strlen(filename) + 5);
-                               if (backup) {
-                                       sprintf(backup, "%s.old", filename);
-                                       unlink(backup);
-                                       link(filename, backup);
-                                       free(backup);
-                               }
-                               rename(opened, filename);
-                               DBG(printf("moved temp cache %s\n", opened));
+       fclose(file);
+       if (opened != filename) {
+               if (ret < 0) {
+                       (void) unlink(opened);
+                       DBG(DEBUG_SAVE,
+                           printf("unlinked temp cache %s\n", opened));
+               } else {
+                       char *backup;
+
+                       backup = malloc(strlen(filename) + 5);
+                       if (backup) {
+                               sprintf(backup, "%s.old", filename);
+                               unlink(backup);
+                               (void) link(filename, backup);
+                               free(backup);
                        }
+                       if (rename(opened, filename) < 0)
+                               (void) unlink(opened);
+                       DBG(DEBUG_SAVE,
+                           printf("moved temp cache %s\n", opened));
                }
        }
 
 errout:
-       if (tmp)
-               free(tmp);
+       free(tmp);
        return ret;
 }
 
@@ -170,14 +175,16 @@ int main(int argc, char **argv)
        blkid_cache cache = NULL;
        int ret;
 
+       blkid_debug_mask = DEBUG_ALL;
        if (argc > 2) {
                fprintf(stderr, "Usage: %s [filename]\n"
                        "Test loading/saving a cache (filename)\n", argv[0]);
                exit(1);
        }
 
-       if ((cache = blkid_new_cache()) == NULL) {
-               fprintf(stderr, "%s: error creating cache\n", argv[0]);
+       if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
+               fprintf(stderr, "%s: error creating cache (%d)\n",
+                       argv[0], ret);
                exit(1);
        }
        if ((ret = blkid_probe_all(cache)) < 0) {
@@ -185,7 +192,7 @@ int main(int argc, char **argv)
                exit(1);
        }
        cache->bic_filename = blkid_strdup(argv[1]);
-       
+
        if ((ret = blkid_flush_cache(cache)) < 0) {
                fprintf(stderr, "error (%d) saving cache\n", ret);
                exit(1);