[WSD] Fix Installing WSD from a directory without write permissions.
authortzachid <tzachid@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Thu, 22 Jun 2006 07:01:21 +0000 (07:01 +0000)
committertzachid <tzachid@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Thu, 22 Jun 2006 07:01:21 +0000 (07:01 +0000)
git-svn-id: svn://openib.tc.cornell.edu/gen1/trunk@388 ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86

tools/wsdinstall/user/installsp.c

index c721210..a13bf39 100644 (file)
@@ -133,25 +133,71 @@ static pm_symbol_def_t  _pm_symbols[]=
 #define IBSP_PM_NUM_SYMBOLS  (sizeof(_pm_symbols)/sizeof(pm_symbol_def_t))\r
 #define IBSP_PM_LANGUAGE "009" /* good for English */\r
 \r
+static CHAR *\r
+_IBSPGenerateFileName(char *header,    char *file )\r
+{\r
+       DWORD size1, size;\r
+       CHAR *full_file_name;\r
+       int header_len = header == NULL ? 0 : strlen(header);\r
+\r
+       size = GetTempPath(0, NULL);\r
+       if (size == 0)\r
+       {\r
+               fprintf( stderr, "GetTempPath  failed\n" );\r
+               return NULL;\r
+       }\r
+       size1 = size + strlen(file) + header_len;\r
+       full_file_name = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, size1);\r
+       if ( full_file_name == NULL )\r
+       {\r
+               fprintf( stderr, "GetTempPath  failed\n" );\r
+               return NULL;\r
+       }\r
+       size1 = GetTempPath(size, full_file_name + header_len);\r
+       if (size != size1 + 1) \r
+       {\r
+               fprintf( stderr, "Very strange, GetTempPath  returned something different\n" );\r
+               HeapFree (GetProcessHeap (), 0, full_file_name);\r
+               return NULL;\r
+       }\r
+       if (header_len != 0)\r
+       {\r
+               memcpy(full_file_name, header, header_len);\r
+       }\r
+       strcat(full_file_name, file);\r
+       return full_file_name;\r
+}\r
+\r
 \r
 static DWORD\r
 _IBSPPerfmonIniFilesGenerate( void )\r
 {\r
        FILE    *f_handle;\r
        DWORD   num;\r
+       DWORD ret = ERROR_SUCCESS;\r
+       char *ibsp_pm_sym_file = NULL;\r
+       char *ibsp_pm_ini_file = NULL;\r
 \r
        /* create ".h" file first */\r
-       f_handle = fopen( IBSP_PM_SYM_H_FILE, "w+" );\r
+       ibsp_pm_sym_file = _IBSPGenerateFileName(NULL, IBSP_PM_SYM_H_FILE);\r
+       if( !ibsp_pm_sym_file )\r
+       {\r
+               fprintf( stderr, "_IBSPGenerateFileName  failed\n" );\r
+               ret = ERROR_NOT_ENOUGH_MEMORY;\r
+               goto Cleanup;\r
+       }\r
+\r
+       f_handle = fopen( ibsp_pm_sym_file, "w+" );\r
 \r
        if( !f_handle )\r
        {\r
-               fprintf(\r
-                       stderr, "Create Header file %s  failed\n", IBSP_PM_SYM_H_FILE );\r
-               return ERROR_FILE_INVALID;\r
+               fprintf( stderr, "Create Header file %s failed\n", ibsp_pm_sym_file );\r
+               ret = ERROR_FILE_INVALID;\r
+               goto Cleanup;\r
        }\r
 \r
        fprintf(\r
-               f_handle, "/* %s Generated by program */ \r\n", IBSP_PM_SYM_H_FILE );\r
+               f_handle, "/* %s Generated by program */ \r\n", ibsp_pm_sym_file );\r
                \r
        \r
        for( num = 0; num < IBSP_PM_NUM_SYMBOLS; num++ )\r
@@ -164,16 +210,24 @@ _IBSPPerfmonIniFilesGenerate( void )
        fclose( f_handle );\r
 \r
        /* create 'ini' file next */\r
-       f_handle = fopen( IBSP_PM_INI_FILE, "w+" );\r
+       ibsp_pm_ini_file = _IBSPGenerateFileName(NULL, IBSP_PM_INI_FILE);\r
+       if( !ibsp_pm_sym_file )\r
+       {\r
+               fprintf( stderr, "_IBSPGenerateFileName  failed\n" );\r
+               ret = ERROR_NOT_ENOUGH_MEMORY;\r
+               goto Cleanup;\r
+       }\r
+       f_handle = fopen( ibsp_pm_ini_file, "w+" );\r
 \r
        if( !f_handle )\r
        {\r
-               fprintf( stderr, "Create INI file %s  failed\n", IBSP_PM_INI_FILE );\r
-               return ERROR_FILE_INVALID;\r
+               fprintf( stderr, "Create INI file %s  failed\n", ibsp_pm_ini_file );\r
+               ret = ERROR_FILE_INVALID;\r
+               goto Cleanup;\r
        }\r
        \r
        fprintf( f_handle, "[info]\r\ndrivername=" IBSP_PM_SUBKEY_NAME\r
-               "\r\nsymbolfile=" IBSP_PM_SYM_H_FILE "\r\n\r\n" );\r
+               "\r\nsymbolfile=%s\r\n\r\n", ibsp_pm_sym_file );\r
        fprintf( f_handle,"[languages]\r\n" IBSP_PM_LANGUAGE\r
                "=language" IBSP_PM_LANGUAGE "\r\n\r\n" );\r
 \r
@@ -192,23 +246,60 @@ _IBSPPerfmonIniFilesGenerate( void )
        fflush( f_handle );\r
        fclose( f_handle );\r
 \r
-       return ERROR_SUCCESS;\r
+Cleanup:\r
+       if ( ibsp_pm_sym_file )\r
+       {\r
+               HeapFree (GetProcessHeap (), 0, ibsp_pm_sym_file);\r
+       }\r
+       if ( ibsp_pm_ini_file )\r
+       {\r
+               HeapFree (GetProcessHeap (), 0, ibsp_pm_ini_file);\r
+       }\r
+       return ret;\r
 }\r
 \r
 \r
 static void\r
 _IBSPPerfmonIniFilesRemove( void )\r
 {\r
-       if( !DeleteFile( IBSP_PM_INI_FILE ) )\r
+       char *ibsp_pm_sym_file = NULL;\r
+       char *ibsp_pm_ini_file = NULL;\r
+\r
+       ibsp_pm_sym_file = _IBSPGenerateFileName(NULL, IBSP_PM_SYM_H_FILE);\r
+       if( !ibsp_pm_sym_file )\r
+       {\r
+               fprintf( stderr, "_IBSPGenerateFileName  failed\n" );\r
+               goto Cleanup;\r
+       }\r
+\r
+       ibsp_pm_ini_file = _IBSPGenerateFileName(NULL, IBSP_PM_INI_FILE);\r
+       if( !ibsp_pm_sym_file )\r
+       {\r
+               fprintf( stderr, "_IBSPGenerateFileName  failed\n" );\r
+               goto Cleanup;\r
+       }\r
+\r
+       if( !DeleteFile( ibsp_pm_ini_file ) )\r
        {\r
                fprintf( stderr, "Delete file %s failed status %d\n",\r
-                       IBSP_PM_INI_FILE, GetLastError() );\r
+                       ibsp_pm_ini_file, GetLastError() );\r
        }\r
-       if( !DeleteFile( IBSP_PM_SYM_H_FILE ) )\r
+       if( !DeleteFile( ibsp_pm_sym_file ) )\r
        {\r
                fprintf( stderr,"Delete file %s failed status %d\n",\r
-                       IBSP_PM_SYM_H_FILE, GetLastError() );\r
+                       ibsp_pm_sym_file, GetLastError() );\r
+       }\r
+\r
+Cleanup:       \r
+       if ( ibsp_pm_sym_file )\r
+       {\r
+               HeapFree (GetProcessHeap (), 0, ibsp_pm_sym_file);\r
+       }\r
+       if ( ibsp_pm_ini_file )\r
+       {\r
+               HeapFree (GetProcessHeap (), 0, ibsp_pm_ini_file);\r
        }\r
+       \r
 }\r
 \r
 \r
@@ -322,20 +413,34 @@ static DWORD
 _IBSPPerfmonRegisterCounters( void )\r
 {\r
        DWORD status;\r
+       char *ibsp_pm_ini_file = NULL;\r
+\r
+       ibsp_pm_ini_file = _IBSPGenerateFileName("unused ", IBSP_PM_INI_FILE);\r
+       if( !ibsp_pm_ini_file )\r
+       {\r
+               fprintf( stderr, "_IBSPGenerateFileName  failed\n" );\r
+               status = ERROR_NOT_ENOUGH_MEMORY;\r
+               goto Cleanup;\r
+       }\r
+\r
        /*\r
         * format commandline string, as per SDK :\r
         *      Pointer to a null-terminated string that consists of one or more \r
         *      arbitrary letters, a space, and then the name of the initialization\r
         *      file.\r
         */\r
-       status = LoadPerfCounterTextStrings(\r
-               TEXT("unused ") TEXT(IBSP_PM_INI_FILE), TRUE );\r
+       status = LoadPerfCounterTextStrings( ibsp_pm_ini_file, TRUE );\r
        if( status != ERROR_SUCCESS )\r
        {\r
                status = GetLastError();\r
                fprintf( stderr,\r
                        "IBSPPerfmonRegisterCounters install failed status %d\n", status );\r
        }\r
+Cleanup:       \r
+       if ( ibsp_pm_ini_file )\r
+       {\r
+               HeapFree (GetProcessHeap (), 0, ibsp_pm_ini_file);\r
+       }\r
 \r
        return status;\r
 }\r