2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1997-1998
5 Copyright (C) Richard Sharpe 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 __declspec( thread ) extern int ProcessNumber;
30 __declspec( thread ) extern int LineCount;
31 __declspec( thread ) extern int AfsTrace;
32 __declspec( thread ) extern int *pThreadStatus;
33 __declspec( thread ) extern int LogID;
34 __declspec( thread ) extern char *IoBuffer;
35 __declspec( thread ) extern char AfsLocker[256];
36 __declspec( thread ) extern char OriginalAfsLocker[256];
37 __declspec( thread ) extern char HostName[256];
38 __declspec( thread ) extern DWORD TickCount1, TickCount2, MilliTickStart;
39 __declspec( thread ) extern FTABLE ftable[MAX_FILES];
40 __declspec( thread ) extern struct cmd_struct ThreadCommandInfo[CMD_MAX_CMD + 1];
41 __declspec( thread ) extern EXIT_STATUS *pExitStatus;
42 __declspec( thread ) extern DWORD LastKnownError;
43 extern void LogMessage(int ProcessNumber, char *HostName, char *FileName, char *message, int LogID);
45 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
46 uint32 FileAttributes, uint32 ShareAccess,
47 uint32 CreateDisposition, uint32 CreateOptions);
48 void DumpAFSLog(char * HostName, int LogID);
49 int FindHandle(int handle);
50 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state);
51 BOOL GetFileInfo(char *FileName, HANDLE fd, uint16 *mode, size_t *size,
52 time_t *c_time, time_t *a_time, time_t *m_time,
54 BOOL GetPathInfo(const char *fname, time_t *c_time, time_t *a_time, time_t *m_time,
55 size_t *size, uint16 *mode);
56 int LeaveThread(int status, char *Reason, int cmd);
57 void StartFirstTimer();
58 void EndFirstTimer(int cmd, int Type);
59 void StartSecondTime(int cmd);
60 void EndSecondTime(int cmd);
61 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len);
62 intptr_t SystemCall(char *command);
63 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes);
64 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes);
66 int FindHandle(int handle)
69 for (i=0;i<MAX_FILES;i++)
71 if (ftable[i].handle == handle)
75 printf("(%d) ERROR: handle %d was not found\n", LineCount, handle);
76 return(LeaveThread(1, "", -1));
79 int nb_unlink(char *fname)
86 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
88 strcpy(path, AfsLocker);
92 rc = DeleteFile(path);
93 EndFirstTimer(CMD_UNLINK, 1);
96 LeaveThread(0, "", CMD_UNLINK);
97 sprintf(temp, "FILE: DeleteFile %s failed\n", fname);
100 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
107 int nb_SetLocker(char *Locker)
111 if (strlen(Locker) == 0)
112 strcpy(AfsLocker, OriginalAfsLocker);
114 strcpy(AfsLocker, Locker);
115 EndFirstTimer(CMD_SETLOCKER, 1);
119 int nb_Xrmdir(char *Directory, char *type)
124 char NewDirectory[256];
127 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
128 if (strlen(Directory) == 0)
130 return(LeaveThread(1, "rmdir failed no path found\n", CMD_XRMDIR));
132 strcpy(NewDirectory, Directory);
133 memset(command, '\0', sizeof(command));
134 strcpy(command,"rmdir /Q ");
135 if (!stricmp(type, "all"))
137 strcat(command, "/S ");
139 strcat(command, NewDirectory);
142 rc = system(command);
144 if ((rc) && (rc != 2) && (rc != 3))
146 EndFirstTimer(CMD_XRMDIR, 0);
147 sprintf(temp, "rmdir failed on %s\n", command);
148 LeaveThread(rc, temp, CMD_XRMDIR);
149 sprintf(temp, "FAILURE: Thread %d - rmdir failed on\n %s\n", ProcessNumber, command);
150 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
153 EndFirstTimer(CMD_XRMDIR, 1);
158 int nb_Mkdir(char *Directory)
163 char NewDirectory[256];
166 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
167 if (strlen(Directory) == 0)
169 return(LeaveThread(1, "mkdir failed on no path found\n", CMD_MKDIR));
171 strcpy(NewDirectory, Directory);
172 memset(command, '\0', sizeof(command));
173 strcpy(command,"mkdir ");
174 strcat(command, NewDirectory);
177 rc = system(command);
181 EndFirstTimer(CMD_MKDIR, 0);
182 sprintf(temp, "mkdir failed on %s\n", command);
183 LeaveThread(rc, temp, CMD_MKDIR);
184 sprintf(temp, "ERROR: Thread %d - mkdir failed on\n %s\n", ProcessNumber, command);
185 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
188 EndFirstTimer(CMD_MKDIR, 1);
192 int nb_Attach(char *Locker, char *Drive)
199 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
200 if (strlen(Locker) == 0)
202 return(LeaveThread(1, "attach failed no locker found\n", CMD_ATTACH));
204 memset(command, '\0', sizeof(command));
205 strcpy(command,"attach -q ");
207 if (strlen(Drive) != 0)
209 sprintf(temp, "-D %s ", Drive);
210 strcat(command, temp);
212 strcat(command, Locker);
215 rc = system(command);
219 EndFirstTimer(CMD_ATTACH, 0);
220 sprintf(pExitStatus->Reason, "attach failed on %s\n", command);
221 pExitStatus->ExitStatus = rc;
222 sprintf(temp, "ERROR: Thread %d - attach failed on\n %s\n", ProcessNumber, command);
223 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
225 EndFirstTimer(CMD_ATTACH, 1);
229 int nb_Detach(char *Name, char *type)
236 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
237 memset(command, '\0', sizeof(command));
238 strcpy(command,"detach -q ");
240 if (!stricmp(type, "drive"))
242 sprintf(temp, "-D %s ", Name);
243 strcat(command, temp);
245 else if (!stricmp(type, "locker"))
247 strcat(command, Name);
251 return(LeaveThread(1, "nb_Detach failed unknown type: %s\n", CMD_DETACH));
255 rc = system(command);
259 EndFirstTimer(CMD_DETACH, 0);
260 sprintf(temp, "detach failed on %s\n", command);
261 LeaveThread(rc, temp, CMD_DETACH);
262 sprintf(temp, "ERROR: Thread %d - detach failed on\n %s\n", ProcessNumber, command);
263 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
266 EndFirstTimer(CMD_DETACH, 1);
270 int nb_CreateFile(char *path, DWORD size)
282 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
283 if (strlen(path) == 0)
285 return(LeaveThread(1, "nb_DeleteFile failed no path found\n", CMD_CREATEFILE));
288 strcpy(NewPath, path);
291 fHandle = CreateFile(NewPath,
292 GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_ALL,
293 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
296 FILE_ATTRIBUTE_NORMAL,
299 if (fHandle == INVALID_HANDLE_VALUE)
301 EndFirstTimer(CMD_CREATEFILE, 0);
302 sprintf(temp, "Create file failed on %s\n", NewPath);
303 LeaveThread(0, "", CMD_CREATEFILE);
304 sprintf(temp, "ERROR: Thread %d - Create file failed on\n %s\n", ProcessNumber, NewPath);
305 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
308 EndFirstTimer(CMD_CREATEFILE, 1);
309 Moved = SetFilePointer(fHandle,
313 memset(Buffer, 'A', sizeof(Buffer));
314 BytesToWrite = sizeof(Buffer);
315 rc = WriteFile(fHandle, Buffer, BytesToWrite, &BytesWritten, NULL);
317 FlushFileBuffers(fHandle);
318 CloseHandle(fHandle);
323 int nb_CopyFile(char *Source, char *Destination)
330 char NewDestination[256];
332 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
333 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
335 return(LeaveThread(1, "nb_CopyFile failed to copy files: either source or destination path not found\n", CMD_COPYFILES));
337 strcpy(NewSource, Source);
338 strcpy(NewDestination, Destination);
340 memset(command, '\0', sizeof(command));
341 sprintf(command, "copy /V /Y /B %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
344 rc = system(command);
348 EndFirstTimer(CMD_COPYFILES, 0);
349 sprintf(temp, "copy failed on %s\n", command);
350 LeaveThread(rc, temp, CMD_COPYFILES);
351 sprintf(temp, "FAILURE: Thread %d - copy failed on\n %s\n", ProcessNumber, command);
352 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
357 EndFirstTimer(CMD_COPYFILES, 1);
361 int nb_DeleteFile(char *path)
369 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
370 if (strlen(path) == 0)
372 return(LeaveThread(1, "nb_DeleteFile failed to delete files: no path found\n", CMD_DELETEFILES));
374 strcpy(NewPath, path);
376 memset(command, '\0', sizeof(command));
377 sprintf(command, "del /Q %s", NewPath);
380 rc = system(command);
384 EndFirstTimer(CMD_DELETEFILES, 0);
385 sprintf(temp, "del failed on %s\n", NewPath);
386 LeaveThread(rc, temp, CMD_DELETEFILES);
387 sprintf(temp, "ERROR: Thread %d - del failed on\n %s\n", ProcessNumber, command);
388 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
391 EndFirstTimer(CMD_DELETEFILES, 1);
395 int nb_xcopy(char *Source, char *Destination)
402 char NewDestination[256];
404 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
405 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
407 return(LeaveThread(1, "nb_xcopy failed to xcopy: either source or destination is missing\n", CMD_XCOPY));
409 strcpy(NewSource, Source);
410 strcpy(NewDestination, Destination);
411 memset(command, '\0', sizeof(command));
412 sprintf(command, "xcopy /E /I /V /Y /Q %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
415 rc = SystemCall(command);
419 EndFirstTimer(CMD_XCOPY, 0);
420 // sprintf(temp, "xcopy failed on %s\n", command);
421 // LeaveThread(rc, temp, CMD_XCOPY);
422 LeaveThread(0, "", CMD_XCOPY);
423 sprintf(temp, "FAIURE: Thread %d - xcopy failed on\n %s\n", ProcessNumber, command);
424 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
427 EndFirstTimer(CMD_XCOPY, 1);
431 int nb_Move(char *Source, char *Destination)
438 char NewDestination[256];
440 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
441 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
443 return(LeaveThread(1, "nb_Move failed to move: either source or destination is missing\n", CMD_MOVE));
445 strcpy(NewSource, Source);
446 strcpy(NewDestination, Destination);
447 memset(command, '\0', sizeof(command));
448 sprintf(command, "move /Y %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
450 rc = system(command);
454 EndFirstTimer(CMD_MOVE, 0);
455 sprintf(temp, "move failed on %s\n", command);
456 LeaveThread(rc, temp, CMD_MOVE);
457 sprintf(temp, "FAILURE: Thread %d - move failed on\n %s\n", ProcessNumber, command);
458 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
461 EndFirstTimer(CMD_MOVE, 1);
465 int nb_createx(char *fname, unsigned create_options, unsigned create_disposition, int handle)
469 uint32 desired_access;
474 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
476 strcpy(path, AfsLocker);
478 if (create_options & FILE_DIRECTORY_FILE)
480 desired_access = FILE_READ_DATA;
484 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
488 fd = CreateObject(path,
491 FILE_SHARE_READ|FILE_SHARE_WRITE,
495 if (fd == INVALID_HANDLE_VALUE && handle != -1)
497 if (create_options & FILE_DIRECTORY_FILE)
499 DWORD rc = GetLastError();
500 if ((rc != ERROR_FILE_NOT_FOUND) && (rc != ERROR_PATH_NOT_FOUND) && (rc != ERROR_ALREADY_EXISTS))
502 EndFirstTimer(CMD_NTCREATEX, 0);
504 LeaveThread(0, "", CMD_NTCREATEX);
505 sprintf(temp, "Directory: unable to create directory %s\n", path);
508 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
515 EndFirstTimer(CMD_NTCREATEX, 0);
516 LeaveThread(0, "", CMD_NTCREATEX);
517 sprintf(temp, "File: unable to create file %s\n", path);
520 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
525 EndFirstTimer(CMD_NTCREATEX, 1);
527 if (create_options & FILE_DIRECTORY_FILE)
530 if (fd != INVALID_HANDLE_VALUE && handle == -1)
537 if (fd == INVALID_HANDLE_VALUE && handle == -1)
540 for (i = 0; i < MAX_FILES; i++)
542 if (ftable[i].handle == 0)
547 printf("(%d) file table full for %s\n", LineCount, path);
548 return(LeaveThread(1, "file table is full\n", CMD_NTCREATEX));
550 ftable[i].handle = handle;
553 free(ftable[i].name);
554 ftable[i].name = strdup(path);
555 ftable[i].reads = ftable[i].writes = 0;
559 int nb_writex(int handle, int offset, int size, int ret_size)
566 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
568 if (IoBuffer[0] == 0)
569 memset(IoBuffer, 1, sizeof(IoBuffer));
571 if ((i = FindHandle(handle)) == -1)
574 status = nb_write(ftable[i].fd, IoBuffer, offset, size);
575 if (status != ret_size)
577 EndFirstTimer(CMD_WRITEX, 0);
578 LeaveThread(0, "", CMD_WRITEX);
580 sprintf(temp, "File: %s. wrote %d bytes, got %d bytes\n", ftable[i].name, size, status);
582 sprintf(temp, "File: %s. On write, cannot set file pointer\n", ftable[i].name);
586 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
589 EndFirstTimer(CMD_WRITEX, 1);
595 int nb_lock(int handle, int offset, int size, int timeout, unsigned char locktype, NTSTATUS exp)
603 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
605 if ((i = FindHandle(handle)) == -1)
607 LeaveThread(0, "", CMD_LOCKINGX);
608 sprintf(temp, "File unlock: Cannot find handle for %s", ftable[i].name);
611 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
616 ret = cli_locktype(c, i, offset, size, timeout, locktype);
620 EndFirstTimer(CMD_LOCKINGX, 0);
621 LeaveThread(0, "", CMD_LOCKINGX);
622 sprintf(temp, "(%d) ERROR: lock failed on handle %d ofs=%d size=%d timeout= %d exp=%d fd %d errno %d (%s)\n",
623 LineCount, handle, offset, size, timeout, exp, ftable[i].fd, exp, "");
624 sprintf(temp, "File unlock: lock failed %s", ftable[i].name);
627 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
630 EndFirstTimer(CMD_LOCKINGX, 1);
635 int nb_readx(int handle, int offset, int size, int ret_size)
642 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
644 if ((i = FindHandle(handle)) == -1)
648 ret = nb_read(ftable[i].fd, IoBuffer, offset, size);
650 if ((ret != size) && (ret != ret_size))
652 EndFirstTimer(CMD_READX, 0);
653 LeaveThread(0, "", CMD_READX);
655 sprintf(temp, "File: read failed on index=%d, offset=%d ReadSize=%d ActualRead=%d handle=%p\n",
656 handle, offset, size, ret, ftable[i].fd);
658 sprintf(temp, "File: %s. On read, cannot set file pointer\n", ftable[i].name);
662 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
665 EndFirstTimer(CMD_READX, 1);
670 int nb_close(int handle)
677 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
679 if ((i = FindHandle(handle)) == -1)
683 ret = nb_close1(ftable[i].fd);
684 EndFirstTimer(CMD_CLOSE, ret);
687 LeaveThread(0, "", CMD_CLOSE);
688 sprintf(temp, "(%d) close failed on handle %d\n", LineCount, handle);
691 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
695 ftable[i].handle = 0;
698 free(ftable[i].name);
699 ftable[i].name = NULL;
703 int nb_rmdir(char *fname)
710 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
712 strcpy(path, AfsLocker);
716 rc = RemoveDirectory(path);
717 EndFirstTimer(CMD_RMDIR, rc);
721 LeaveThread(0, "", CMD_RMDIR);
722 sprintf(temp, "Directory: RemoveDirectory %s failed\n", fname);
725 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
731 int nb_rename(char *old, char *New)
739 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
741 strcpy(opath, AfsLocker);
743 strcpy(npath, AfsLocker);
747 rc = MoveFileEx(opath, npath, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
748 EndFirstTimer(CMD_RENAME, rc);
752 LeaveThread(0, "", CMD_RENAME);
753 sprintf(temp, "File: rename %s %s failed\n", old, New);
756 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
763 int nb_qpathinfo(char *fname, int Type)
774 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
776 strcpy(path, AfsLocker);
780 rc = GetPathInfo(path, NULL, NULL, NULL, NULL, NULL);
782 if (strstr(fname, "~TS"))
801 EndFirstTimer(CMD_QUERY_PATH_INFO, 0);
802 LeaveThread(0, "", CMD_QUERY_PATH_INFO);
803 sprintf(temp, "File: qpathinfo failed for %s\n", path);
806 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
810 EndFirstTimer(CMD_QUERY_PATH_INFO, 1);
814 int nb_qfileinfo(int handle)
821 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
823 if ((i = FindHandle(handle)) == -1)
827 rc = GetFileInfo(ftable[i].name, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL);
831 EndFirstTimer(CMD_QUERY_FILE_INFO, 0);
832 LeaveThread(0, "", CMD_QUERY_FILE_INFO);
833 sprintf(temp, "File: qfileinfo failed for %s\n", ftable[i].name);
836 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
839 EndFirstTimer(CMD_QUERY_FILE_INFO, 1);
844 int nb_qfsinfo(int level)
854 ULARGE_INTEGER FreeBytesAvailable;
855 ULARGE_INTEGER TotalNumberOfBytes;
856 ULARGE_INTEGER TotalNumberOfFreeBytes;
858 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
859 sprintf(Path, "%s\\%s%05d", AfsLocker, HostName, LogID);
862 rc = GetDiskFreeSpaceEx(Path, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
863 // rc = cli_dskattr(c, &bsize, &total, &avail);
867 EndFirstTimer(CMD_QUERY_FS_INFO, 0);
868 LeaveThread(0, "", CMD_QUERY_FS_INFO);
869 strcpy(temp, "File: Disk free space failed\n");
872 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
875 EndFirstTimer(CMD_QUERY_FS_INFO, 1);
880 void find_fn(file_info *finfo, char *name, void *state)
885 int nb_findfirst(char *mask)
892 if (strstr(mask, "<.JNK"))
895 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
897 strcpy(NewMask, AfsLocker);
898 strcat(NewMask, mask);
901 rc = GetFileList(NewMask, (void *)find_fn, NULL);
905 EndFirstTimer(CMD_FIND_FIRST, 0);
906 sprintf(temp, "File: findfirst cannot find for %s\n", mask);
909 LeaveThread(1, temp, CMD_FIND_FIRST);
910 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
913 EndFirstTimer(CMD_FIND_FIRST, 1);
917 int nb_flush(int handle)
921 if ((i = FindHandle(handle)) == -1)
924 FlushFileBuffers(ftable[i].fd);
928 static int total_deleted;
930 void delete_fn(file_info *finfo, const char *name, void *state)
937 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
939 if (finfo->mode & aDIR)
942 sprintf(s2, "%s\\*", name);
943 GetFileList(s2, delete_fn, NULL);
944 sprintf(s, "%s", &name[strlen(AfsLocker)]);
949 rc = DeleteFile(name);
952 LeaveThread(0, "", CMD_UNLINK);
953 sprintf(temp, "FILE: DeleteFile %s failed\n", name);
956 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
963 int nb_deltree(char *dname)
970 strcpy(path, AfsLocker);
972 sprintf(mask, "%s\\*", path);
977 GetFileList(mask, delete_fn, NULL);
979 // pstrcpy(path, AfsLocker);
980 // pstrcat(path, dname);
981 rc = RemoveDirectory(path);
982 EndFirstTimer(CMD_DELTREE, rc);
990 if ((rc != ERROR_FILE_NOT_FOUND) && (rc != ERROR_PATH_NOT_FOUND))
993 LeaveThread(0, "", CMD_DELTREE);
994 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
995 sprintf(temp, "ERROR: Thread %d - Unable to remove %s.\n", ProcessNumber, path);
996 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1006 int nb_cleanup(char *cname)
1010 strcpy(temp, "\\clients\\client1");
1011 SubstituteString(temp, "client1", cname, sizeof(temp));
1012 SubstituteString(temp, "clients", HostName, sizeof(temp));
1017 int LeaveThread(int status, char *Reason, int cmd)
1024 ++ThreadCommandInfo[cmd].ErrorCount;
1026 if (strlen(Reason) == 0)
1029 rc = GetLastError();
1032 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1035 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1036 pExitStatus->Reason,
1037 sizeof(pExitStatus->Reason),
1039 LastKnownError = rc;
1042 strcpy(pExitStatus->Reason, Reason);
1044 if (strlen(pExitStatus->Reason) == 0)
1045 strcpy(pExitStatus->Reason, "\n");
1046 if (pExitStatus->Reason[strlen(pExitStatus->Reason) - 1] != '\n')
1047 strcat(pExitStatus->Reason, "\n");
1048 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1049 if (strlen(Reason) == 0)
1050 sprintf(temp, "ERROR(%d): Thread %d - (%d) %s", LineCount, ProcessNumber, rc, pExitStatus->Reason);
1052 sprintf(temp, "ERROR(%d): Thread %d - %s", LineCount, ProcessNumber, pExitStatus->Reason);
1053 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1056 pExitStatus->ExitStatus = status;
1058 DumpAFSLog(HostName, LogID);
1059 (*pThreadStatus) = 0;
1063 void StartFirstTimer(void)
1066 EndSecondTime(CMD_NONAFS);
1067 TickCount1 = GetTickCount();
1070 void EndFirstTimer(int cmd, int Type)
1075 ThreadCommandInfo[cmd].count++;
1077 MilliTick = GetTickCount() - TickCount1;
1080 StartSecondTime(CMD_NONAFS);
1084 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1085 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1087 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1093 ThreadCommandInfo[cmd].min_sec = cmd_time;
1094 StartSecondTime(CMD_NONAFS);
1099 ThreadCommandInfo[cmd].ErrorTime += cmd_time;
1102 ThreadCommandInfo[cmd].total_sec += cmd_time;
1103 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1104 ThreadCommandInfo[cmd].min_sec = cmd_time;
1105 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1106 ThreadCommandInfo[cmd].max_sec = cmd_time;
1109 StartSecondTime(CMD_NONAFS);
1112 void StartSecondTime(int cmd)
1115 TickCount2 = GetTickCount();
1119 void EndSecondTime(int cmd)
1124 ThreadCommandInfo[cmd].count++;
1126 MilliTick = GetTickCount() - TickCount2;
1131 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1132 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1134 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1139 ThreadCommandInfo[cmd].min_sec = cmd_time;
1142 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1143 ThreadCommandInfo[cmd].min_sec = cmd_time;
1144 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1145 ThreadCommandInfo[cmd].max_sec = cmd_time;
1146 ThreadCommandInfo[cmd].total_sec += cmd_time;
1150 intptr_t SystemCall(char *command)
1155 argv[0] = getenv("COMSPEC");
1158 argv[3] = (char *)command;
1161 rc = spawnve(_P_WAIT,argv[0],argv,NULL);
1162 // != -1 || (errno != ENOENT && errno != EACCES))
1166 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
1167 uint32 FileAttributes, uint32 ShareAccess,
1168 uint32 CreateDisposition, uint32 CreateOptions)
1171 DWORD dwCreateDisposition = 0;
1172 DWORD dwDesiredAccess = 0;
1173 DWORD dwShareAccess = 0;
1175 if (CreateOptions & FILE_DIRECTORY_FILE)
1177 if (!CreateDirectory(fname, NULL))
1178 fd = INVALID_HANDLE_VALUE;
1184 dwDesiredAccess = 0;
1185 if (DesiredAccess & FILE_READ_DATA)
1186 dwDesiredAccess |= GENERIC_READ;
1187 if (DesiredAccess & FILE_WRITE_DATA)
1188 dwDesiredAccess |= GENERIC_WRITE;
1189 dwShareAccess = ShareAccess;
1190 dwShareAccess |= FILE_SHARE_DELETE;
1191 dwCreateDisposition = OPEN_ALWAYS;
1192 if (CreateDisposition == 1)
1193 dwCreateDisposition = OPEN_EXISTING;
1194 fd = CreateFile(fname, dwDesiredAccess, ShareAccess, NULL, dwCreateDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1200 BOOL nb_close1(HANDLE fd)
1205 if (fd != INVALID_HANDLE_VALUE)
1207 if (rc = GetHandleInformation(fd, &dwFlags))
1213 /****************************************************************************
1214 do a directory listing, calling fn on each file found
1215 this uses the old SMBsearch interface. It is needed for testing Samba,
1216 but should otherwise not be used
1217 ****************************************************************************/
1218 int nb_list_old(const char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1220 int num_received = 0;
1223 char cFileName[1024];
1224 int dwFileAttributes;
1231 if (!strcmp(&mask[strlen(mask)-2], "\"*"))
1233 strcpy(&mask[strlen(mask)-2], "*");
1236 dwFileAttributes = 0;
1237 memset(cFileName, '\0', sizeof(cFileName));
1238 hFind = WinFindFirstFile(mask, &FileData, cFileName, &dwFileAttributes);
1239 if (hFind == INVALID_HANDLE_VALUE)
1243 mask[strlen(mask) - 1] = '\0';
1246 if (cFileName[0] != '.')
1250 memset(&finfo, '\0', sizeof(finfo));
1251 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1253 strcpy(finfo.name, cFileName);
1254 sprintf(temp, "%s%s", mask, cFileName);
1255 fn(&finfo, temp, state);
1258 memset(cFileName, '\0', sizeof(cFileName));
1259 dwFileAttributes = 0;
1260 if (!WinFindNextFile(hFind, &FileData, cFileName, &dwFileAttributes))
1264 return(num_received);
1268 /****************************************************************************
1269 do a directory listing, calling fn on each file found
1270 this auto-switches between old and new style
1271 ****************************************************************************/
1272 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1274 return(nb_list_old(Mask, fn, state));
1277 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes)
1280 static WIN32_FIND_DATAW FileDataW;
1281 static WIN32_FIND_DATAA FileDataA;
1283 memset(&FileDataA, '\0', sizeof(FileDataA));
1284 rc = FindFirstFile(Mask, &FileDataA);
1285 if (rc != INVALID_HANDLE_VALUE)
1287 (*FileData) = (void *)&FileDataA;
1288 (*dwFileAttributes) = FileDataA.dwFileAttributes;
1289 strcpy(cFileName, FileDataA.cFileName);
1295 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes)
1298 WIN32_FIND_DATAA *FileDataA;
1300 FileDataA = (WIN32_FIND_DATAA *)(*FileData);
1301 if (!(rc = FindNextFile(hFind, FileDataA)))
1305 (*dwFileAttributes) = FileDataA->dwFileAttributes;
1306 strcpy(cFileName, FileDataA->cFileName);
1311 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len)
1316 if (!insert || !pattern || !s) return;
1318 ls = (ssize_t)strlen(s);
1319 lp = (ssize_t)strlen(pattern);
1320 li = (ssize_t)strlen(insert);
1322 if (!*pattern) return;
1324 while (lp <= ls && (p = strstr(s,pattern)))
1326 if (len && (ls + (li-lp) >= (int)len))
1332 memmove(p+li,p+lp,strlen(p+lp)+1);
1334 memcpy(p, insert, li);
1340 #define CHANGE_TIME(A,B) \
1341 B.tm_hour = A.wHour; \
1342 B.tm_sec = A.wSecond; \
1343 B.tm_min = A.wMinute; \
1344 B.tm_mon = A.wMonth - 1; \
1346 B.tm_year = A.wYear - 1900; \
1347 B.tm_wday = A.wDayOfWeek - 1; \
1352 BOOL GetPathInfo(const char *fname,
1353 time_t *c_time, time_t *a_time, time_t *m_time,
1354 size_t *size, uint16 *mode)
1356 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1358 SYSTEMTIME SystemTime;
1361 rc = GetFileAttributesEx(fname, GetFileExInfoStandard, &FileInfo);
1366 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1367 CHANGE_TIME(SystemTime, tm_time)
1368 (*c_time) = mktime(&tm_time);
1372 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1373 CHANGE_TIME(SystemTime, tm_time)
1374 (*a_time) = mktime(&tm_time);
1378 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1379 CHANGE_TIME(SystemTime, tm_time)
1380 (*m_time) = mktime(&tm_time);
1385 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1386 *size = FileInfo.nFileSizeLow;
1397 /****************************************************************************
1398 send a qfileinfo call
1399 ****************************************************************************/
1400 BOOL GetFileInfo(char *FileName, HANDLE fd,
1401 uint16 *mode, size_t *size,
1402 time_t *c_time, time_t *a_time, time_t *m_time,
1405 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1407 SYSTEMTIME SystemTime;
1410 rc = GetFileAttributesEx(FileName, GetFileExInfoStandard, &FileInfo);
1415 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1416 CHANGE_TIME(SystemTime, tm_time)
1417 (*c_time) = mktime(&tm_time);
1421 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1422 CHANGE_TIME(SystemTime, tm_time)
1423 (*a_time) = mktime(&tm_time);
1427 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1428 CHANGE_TIME(SystemTime, tm_time)
1429 (*m_time) = mktime(&tm_time);
1434 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1435 *size = FileInfo.nFileSizeLow;
1446 /****************************************************************************
1447 Read size bytes at offset offset using SMBreadX.
1448 ****************************************************************************/
1450 ssize_t nb_read(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1459 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1461 if (LowDword == INVALID_SET_FILE_POINTER)
1463 rc = ReadFile(fd, IoBuffer, (DWORD)size, &total, NULL);
1470 /****************************************************************************
1472 ****************************************************************************/
1474 ssize_t nb_write(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1480 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1481 if (LowDword == INVALID_SET_FILE_POINTER)
1483 rc = WriteFile(fd, IoBuffer, (DWORD)size, &bwritten, NULL);
1486 FlushFileBuffers(fd);