Back to index

lightning-sunbird  0.9+nobinonly
delayRemove.c
Go to the documentation of this file.
00001 #include <windows.h>
00002 #include <stdio.h>
00003 
00004 #define MAX_BUF 1024
00005 #define  LEN_DELAYED_DELETE_PREFIX lstrlen("\\??\\")
00006 
00007 void PrintSpecial(char *string, int stringBufSize);
00008 
00009 void PrintSpecial(char *string, int stringBufSize)
00010 {
00011   int i;
00012   int nullCount = 0;
00013 
00014   printf("\nlength: %d\n", stringBufSize);
00015   for(i = 0; i < stringBufSize; i++)
00016   {
00017     if(string[i] == '\0')
00018     {
00019       printf("{null}");
00020       nullCount++;
00021     }
00022     else
00023       printf("%c", string[i]);
00024   }
00025 }
00026 
00027 void RemoveDelayedDeleteFileEntries(const char *aPathToMatch)
00028 {
00029   HKEY  hkResult;
00030   DWORD dwErr;
00031   DWORD dwType = REG_NONE;
00032   DWORD oldMaxValueLen = 0;
00033   DWORD newMaxValueLen = 0;
00034   DWORD lenToEnd = 0;
00035   char  *multiStr = NULL;
00036   const char key[] = "SYSTEM\\CurrentControlSet\\Control\\Session Manager";
00037   const char name[] = "PendingFileRenameOperations";
00038   char  *pathToMatch;
00039   char  *lcName;
00040   char  *pName;
00041   char  *pRename;
00042   int   nameLen, renameLen;
00043 
00044   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ|KEY_WRITE, &hkResult) != ERROR_SUCCESS)
00045     return;
00046 
00047   dwErr = RegQueryValueEx(hkResult, name, 0, &dwType, NULL, &oldMaxValueLen);
00048   if (dwErr != ERROR_SUCCESS || oldMaxValueLen == 0 || dwType != REG_MULTI_SZ)
00049   {
00050     /* no value, no data, or wrong type */
00051     return;
00052   }
00053 
00054   multiStr = calloc(oldMaxValueLen, sizeof(BYTE));
00055   if (!multiStr)
00056     return;
00057 
00058   pathToMatch = strdup(aPathToMatch);
00059   if (!pathToMatch)
00060   {
00061       free(multiStr);
00062       return;
00063   }
00064 
00065   if (RegQueryValueEx(hkResult, name, 0, NULL, multiStr, &oldMaxValueLen) == ERROR_SUCCESS)
00066   {
00067        PrintSpecial(multiStr, oldMaxValueLen);
00068       // The registry value consists of name/newname pairs of null-terminated
00069       // strings, with a final extra null termination. We're only interested
00070       // in files to be deleted, which are indicated by a null newname.
00071       _mbslwr(pathToMatch);
00072       lenToEnd = newMaxValueLen = oldMaxValueLen;
00073       pName = multiStr;
00074       while(*pName && lenToEnd > 0)
00075       {
00076           // find the locations and lengths of the current pair. Count the
00077           // nulls,  we need to know how much data to skip or move
00078           nameLen = strlen(pName) + 1;
00079           pRename = pName + nameLen;
00080           renameLen = strlen(pRename) + 1;
00081 
00082           // How much remains beyond the current pair
00083           lenToEnd -= (nameLen + renameLen);
00084 
00085           if (*pRename == '\0')
00086           {
00087               // No new name, it's a delete. Is it the one we want?
00088               lcName = strdup(pName);
00089               if (lcName)
00090               {
00091                 _mbslwr(lcName);
00092                 if (strstr(lcName, pathToMatch))
00093                 {
00094                   // It's a match--
00095                   // delete this pair by moving the remainder on top
00096                   memmove(pName, pRename + renameLen, lenToEnd);
00097 
00098                   // update the total length to reflect the missing pair
00099                   newMaxValueLen -= (nameLen + renameLen);
00100 
00101                   // next pair is in place, continue w/out moving pName
00102                   continue;
00103                 }
00104               }
00105           }
00106           // on to the next pair
00107           pName = pRename + renameLen;
00108       }
00109 
00110       if (newMaxValueLen != oldMaxValueLen)
00111       {
00112           PrintSpecial(multiStr, newMaxValueLen);
00113           // We've deleted something, save the changed data
00114           RegSetValueEx(hkResult, name, 0, REG_MULTI_SZ, multiStr, newMaxValueLen);
00115           RegFlushKey(hkResult);
00116       }
00117   }
00118 
00119   RegCloseKey(hkResult);
00120   free(multiStr);
00121 }
00122 
00123 #if 0
00124 void RemoveDelayedDeleteFileEntries(const char *aPathToMatch)
00125 {
00126   HKEY  hkResult;
00127   DWORD dwErr;
00128   DWORD oldMaxValueLen = 0;
00129   DWORD newMaxValueLen = 0;
00130   int   lenToEnd = 0;
00131   char  buf[MAX_BUF];
00132   char  *pathToMatch = NULL;
00133   char  *multiStr = NULL;
00134   char  key[] = "SYSTEM\\CurrentControlSet\\Control\\Session Manager";
00135   char  name[] = "PendingFileRenameOperations";
00136   char  *op;
00137   char  *np;
00138   char  *beginPath;
00139   BOOL  done = FALSE;
00140 
00141   pathToMatch = strdup(aPathToMatch);
00142   if(!pathToMatch)
00143     return;
00144 
00145   if((dwErr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ|KEY_WRITE, &hkResult)) == ERROR_SUCCESS)
00146   {
00147     dwErr = RegQueryValueEx(hkResult, name, 0, NULL, NULL, &oldMaxValueLen);
00148     printf("maxValueLen0: %d\n", oldMaxValueLen);
00149 
00150     multiStr = calloc(oldMaxValueLen, sizeof(BYTE));
00151     dwErr = RegQueryValueEx(hkResult, name, 0, NULL, multiStr, &oldMaxValueLen);
00152     printf("maxValueLen1: %d\n", oldMaxValueLen);
00153 
00154     if(dwErr != ERROR_SUCCESS)
00155       printf("RegQueryValueEx() failed: %d\n", dwErr);
00156 
00157 
00158     PrintSpecial(multiStr, oldMaxValueLen);
00159 
00160     lenToEnd = newMaxValueLen = oldMaxValueLen;
00161     op = multiStr;
00162     if(op && (oldMaxValueLen > 4))
00163     {
00164       _mbslwr(pathToMatch);
00165       while(!done)
00166       {
00167         beginPath = op + LEN_DELAYED_DELETE_PREFIX; // skip the first 4 chars ('\\??\\') to get to the first char of path;
00168         np = beginPath + lstrlen(beginPath) + 1;
00169         lstrcpy(buf, beginPath);
00170         _mbslwr(buf);
00171         lenToEnd = lenToEnd - lstrlen(op) + 2 - LEN_DELAYED_DELETE_PREFIX; // reset the lenToEnd
00172         if(strstr(buf, pathToMatch))
00173         {
00174           if(*np == '\0') // delete found
00175           {
00176             newMaxValueLen = newMaxValueLen - lstrlen(op) + 2 - LEN_DELAYED_DELETE_PREFIX; // reset the newMaxValueLen
00177             memmove(op, np + 1, lenToEnd);
00178           }
00179           else // rename found.  do nothing for renaming
00180           {
00181             printf("len of np: %d\n", lstrlen(np));
00182             lenToEnd = lenToEnd - lstrlen(np); // reset the lenToEnd
00183             op = np + lstrlen(np) + 1;
00184           }
00185         }
00186         else if(*np != '\0')
00187         {
00188           lenToEnd = lenToEnd - lstrlen(np); // reset the lenToEnd
00189           op = np + lstrlen(np) + 1;
00190         }
00191         else
00192           op = np + 1;
00193 
00194         if(*op == '\0') // terminating NULL byte indicating end of MULTISZ
00195           done = TRUE;
00196       }
00197     }
00198 
00199     PrintSpecial(multiStr, newMaxValueLen);
00200 
00201     dwErr = RegSetValueEx(hkResult,
00202                           name,
00203                           0,
00204                           REG_MULTI_SZ,
00205                           multiStr,
00206                           newMaxValueLen);
00207     if(dwErr != ERROR_SUCCESS)
00208       printf("RegQueryValueEx() failed: %d\n", dwErr);
00209 
00210     if(multiStr)
00211       free(multiStr);
00212 
00213     RegFlushKey(hkResult);
00214     RegCloseKey(hkResult);
00215   }
00216 
00217   if(pathToMatch)
00218     free(pathToMatch);
00219 }
00220 #endif
00221 
00222 int main(argc, argv)
00223 {
00224     char buf[MAX_BUF];
00225     DWORD bufSize = sizeof(buf);
00226 
00227 //    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00228     MoveFileEx("c:\\program files\\aol communicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00229 //    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00230     MoveFileEx("c:\\prOGRAM fILES\\aol COMMUNicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00231 //    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00232     MoveFileEx("srcfiletoremane.txt", "dstfilerenamed.txt", MOVEFILE_DELAY_UNTIL_REBOOT);
00233 
00234 //    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00235     MoveFileEx("c:\\program files\\aol communicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00236 //    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00237     MoveFileEx("c:\\program files\\aol communicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00238 //    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
00239     MoveFileEx("srcfiletorename.txt", "dstfilerenamed.txt", MOVEFILE_DELAY_UNTIL_REBOOT);
00240 
00241     RemoveDelayedDeleteFileEntries("C:\\Program Files\\AOL Communicator\\");
00242 }