Author: pschweitzer
Date: Tue Sep  8 20:04:04 2015
New Revision: 69127

URL: http://svn.reactos.org/svn/reactos?rev=69127&view=rev
Log:
[FATTEN]
Let's try not to leak memory...
So, close what's been opened in case of an error
And in a general way, close files we opened

CORE-10140 #resolve #comment Fixed with r69127

Modified:
    trunk/reactos/tools/fatten/fatfs/diskio.c
    trunk/reactos/tools/fatten/fatfs/diskio.h
    trunk/reactos/tools/fatten/fatten.c

Modified: trunk/reactos/tools/fatten/fatfs/diskio.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/tools/fatten/fatfs/diskio.c?rev=69127&r1=69126&r2=69127&view=diff
==============================================================================
--- trunk/reactos/tools/fatten/fatfs/diskio.c   [iso-8859-1] (original)
+++ trunk/reactos/tools/fatten/fatfs/diskio.c   [iso-8859-1] Tue Sep  8 
20:04:04 2015
@@ -61,6 +61,24 @@
 }
 
 
+
+/*-----------------------------------------------------------------------*/
+/* Cleanup a Drive                                                       */
+/*-----------------------------------------------------------------------*/
+
+VOID disk_cleanup(
+    BYTE pdrv          /* Physical drive nmuber (0..) */
+    )
+{
+    if (pdrv < driveHandleCount)
+    {
+        if (driveHandle[pdrv] != NULL)
+        {
+            fclose(driveHandle[pdrv]);
+            driveHandle[pdrv] = NULL;
+        }
+    }
+}
 
 /*-----------------------------------------------------------------------*/
 /* Read Sector(s)                                                        */

Modified: trunk/reactos/tools/fatten/fatfs/diskio.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/tools/fatten/fatfs/diskio.h?rev=69127&r1=69126&r2=69127&view=diff
==============================================================================
--- trunk/reactos/tools/fatten/fatfs/diskio.h   [iso-8859-1] (original)
+++ trunk/reactos/tools/fatten/fatfs/diskio.h   [iso-8859-1] Tue Sep  8 
20:04:04 2015
@@ -34,6 +34,7 @@
 
 DSTATUS disk_initialize (BYTE pdrv);
 DSTATUS disk_status (BYTE pdrv);
+VOID disk_cleanup (BYTE pdrv);
 DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
 DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
 DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);

Modified: trunk/reactos/tools/fatten/fatten.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/tools/fatten/fatten.c?rev=69127&r1=69126&r2=69127&view=diff
==============================================================================
--- trunk/reactos/tools/fatten/fatten.c [iso-8859-1] (original)
+++ trunk/reactos/tools/fatten/fatten.c [iso-8859-1] Tue Sep  8 20:04:04 2015
@@ -67,8 +67,8 @@
 
 #define NEED_PARAMS(_min_,_max_) \
     do {\
-        if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); 
goto print_help; } \
-        if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); 
goto print_help; } \
+        if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); 
PRINT_HELP_AND_QUIT(); } \
+        if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); 
PRINT_HELP_AND_QUIT(); } \
     } while(0)
 
 int need_mount()
@@ -90,272 +90,12 @@
     do { ret = need_mount(); if(ret) \
     {\
         printf("Error: could not mount image file '%s' (%d). \n", 
imageFileName, ret); \
-        goto print_help; \
+        PRINT_HELP_AND_QUIT(); \
     } } while(0)
 
-int main(int oargc, char* oargv[])
-{
-    int ret;
-    int    argc = oargc - 1;
-    char** argv = oargv + 1;
-
-    // first parameter must be the image file.
-    if (argc == 0)
-    {
-        goto print_help;
-    }
-
-    if (is_command(argv[0]))
-    {
-        printf("Error: first parameter must be a filename, found '%s' instead. 
\n", argv[0]);
-        goto print_help;
-    }
-
-    imageFileName = argv[0];
-
-    if (disk_initialize(0))
-    {
-        printf("Error: could not open image file '%s'. \n", imageFileName);
-        goto print_help;
-    }
-
-    argc--;
-    argv++;
-
-    while (argc > 0)
-    {
-        char *parg = *argv;
-        int nargs = 0;
-        int i = 0;
-
-        if (!is_command(parg))
-        {
-            printf("Error: Expected a command, found '%s' instead. \n", parg);
-            goto print_help;
-        }
-
-        parg++;
-        argv++;
-        argc--;
-
-        // find next command, to calculare number of args
-        while ((argv[i] != NULL) && !is_command(argv[i++]))
-            nargs++;
-
-        if (strcmp(parg, "format") == 0)
-        {
-            // NOTE: The fs driver detects which FAT format fits best based on 
size
-            int sectors;
-
-            NEED_PARAMS(1, 1);
-
-            NEED_MOUNT();
-
-            // Arg 1: number of sectors
-            sectors = atoi(argv[0]);
-
-            if (sectors <= 0)
-            {
-                printf("Error: Sectors must be > 0\n");
-                return 1;
-            }
-
-            disk_ioctl(0, SET_SECTOR_COUNT, &sectors);
-
-            ret = f_mkfs("0:", 1, 4096);
-            if (ret)
-            {
-                printf("ERROR: Formatting drive: %d.\n", ret);
-                goto print_help;
-            }
-        }
-        else if (strcmp(parg, "boot") == 0)
-        {
-            NEED_PARAMS(1, 1);
-
-            // Arg 1: boot file
-            printf("Not Implemented.");
-        }
-        else if (strcmp(parg, "add") == 0)
-        {
-            FILE* fe;
-            FIL   fv = { 0 };
-            UINT rdlen = 0;
-            UINT wrlen = 0;
-
-            NEED_PARAMS(2, 2);
-
-            NEED_MOUNT();
-
-            // Arg 1: external file to add
-            // Arg 2: virtual filename
-
-            fe = fopen(argv[0], "rb");
-
-            if (!fe)
-            {
-                printf("Error: unable to open external file '%s' for 
reading.", argv[0]);
-                return 1;
-            }
-
-            if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
-            {
-                printf("Error: unable to open file '%s' for writing.", 
argv[1]);
-                return 1;
-            }
-
-            while ((rdlen = fread(buff, 1, 32768, fe)) > 0)
-            {
-                f_write(&fv, buff, rdlen, &wrlen);
-            }
-
-            fclose(fe);
-            f_close(&fv);
-        }
-        else if (strcmp(parg, "extract") == 0)
-        {
-            FIL   fe = { 0 };
-            FILE* fv;
-            UINT rdlen = 0;
-            UINT wrlen = 0;
-
-            NEED_PARAMS(2, 2);
-
-            NEED_MOUNT();
-
-            // Arg 1: virtual file to extract
-            // Arg 2: external filename
-
-            if (f_open(&fe, argv[0], FA_READ))
-            {
-                printf("Error: unable to open file '%s' for reading.", 
argv[0]);
-                return 1;
-            }
-
-            fv = fopen(argv[1], "wb");
-
-            if (!fv)
-            {
-                printf("Error: unable to open external file '%s' for 
writing.", argv[1]);
-                return 1;
-            }
-
-            while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
-            {
-                fwrite(buff, 1, rdlen, fv);
-            }
-
-            f_close(&fe);
-            fclose(fv);
-        }
-        else if (strcmp(parg, "move") == 0)
-        {
-            NEED_PARAMS(2, 2);
-
-            NEED_MOUNT();
-            // Arg 1: src path & filename
-            // Arg 2: new path & filename
-
-            if (f_rename(argv[0], argv[1]))
-                printf("Error moving/renaming '%s' to '%s'", argv[0], argv[1]);
-        }
-        else if (strcmp(parg, "copy") == 0)
-        {
-            FIL fe = { 0 };
-            FIL fv = { 0 };
-            UINT rdlen = 0;
-            UINT wrlen = 0;
-
-            NEED_PARAMS(2, 2);
-
-            NEED_MOUNT();
-            // Arg 1: src path & filename
-            // Arg 2: new path & filename
-
-            if (f_open(&fe, argv[0], FA_READ))
-            {
-                printf("Error: unable to open file '%s' for reading.", 
argv[0]);
-                return 1;
-            }
-            if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
-            {
-                printf("Error: unable to open file '%s' for writing.", 
argv[1]);
-                return 1;
-            }
-
-            while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
-            {
-                f_write(&fv, buff, rdlen, &wrlen);
-            }
-
-            f_close(&fe);
-            f_close(&fv);
-        }
-        else if (strcmp(parg, "mkdir") == 0)
-        {
-            NEED_PARAMS(1, 1);
-
-            NEED_MOUNT();
-
-            // Arg 1: folder path
-            f_mkdir(argv[0]);
-        }
-        else if (strcmp(parg, "delete") == 0)
-        {
-            NEED_PARAMS(1, 1);
-
-            NEED_MOUNT();
-
-            // Arg 1: file/folder path (cannot delete non-empty folders)
-            f_unlink(argv[0]);
-        }
-        else if (strcmp(parg, "list") == 0)
-        {
-            char* root = "/";
-            DIR dir = { 0 };
-            FILINFO info = { 0 };
-            char lfname[257];
-
-            NEED_PARAMS(0, 1);
-
-            // Arg 1: folder path (optional)
-
-            if (nargs == 1)
-            {
-                root = argv[0];
-            }
-
-            if (f_opendir(&dir, root))
-            {
-                printf("Error opening directory '%s'.\n", root);
-                return 1;
-            }
-
-            printf("Listing directory contents of: %s\n", root);
-
-            info.lfname = lfname;
-            info.lfsize = 256;
-            while ((!f_readdir(&dir, &info)) && (strlen(info.fname) > 0))
-            {
-                if (strlen(info.lfname) > 0)
-                    printf(" - %s (%s)\n", info.lfname, info.fname);
-                else
-                    printf(" - %s\n", info.fname);
-            }
-        }
-        else
-        {
-            printf("Error: Unknown or invalid command: %s\n", argv[-1]);
-            goto print_help;
-        }
-        argv += nargs;
-        argc -= nargs;
-    }
-
-    return 0;
-
-print_help:
-    printf("Syntax: %s image_file [list of commands]\n\n", oargv[0]);
+void print_help(char const * const name)
+{
+    printf("Syntax: %s image_file [list of commands]\n\n", name);
     printf("Commands: [Note: both '/' and '-' are accepted as command 
prefixes.] \n");
     printf("    /format <sectors> [<filesystem>]         Formats the disk 
image.\n");
     printf("    /boot <sector file>          Writes a new boot sector.\n");
@@ -369,7 +109,291 @@
     printf("    /rmdir <src path> <new path> Creates a directory.\n");
     printf("    /list [<pattern>]            Lists files a directory (defaults 
to root).\n");
     //printf("    /recursive                   Enables recursive processing 
for directories.\n");
-
-    return 1;
-}
-
+}
+
+#define PRINT_HELP_AND_QUIT() \
+    do { \
+        ret = 1; \
+        print_help(oargv[0]); \
+        goto exit; \
+    } while (0)
+
+int main(int oargc, char* oargv[])
+{
+    int ret;
+    int    argc = oargc - 1;
+    char** argv = oargv + 1;
+
+    // first parameter must be the image file.
+    if (argc == 0)
+    {
+        PRINT_HELP_AND_QUIT();
+    }
+
+    if (is_command(argv[0]))
+    {
+        printf("Error: first parameter must be a filename, found '%s' instead. 
\n", argv[0]);
+        PRINT_HELP_AND_QUIT();
+    }
+
+    imageFileName = argv[0];
+
+    if (disk_initialize(0))
+    {
+        printf("Error: could not open image file '%s'. \n", imageFileName);
+        PRINT_HELP_AND_QUIT();
+    }
+
+    argc--;
+    argv++;
+
+    while (argc > 0)
+    {
+        char *parg = *argv;
+        int nargs = 0;
+        int i = 0;
+
+        if (!is_command(parg))
+        {
+            printf("Error: Expected a command, found '%s' instead. \n", parg);
+            PRINT_HELP_AND_QUIT();
+        }
+
+        parg++;
+        argv++;
+        argc--;
+
+        // find next command, to calculare number of args
+        while ((argv[i] != NULL) && !is_command(argv[i++]))
+            nargs++;
+
+        if (strcmp(parg, "format") == 0)
+        {
+            // NOTE: The fs driver detects which FAT format fits best based on 
size
+            int sectors;
+
+            NEED_PARAMS(1, 1);
+
+            NEED_MOUNT();
+
+            // Arg 1: number of sectors
+            sectors = atoi(argv[0]);
+
+            if (sectors <= 0)
+            {
+                printf("Error: Sectors must be > 0\n");
+                ret = 1;
+                goto exit;
+            }
+
+            disk_ioctl(0, SET_SECTOR_COUNT, &sectors);
+
+            ret = f_mkfs("0:", 1, 4096);
+            if (ret)
+            {
+                printf("ERROR: Formatting drive: %d.\n", ret);
+                PRINT_HELP_AND_QUIT();
+            }
+        }
+        else if (strcmp(parg, "boot") == 0)
+        {
+            NEED_PARAMS(1, 1);
+
+            // Arg 1: boot file
+            printf("Not Implemented.");
+        }
+        else if (strcmp(parg, "add") == 0)
+        {
+            FILE* fe;
+            FIL   fv = { 0 };
+            UINT rdlen = 0;
+            UINT wrlen = 0;
+
+            NEED_PARAMS(2, 2);
+
+            NEED_MOUNT();
+
+            // Arg 1: external file to add
+            // Arg 2: virtual filename
+
+            fe = fopen(argv[0], "rb");
+
+            if (!fe)
+            {
+                printf("Error: unable to open external file '%s' for 
reading.", argv[0]);
+                ret = 1;
+                goto exit;
+            }
+
+            if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
+            {
+                printf("Error: unable to open file '%s' for writing.", 
argv[1]);
+                fclose(fe);
+                ret = 1;
+                goto exit;
+            }
+
+            while ((rdlen = fread(buff, 1, 32768, fe)) > 0)
+            {
+                f_write(&fv, buff, rdlen, &wrlen);
+            }
+
+            fclose(fe);
+            f_close(&fv);
+        }
+        else if (strcmp(parg, "extract") == 0)
+        {
+            FIL   fe = { 0 };
+            FILE* fv;
+            UINT rdlen = 0;
+            UINT wrlen = 0;
+
+            NEED_PARAMS(2, 2);
+
+            NEED_MOUNT();
+
+            // Arg 1: virtual file to extract
+            // Arg 2: external filename
+
+            if (f_open(&fe, argv[0], FA_READ))
+            {
+                printf("Error: unable to open file '%s' for reading.", 
argv[0]);
+                ret = 1;
+                goto exit;
+            }
+
+            fv = fopen(argv[1], "wb");
+
+            if (!fv)
+            {
+                printf("Error: unable to open external file '%s' for 
writing.", argv[1]);
+                f_close(&fe);
+                ret = 1;
+                goto exit;
+            }
+
+            while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
+            {
+                fwrite(buff, 1, rdlen, fv);
+            }
+
+            f_close(&fe);
+            fclose(fv);
+        }
+        else if (strcmp(parg, "move") == 0)
+        {
+            NEED_PARAMS(2, 2);
+
+            NEED_MOUNT();
+            // Arg 1: src path & filename
+            // Arg 2: new path & filename
+
+            if (f_rename(argv[0], argv[1]))
+                printf("Error moving/renaming '%s' to '%s'", argv[0], argv[1]);
+        }
+        else if (strcmp(parg, "copy") == 0)
+        {
+            FIL fe = { 0 };
+            FIL fv = { 0 };
+            UINT rdlen = 0;
+            UINT wrlen = 0;
+
+            NEED_PARAMS(2, 2);
+
+            NEED_MOUNT();
+            // Arg 1: src path & filename
+            // Arg 2: new path & filename
+
+            if (f_open(&fe, argv[0], FA_READ))
+            {
+                printf("Error: unable to open file '%s' for reading.", 
argv[0]);
+                ret = 1;
+                goto exit;
+            }
+            if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
+            {
+                printf("Error: unable to open file '%s' for writing.", 
argv[1]);
+                f_close(&fe);
+                ret = 1;
+                goto exit;
+            }
+
+            while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
+            {
+                f_write(&fv, buff, rdlen, &wrlen);
+            }
+
+            f_close(&fe);
+            f_close(&fv);
+        }
+        else if (strcmp(parg, "mkdir") == 0)
+        {
+            NEED_PARAMS(1, 1);
+
+            NEED_MOUNT();
+
+            // Arg 1: folder path
+            f_mkdir(argv[0]);
+        }
+        else if (strcmp(parg, "delete") == 0)
+        {
+            NEED_PARAMS(1, 1);
+
+            NEED_MOUNT();
+
+            // Arg 1: file/folder path (cannot delete non-empty folders)
+            f_unlink(argv[0]);
+        }
+        else if (strcmp(parg, "list") == 0)
+        {
+            char* root = "/";
+            DIR dir = { 0 };
+            FILINFO info = { 0 };
+            char lfname[257];
+
+            NEED_PARAMS(0, 1);
+
+            // Arg 1: folder path (optional)
+
+            if (nargs == 1)
+            {
+                root = argv[0];
+            }
+
+            if (f_opendir(&dir, root))
+            {
+                printf("Error opening directory '%s'.\n", root);
+                ret = 1;
+                goto exit;
+            }
+
+            printf("Listing directory contents of: %s\n", root);
+
+            info.lfname = lfname;
+            info.lfsize = 256;
+            while ((!f_readdir(&dir, &info)) && (strlen(info.fname) > 0))
+            {
+                if (strlen(info.lfname) > 0)
+                    printf(" - %s (%s)\n", info.lfname, info.fname);
+                else
+                    printf(" - %s\n", info.fname);
+            }
+        }
+        else
+        {
+            printf("Error: Unknown or invalid command: %s\n", argv[-1]);
+            PRINT_HELP_AND_QUIT();
+        }
+        argv += nargs;
+        argc -= nargs;
+    }
+
+    ret = 0;
+
+exit:
+
+    disk_cleanup(0);
+
+    return ret;
+}
+


Reply via email to