UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB) { HRESULT r = ERROR_FUNCTION_FAILED; LPWSTR szwDBPath = NULL, szwPersist = NULL; TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB); if( szDBPath ) { szwDBPath = strdupAtoW( szDBPath ); if( !szwDBPath ) goto end; } if( HIWORD(szPersist) ) { szwPersist = strdupAtoW( szPersist ); if( !szwPersist ) goto end; } else szwPersist = (LPWSTR)(DWORD_PTR)szPersist; r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB ); end: if( HIWORD(szPersist) ) msi_free( szwPersist ); msi_free( szwDBPath ); return r; }
/*********************************************************************** * msidbExportStream * * Exports a stream to a file with an .idb extension. * * Examples (note wildcard escape for *nix/bash): * msidb -d <pathtomsi>.msi -f <workdir> -x <streamname> * msidb -d <pathtomsi>.msi -f <workdir> -x data.cab **********************************************************************/ static BOOL msidbExportStream(LPCWSTR dbfile, LPCWSTR wdir, LPCWSTR streamName) { static const char ext[] = {'.', 'i', 'd', 'b', 0}; UINT r, len; MSIHANDLE dbhandle, streamListView, rec; char queryBuffer[100]; FILE *fp = 0; char *streamNameA = strdupWtoA(CP_ACP, streamName); char *wdirA = strdupWtoA(CP_ACP, wdir); char *streamFileA = 0; char streamPath[MAX_PATH]; char *dataBuffer; DWORD dataLen = 0; r = MsiOpenDatabaseW(dbfile, (LPCWSTR) MSIDBOPEN_READONLY, &dbhandle); if (r != ERROR_SUCCESS) return FALSE; sprintf(queryBuffer, "SELECT * FROM _Streams WHERE Name = '%s'", streamNameA); MsiDatabaseOpenView(dbhandle, queryBuffer, &streamListView); MsiViewExecute(streamListView, 0); r = MsiViewFetch(streamListView, &rec); if (r != ERROR_SUCCESS) return FALSE; if (MsiRecordReadStream(rec, 2, 0, &dataLen) != ERROR_SUCCESS) return FALSE; dataBuffer = malloc(dataLen); if (!dataBuffer) return FALSE; if (MsiRecordReadStream(rec, 2, dataBuffer, &dataLen) != ERROR_SUCCESS) return FALSE; len = strlen(streamNameA) + 5; streamFileA = malloc(len); if (streamFileA == NULL) return FALSE; strcpy(streamFileA, streamNameA); strcat(streamFileA, ext); strcpy(streamPath, wdirA); strcat(streamPath, PATH_DELIMITER); strcat(streamPath, streamFileA); fp = fopen(streamPath , "wb"); if (fp != NULL) { fwrite(dataBuffer, 1, dataLen, fp); fclose(fp); } free(streamFileA); MsiCloseHandle(rec); MsiViewClose(streamListView); MsiCloseHandle(streamListView); MsiCloseHandle(dbhandle); free(wdirA); free(streamNameA); return TRUE; }
static BOOL msidbExportStorage(LPCWSTR dbfile, LPCWSTR wdir, LPWSTR storageName) { UINT r, len; MSIHANDLE dbhandle, view, rec; char queryBuffer[100]; char *storageNameA = strdupWtoA(CP_ACP, storageName); char *wdirA = strdupWtoA(CP_ACP, wdir); char *storagePath = NULL; char *dataBuffer; DWORD dataLen = 0; FILE *fp = NULL; sprintf(queryBuffer, "SELECT * FROM _Storages WHERE Name = '%s'", storageNameA); r = MsiOpenDatabaseW(dbfile, (LPWSTR) MSIDBOPEN_READONLY, &dbhandle); if (r != ERROR_SUCCESS) return FALSE; MsiDatabaseOpenView(dbhandle, queryBuffer, &view); MsiViewExecute(view, 0); r = MsiViewFetch(view, &rec); if (r != ERROR_SUCCESS) return FALSE; if ((r = MsiRecordReadStream(rec, 2, 0, &dataLen)) != ERROR_SUCCESS) { return FALSE; } if ((dataBuffer = malloc(dataLen)) == NULL) return FALSE; if (MsiRecordReadStream(rec, 2, dataBuffer, &dataLen) != ERROR_SUCCESS) return FALSE; len = strlen(wdirA) + strlen(storageNameA) + 2; storagePath = malloc(len * sizeof(WCHAR)); if (storagePath == NULL) return FALSE; strcpy(storagePath, wdirA); strcat(storagePath, "/"); strcat(storagePath, storageNameA); fp = fopen(storagePath , "wb"); if (fp != NULL) { fwrite(dataBuffer, 1, dataLen, fp); fclose(fp); } free(storagePath); MsiCloseHandle(rec); MsiViewClose(view); MsiCloseHandle(view); MsiCloseHandle(dbhandle); free(storageNameA); free(wdirA); return TRUE; }
static BOOL msidbImportStorages(LPCWSTR dbfile, LPCWSTR wdir, LPWSTR storageNames[]) { static const WCHAR delim[] = {'/', 0}; UINT r, len, i; MSIHANDLE dbhandle, view, rec; WCHAR *storagePath = 0; char *query = "INSERT INTO _Storages (Name, Data) VALUES(?, ?)"; r = MsiOpenDatabaseW(dbfile, (LPWSTR) MSIDBOPEN_TRANSACT, &dbhandle); if (r != ERROR_SUCCESS) return FALSE; r = MsiDatabaseOpenView(dbhandle, query, &view); if (r != ERROR_SUCCESS) return FALSE; MsiViewExecute(view, 0); r = MsiViewFetch(view, &rec); if (r != ERROR_SUCCESS) return FALSE; for (i = 0; i < MAX_STORAGES && storageNames[i] != 0; ++i) { len = lstrlenW(wdir) + lstrlenW(storageNames[i]) + 2; storagePath = malloc(len * sizeof(WCHAR)); if (storagePath == NULL) return FALSE; lstrcpyW(storagePath, wdir); lstrcatW(storagePath, delim); lstrcatW(storagePath, storageNames[i]); rec = MsiCreateRecord(2); MsiRecordSetStringW(rec, 1, storageNames[i]); r = MsiRecordSetStreamW(rec, 2, storagePath); if (r != ERROR_SUCCESS) { return FALSE; } r = MsiViewExecute(view, rec); if (r != ERROR_SUCCESS) { return FALSE; } MsiCloseHandle(rec); } MsiViewClose(view); MsiCloseHandle(view); MsiDatabaseCommit(dbhandle); MsiCloseHandle(dbhandle); return TRUE; }
static BOOL msidbImportStreams(LPCWSTR dbfile, LPCWSTR wdir, LPWSTR streamNames[]) { UINT r, len; MSIHANDLE dbhandle, view, rec; int i = 0; WCHAR *streamPath = 0; char *query = "INSERT INTO _Streams (Name, Data) VALUES(?, ?)"; r = MsiOpenDatabaseW(dbfile, (LPWSTR) MSIDBOPEN_TRANSACT, &dbhandle); if (r != ERROR_SUCCESS) return FALSE; r = MsiDatabaseOpenView(dbhandle, query, &view); if (r != ERROR_SUCCESS) return FALSE; for (i = 0; i < MAX_STREAMS && streamNames[i] != NULL; i++) { len = lstrlenW(wdir) + lstrlenW(streamNames[i]) + 2; streamPath = malloc(len * sizeof(WCHAR)); if (streamPath == NULL) return FALSE; lstrcpyW(streamPath, wdir); lstrcatW(streamPath, PATH_DELIMITERW); lstrcatW(streamPath, streamNames[i]); rec = MsiCreateRecord(2); MsiRecordSetStringW(rec, 1, streamNames[i]); r = MsiRecordSetStreamW(rec, 2, streamPath); if (r != ERROR_SUCCESS) { return FALSE; } r = MsiViewExecute(view, rec); if (r != ERROR_SUCCESS) { return FALSE; } MsiCloseHandle(rec); } MsiViewClose(view); MsiCloseHandle(view); MsiDatabaseCommit(dbhandle); MsiCloseHandle(dbhandle); return TRUE; }
/*********************************************************************** * msidbExportTables * * Takes a list of tables or '*' (for all) to export to text archive * files in specified folder * * For each table, a file called <tablename>.idt is exported containing * tab separated ASCII. * * Examples (note wildcard escape for *nix/bash): * msidb -d <pathtomsi>.msi -f <workdir> -e \* * msidb -d <pathtomsi>.msi -f <workdir> -e File Directory Binary **********************************************************************/ static BOOL msidbExportTables(LPCWSTR dbfile, LPCWSTR wdir, LPWSTR tables[]) { static const WCHAR ext[] = {'.', 'i', 'd', 't', 0}; static const WCHAR all[] = {'*', 0}; UINT r, len; MSIHANDLE dbhandle, tableListView, rec; LPWSTR tableFile = 0; WCHAR tableName[MAX_TABLE_NAME]; DWORD size = sizeof(tableName) / sizeof(tableName[0]); int i = 0; r = MsiOpenDatabaseW(dbfile, (LPCWSTR) MSIDBOPEN_READONLY, &dbhandle); if (r != ERROR_SUCCESS) return FALSE; if (strcmpW(tables[0], all) == 0) { r = MsiDatabaseOpenView(dbhandle, "SELECT Name FROM _Tables", &tableListView); r = MsiViewExecute(tableListView, 0); r = MsiViewFetch(tableListView, &rec); while (r == ERROR_SUCCESS) { size = sizeof(tableName) / sizeof(tableName[0]); r = MsiRecordGetStringW(rec, 1, tableName, &size); if (r == ERROR_SUCCESS) { len = lstrlenW(tableName) + 5; tableFile = malloc(len * sizeof (WCHAR)); if (tableFile == NULL) return FALSE; lstrcpyW(tableFile, tableName); lstrcatW(tableFile, ext); r = MsiDatabaseExportW(dbhandle, tableName, wdir, tableFile); free(tableFile); MsiCloseHandle(rec); } r = MsiViewFetch(tableListView, &rec); } MsiViewClose(tableListView); MsiCloseHandle(tableListView); } else { for (i = 0; i < MAX_TABLES && tables[i] != 0; ++i) { len = lstrlenW(tables[i]) + 5; tableFile = malloc(len * sizeof (WCHAR)); if (tableFile == NULL) return FALSE; lstrcpyW(tableFile, tables[i]); lstrcatW(tableFile, ext); r = MsiDatabaseExportW(dbhandle, tables[i], wdir, tableFile); free(tableFile); } } MsiCloseHandle(dbhandle); return TRUE; }
/*********************************************************************** * msidbImportTables * * Takes a list of tables or '*' (for all) to import from text archive * files in specified folder * * For each table, a file called <tablename>.idt is imported containing * tab separated ASCII. * * Examples (note wildcard escape for *nix/bash): * msidb -d <pathtomsi>.msi -f <workdir> -i \* * msidb -d <pathtomsi>.msi -f <workdir> -i File Directory Binary **********************************************************************/ static BOOL msidbImportTables(LPCWSTR dbfile, LPCWSTR wdir, LPWSTR tables[], BOOL create) { static const WCHAR ext[] = {'.', 'i', 'd', 't', 0}; static const WCHAR all[] = {'*', 0}; UINT r, len; char *dirNameA; char *fileName; DIR *dir; struct dirent *ent; int i = 0; MSIHANDLE dbhandle; LPWSTR tableFile = 0; LPCWSTR oFlag = (LPCWSTR) MSIDBOPEN_TRANSACT; if (create == TRUE) oFlag = (LPCWSTR) MSIDBOPEN_CREATE; r = MsiOpenDatabaseW(dbfile, oFlag, &dbhandle); if (r != ERROR_SUCCESS) { return FALSE; } if (strcmpW(tables[0], all) == 0) { dirNameA = strdupWtoA(CP_ACP, wdir); dir = opendir(dirNameA); if (dir) { while ((ent = readdir(dir)) != NULL) { if (ent->d_type != DT_REG) continue; fileName = ent->d_name; if (strcmp(fileName+strlen(fileName)-4*sizeof(fileName[0]), ".idt") != 0) continue; if (strcmp(fileName, ".") == 0 || strcmp(fileName, "..") == 0) continue; tableFile = strdupAtoW(CP_ACP, fileName); r = MsiDatabaseImportW(dbhandle, wdir, tableFile); free(tableFile); } } else return FALSE; closedir(dir); free(dirNameA); } else { for (i = 0; i < MAX_TABLES && tables[i] != 0; ++i) { len = lstrlenW(tables[i]) + 5; tableFile = malloc(len * sizeof (WCHAR)); if (tableFile == NULL) return FALSE; lstrcpyW(tableFile, tables[i]); lstrcatW(tableFile, ext); r = MsiDatabaseImportW(dbhandle, wdir, tableFile); free(tableFile); if (r != ERROR_SUCCESS) { return FALSE; } } } MsiDatabaseCommit(dbhandle); MsiCloseHandle(dbhandle); return TRUE; }
static void test_fieldzero(void) { MSIHANDLE hdb, hview, rec; CHAR buf[MAX_PATH]; LPCSTR query; DWORD sz; UINT r; rec = MsiCreateRecord(1); ok(rec != 0, "Expected a valid handle\n"); r = MsiRecordGetInteger(rec, 0); ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r); sz = MAX_PATH; lstrcpyA(buf, "apple"); r = MsiRecordGetStringA(rec, 0, buf, &sz); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf); ok(sz == 0, "Expectd 0, got %d\n", sz); r = MsiRecordIsNull(rec, 0); ok(r == TRUE, "Expected TRUE, got %d\n", r); r = MsiRecordGetInteger(rec, 1); ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r); r = MsiRecordSetInteger(rec, 1, 42); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiRecordGetInteger(rec, 0); ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r); sz = MAX_PATH; lstrcpyA(buf, "apple"); r = MsiRecordGetStringA(rec, 0, buf, &sz); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf); ok(sz == 0, "Expectd 0, got %d\n", sz); r = MsiRecordIsNull(rec, 0); ok(r == TRUE, "Expected TRUE, got %d\n", r); r = MsiRecordGetInteger(rec, 1); ok(r == 42, "Expected 42, got %d\n", r); r = MsiRecordSetStringA(rec, 1, "bologna"); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiRecordGetInteger(rec, 0); ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r); sz = MAX_PATH; lstrcpyA(buf, "apple"); r = MsiRecordGetStringA(rec, 0, buf, &sz); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf); ok(sz == 0, "Expectd 0, got %d\n", sz); r = MsiRecordIsNull(rec, 0); ok(r == TRUE, "Expected TRUE, got %d\n", r); sz = MAX_PATH; lstrcpyA(buf, "apple"); r = MsiRecordGetStringA(rec, 1, buf, &sz); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); ok(!lstrcmpA(buf, "bologna"), "Expected \"bologna\", got \"%s\"\n", buf); ok(sz == 7, "Expectd 7, got %d\n", sz); MsiCloseHandle(rec); r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb); ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n"); query = "CREATE TABLE `drone` ( " "`id` INT, `name` CHAR(32), `number` CHAR(32) " "PRIMARY KEY `id`)"; r = MsiDatabaseOpenViewA(hdb, query, &hview); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiViewExecute(hview, 0); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiViewClose(hview); ok(r == ERROR_SUCCESS, "MsiViewClose failed\n"); r = MsiCloseHandle(hview); ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n"); query = "INSERT INTO `drone` ( `id`, `name`, `number` )" "VALUES('1', 'Abe', '8675309')"; r = MsiDatabaseOpenViewA(hdb, query, &hview); ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n"); r = MsiViewExecute(hview, 0); ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n"); r = MsiViewClose(hview); ok(r == ERROR_SUCCESS, "MsiViewClose failed\n"); r = MsiCloseHandle(hview); ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n"); r = MsiDatabaseGetPrimaryKeysA(hdb, "drone", &rec); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiRecordGetInteger(rec, 0); ok(r == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", r); sz = MAX_PATH; lstrcpyA(buf, "apple"); r = MsiRecordGetStringA(rec, 0, buf, &sz); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); ok(!lstrcmpA(buf, "drone"), "Expected \"drone\", got \"%s\"\n", buf); ok(sz == 5, "Expectd 5, got %d\n", sz); r = MsiRecordIsNull(rec, 0); ok(r == FALSE, "Expected FALSE, got %d\n", r); MsiCloseHandle(rec); r = MsiDatabaseGetPrimaryKeysA(hdb, "nosuchtable", &rec); ok(r == ERROR_INVALID_TABLE, "Expected ERROR_INVALID_TABLE, got %d\n", r); query = "SELECT * FROM `drone` WHERE `id` = 1"; r = MsiDatabaseOpenViewA(hdb, query, &hview); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiViewExecute(hview, 0); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiViewFetch(hview, &rec); ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r); r = MsiRecordGetInteger(rec, 0); ok(r != MSI_NULL_INTEGER && r != 0, "Expected non-NULL value, got %d\n", r); r = MsiRecordIsNull(rec, 0); ok(r == FALSE, "Expected FALSE, got %d\n", r); r = MsiCloseHandle(hview); ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n"); MsiCloseHandle(rec); MsiCloseHandle(hdb); DeleteFileA(msifile); }