File: delayRemove.c

package info (click to toggle)
iceweasel 2.0.0.19-0etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 298,784 kB
  • ctags: 317,912
  • sloc: cpp: 1,796,902; ansic: 987,677; xml: 109,036; makefile: 47,777; asm: 35,201; perl: 26,983; sh: 20,879; cs: 6,232; java: 5,513; python: 3,249; pascal: 459; lex: 306; php: 244; csh: 132; objc: 97; yacc: 79; ada: 49; awk: 14; sql: 4; sed: 4
file content (242 lines) | stat: -rw-r--r-- 7,681 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <windows.h>
#include <stdio.h>

#define MAX_BUF 1024
#define  LEN_DELAYED_DELETE_PREFIX lstrlen("\\??\\")

void PrintSpecial(char *string, int stringBufSize);

void PrintSpecial(char *string, int stringBufSize)
{
  int i;
  int nullCount = 0;

  printf("\nlength: %d\n", stringBufSize);
  for(i = 0; i < stringBufSize; i++)
  {
    if(string[i] == '\0')
    {
      printf("{null}");
      nullCount++;
    }
    else
      printf("%c", string[i]);
  }
}

void RemoveDelayedDeleteFileEntries(const char *aPathToMatch)
{
  HKEY  hkResult;
  DWORD dwErr;
  DWORD dwType = REG_NONE;
  DWORD oldMaxValueLen = 0;
  DWORD newMaxValueLen = 0;
  DWORD lenToEnd = 0;
  char  *multiStr = NULL;
  const char key[] = "SYSTEM\\CurrentControlSet\\Control\\Session Manager";
  const char name[] = "PendingFileRenameOperations";
  char  *pathToMatch;
  char  *lcName;
  char  *pName;
  char  *pRename;
  int   nameLen, renameLen;

  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ|KEY_WRITE, &hkResult) != ERROR_SUCCESS)
    return;

  dwErr = RegQueryValueEx(hkResult, name, 0, &dwType, NULL, &oldMaxValueLen);
  if (dwErr != ERROR_SUCCESS || oldMaxValueLen == 0 || dwType != REG_MULTI_SZ)
  {
    /* no value, no data, or wrong type */
    return;
  }

  multiStr = calloc(oldMaxValueLen, sizeof(BYTE));
  if (!multiStr)
    return;

  pathToMatch = strdup(aPathToMatch);
  if (!pathToMatch)
  {
      free(multiStr);
      return;
  }

  if (RegQueryValueEx(hkResult, name, 0, NULL, multiStr, &oldMaxValueLen) == ERROR_SUCCESS)
  {
       PrintSpecial(multiStr, oldMaxValueLen);
      // The registry value consists of name/newname pairs of null-terminated
      // strings, with a final extra null termination. We're only interested
      // in files to be deleted, which are indicated by a null newname.
      _mbslwr(pathToMatch);
      lenToEnd = newMaxValueLen = oldMaxValueLen;
      pName = multiStr;
      while(*pName && lenToEnd > 0)
      {
          // find the locations and lengths of the current pair. Count the
          // nulls,  we need to know how much data to skip or move
          nameLen = strlen(pName) + 1;
          pRename = pName + nameLen;
          renameLen = strlen(pRename) + 1;

          // How much remains beyond the current pair
          lenToEnd -= (nameLen + renameLen);

          if (*pRename == '\0')
          {
              // No new name, it's a delete. Is it the one we want?
              lcName = strdup(pName);
              if (lcName)
              {
                _mbslwr(lcName);
                if (strstr(lcName, pathToMatch))
                {
                  // It's a match--
                  // delete this pair by moving the remainder on top
                  memmove(pName, pRename + renameLen, lenToEnd);

                  // update the total length to reflect the missing pair
                  newMaxValueLen -= (nameLen + renameLen);

                  // next pair is in place, continue w/out moving pName
                  continue;
                }
              }
          }
          // on to the next pair
          pName = pRename + renameLen;
      }

      if (newMaxValueLen != oldMaxValueLen)
      {
          PrintSpecial(multiStr, newMaxValueLen);
          // We've deleted something, save the changed data
          RegSetValueEx(hkResult, name, 0, REG_MULTI_SZ, multiStr, newMaxValueLen);
          RegFlushKey(hkResult);
      }
  }

  RegCloseKey(hkResult);
  free(multiStr);
}

#if 0
void RemoveDelayedDeleteFileEntries(const char *aPathToMatch)
{
  HKEY  hkResult;
  DWORD dwErr;
  DWORD oldMaxValueLen = 0;
  DWORD newMaxValueLen = 0;
  int   lenToEnd = 0;
  char  buf[MAX_BUF];
  char  *pathToMatch = NULL;
  char  *multiStr = NULL;
  char  key[] = "SYSTEM\\CurrentControlSet\\Control\\Session Manager";
  char  name[] = "PendingFileRenameOperations";
  char  *op;
  char  *np;
  char  *beginPath;
  BOOL  done = FALSE;

  pathToMatch = strdup(aPathToMatch);
  if(!pathToMatch)
    return;

  if((dwErr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ|KEY_WRITE, &hkResult)) == ERROR_SUCCESS)
  {
    dwErr = RegQueryValueEx(hkResult, name, 0, NULL, NULL, &oldMaxValueLen);
    printf("maxValueLen0: %d\n", oldMaxValueLen);

    multiStr = calloc(oldMaxValueLen, sizeof(BYTE));
    dwErr = RegQueryValueEx(hkResult, name, 0, NULL, multiStr, &oldMaxValueLen);
    printf("maxValueLen1: %d\n", oldMaxValueLen);

    if(dwErr != ERROR_SUCCESS)
      printf("RegQueryValueEx() failed: %d\n", dwErr);


    PrintSpecial(multiStr, oldMaxValueLen);

    lenToEnd = newMaxValueLen = oldMaxValueLen;
    op = multiStr;
    if(op && (oldMaxValueLen > 4))
    {
      _mbslwr(pathToMatch);
      while(!done)
      {
        beginPath = op + LEN_DELAYED_DELETE_PREFIX; // skip the first 4 chars ('\\??\\') to get to the first char of path;
        np = beginPath + lstrlen(beginPath) + 1;
        lstrcpy(buf, beginPath);
        _mbslwr(buf);
        lenToEnd = lenToEnd - lstrlen(op) + 2 - LEN_DELAYED_DELETE_PREFIX; // reset the lenToEnd
        if(strstr(buf, pathToMatch))
        {
          if(*np == '\0') // delete found
          {
            newMaxValueLen = newMaxValueLen - lstrlen(op) + 2 - LEN_DELAYED_DELETE_PREFIX; // reset the newMaxValueLen
            memmove(op, np + 1, lenToEnd);
          }
          else // rename found.  do nothing for renaming
          {
            printf("len of np: %d\n", lstrlen(np));
            lenToEnd = lenToEnd - lstrlen(np); // reset the lenToEnd
            op = np + lstrlen(np) + 1;
          }
        }
        else if(*np != '\0')
        {
          lenToEnd = lenToEnd - lstrlen(np); // reset the lenToEnd
          op = np + lstrlen(np) + 1;
        }
        else
          op = np + 1;

        if(*op == '\0') // terminating NULL byte indicating end of MULTISZ
          done = TRUE;
      }
    }

    PrintSpecial(multiStr, newMaxValueLen);

    dwErr = RegSetValueEx(hkResult,
                          name,
                          0,
                          REG_MULTI_SZ,
                          multiStr,
                          newMaxValueLen);
    if(dwErr != ERROR_SUCCESS)
      printf("RegQueryValueEx() failed: %d\n", dwErr);

    if(multiStr)
      free(multiStr);

    RegFlushKey(hkResult);
    RegCloseKey(hkResult);
  }

  if(pathToMatch)
    free(pathToMatch);
}
#endif

int main(argc, argv)
{
    char buf[MAX_BUF];
    DWORD bufSize = sizeof(buf);

//    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
    MoveFileEx("c:\\program files\\aol communicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
//    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
    MoveFileEx("c:\\prOGRAM fILES\\aol COMMUNicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
//    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
    MoveFileEx("srcfiletoremane.txt", "dstfilerenamed.txt", MOVEFILE_DELAY_UNTIL_REBOOT);

//    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
    MoveFileEx("c:\\program files\\aol communicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
//    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
    MoveFileEx("c:\\program files\\aol communicator\\phidlemon.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
//    MoveFileEx("c:\\windows\\system32\\mapistubs.dll", NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
    MoveFileEx("srcfiletorename.txt", "dstfilerenamed.txt", MOVEFILE_DELAY_UNTIL_REBOOT);

    RemoveDelayedDeleteFileEntries("C:\\Program Files\\AOL Communicator\\");
}