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.
30 __declspec( thread ) extern int ProcessNumber;
31 __declspec( thread ) extern int LineCount;
32 __declspec( thread ) extern int AfsTrace;
33 __declspec( thread ) extern int *pThreadStatus;
34 __declspec( thread ) extern int LogID;
35 __declspec( thread ) extern char *IoBuffer;
36 __declspec( thread ) extern int BufferSize;
37 __declspec( thread ) extern char AfsLocker[256];
38 __declspec( thread ) extern char OriginalAfsLocker[256];
39 __declspec( thread ) extern char HostName[256];
40 __declspec( thread ) extern DWORD TickCount1, TickCount2, MilliTickStart;
41 __declspec( thread ) extern FTABLE ftable[MAX_FILES];
42 __declspec( thread ) extern struct cmd_struct ThreadCommandInfo[CMD_MAX_CMD + 1];
43 __declspec( thread ) extern EXIT_STATUS *pExitStatus;
44 __declspec( thread ) extern DWORD LastKnownError;
45 __declspec( thread ) int EnforcePathInfoErrors = 0;
47 extern void LogMessage(int ProcessNumber, char *HostName, char *FileName, char *message, int LogID);
49 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
50 uint32 FileAttributes, uint32 ShareAccess,
51 uint32 CreateDisposition, uint32 CreateOptions);
52 void DumpAFSLog(char * HostName, int LogID);
53 int FindHandle(int handle);
54 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state);
55 BOOL GetFileInfo(char *FileName, HANDLE fd, uint16 *mode, size_t *size,
56 time_t *c_time, time_t *a_time, time_t *m_time,
58 BOOL GetPathInfo(const char *fname, time_t *c_time, time_t *a_time, time_t *m_time,
59 size_t *size, uint16 *mode);
60 int LeaveThread(int status, char *Reason, int cmd);
61 void StartFirstTimer();
62 void EndFirstTimer(int cmd, int Type);
63 void StartSecondTime(int cmd);
64 void EndSecondTime(int cmd);
65 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len);
66 intptr_t SystemCall(char *command);
67 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes);
68 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes);
70 int FindHandle(int handle)
73 for (i=0;i<MAX_FILES;i++)
75 if (ftable[i].handle == handle)
79 printf("(%d) ERROR: handle %d was not found\n", LineCount, handle);
80 return(LeaveThread(1, "", -1));
83 int nb_unlink(char *fname)
90 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
92 strcpy(path, AfsLocker);
96 rc = DeleteFile(path);
97 EndFirstTimer(CMD_UNLINK, 1);
100 LeaveThread(0, "", CMD_UNLINK);
101 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", fname, GetLastError());
104 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
111 int nb_SetLocker(char *Locker)
115 if (strlen(Locker) == 0)
116 strcpy(AfsLocker, OriginalAfsLocker);
118 strcpy(AfsLocker, Locker);
119 EndFirstTimer(CMD_SETLOCKER, 1);
123 int nb_Xrmdir(char *Directory, char *type)
128 char NewDirectory[256];
131 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
132 if (strlen(Directory) == 0)
134 return(LeaveThread(1, "rmdir failed no path specified\n", CMD_XRMDIR));
136 strcpy(NewDirectory, Directory);
137 memset(command, '\0', sizeof(command));
138 strcpy(command,"rmdir /Q ");
139 if (!stricmp(type, "all"))
141 strcat(command, "/S ");
143 strcat(command, NewDirectory);
146 rc = system(command);
148 if ((rc) && (rc != 2) && (rc != 3))
150 EndFirstTimer(CMD_XRMDIR, 0);
151 sprintf(temp, "rmdir failed on %s\n", command);
152 LeaveThread(rc, temp, CMD_XRMDIR);
153 sprintf(temp, "FAILURE: Thread %d - Xrmdir failed on \"%s\"\n", ProcessNumber, command);
154 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
157 EndFirstTimer(CMD_XRMDIR, 1);
162 int nb_Mkdir(char *Directory)
167 char NewDirectory[256];
170 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
171 if (strlen(Directory) == 0)
173 return(LeaveThread(1, "mkdir failed on no path specified\n", CMD_MKDIR));
175 strcpy(NewDirectory, Directory);
176 memset(command, '\0', sizeof(command));
177 strcpy(command,"mkdir ");
178 strcat(command, NewDirectory);
181 rc = system(command);
185 EndFirstTimer(CMD_MKDIR, 0);
186 sprintf(temp, "mkdir failed on %s\n", command);
187 LeaveThread(rc, temp, CMD_MKDIR);
188 sprintf(temp, "ERROR: Thread %d - mkdir failed on \"%s\"\n", ProcessNumber, command);
189 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
192 EndFirstTimer(CMD_MKDIR, 1);
196 int nb_Attach(char *Locker, char *Drive)
203 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
204 if (strlen(Locker) == 0)
206 return(LeaveThread(1, "attach failed no locker specified\n", CMD_ATTACH));
208 memset(command, '\0', sizeof(command));
209 strcpy(command,"attach -q ");
211 if (strlen(Drive) != 0)
213 sprintf(temp, "-D %s ", Drive);
214 strcat(command, temp);
216 strcat(command, Locker);
219 rc = system(command);
223 EndFirstTimer(CMD_ATTACH, 0);
224 sprintf(pExitStatus->Reason, "attach failed on %s\n", command);
225 pExitStatus->ExitStatus = rc;
226 sprintf(temp, "ERROR: Thread %d - attach failed on \"%s\"\n", ProcessNumber, command);
227 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
229 EndFirstTimer(CMD_ATTACH, 1);
233 int nb_Detach(char *Name, char *type)
240 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
241 memset(command, '\0', sizeof(command));
242 strcpy(command,"detach -q ");
244 if (!stricmp(type, "drive"))
246 sprintf(temp, "-D %s ", Name);
247 strcat(command, temp);
249 else if (!stricmp(type, "locker"))
251 strcat(command, Name);
255 return(LeaveThread(1, "nb_Detach failed unknown type: %s\n", CMD_DETACH));
259 rc = system(command);
263 EndFirstTimer(CMD_DETACH, 0);
264 sprintf(temp, "detach failed on %s\n", command);
265 LeaveThread(rc, temp, CMD_DETACH);
266 sprintf(temp, "ERROR: Thread %d - detach failed on \"%s\"\n", ProcessNumber, command);
267 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
270 EndFirstTimer(CMD_DETACH, 1);
274 int nb_CreateFile(char *path, DWORD size)
286 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
287 if (strlen(path) == 0)
289 return(LeaveThread(1, "nb_DeleteFile failed no path specified\n", CMD_CREATEFILE));
292 strcpy(NewPath, path);
295 fHandle = CreateFile(NewPath,
296 GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_ALL,
297 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
300 FILE_ATTRIBUTE_NORMAL,
303 if (fHandle == INVALID_HANDLE_VALUE)
306 EndFirstTimer(CMD_CREATEFILE, 0);
307 sprintf(temp, "Create file failed on \"%s\" GLE(0x%x)\n", NewPath, rc);
308 LeaveThread(0, temp, CMD_CREATEFILE);
309 sprintf(temp, "ERROR: Thread %d - Create file failed on \"%s\" GLE(0x%x)\n", ProcessNumber, NewPath, rc);
310 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
313 EndFirstTimer(CMD_CREATEFILE, 1);
314 Moved = SetFilePointer(fHandle,
318 memset(Buffer, 'A', sizeof(Buffer));
319 BytesToWrite = sizeof(Buffer);
320 rc = WriteFile(fHandle, Buffer, BytesToWrite, &BytesWritten, NULL);
322 FlushFileBuffers(fHandle);
323 CloseHandle(fHandle);
328 int nb_CopyFile(char *Source, char *Destination)
335 char NewDestination[256];
337 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
338 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
340 return(LeaveThread(1, "nb_CopyFile failed to copy files: either source or destination path not specified\n", CMD_COPYFILES));
342 strcpy(NewSource, Source);
343 strcpy(NewDestination, Destination);
345 memset(command, '\0', sizeof(command));
346 sprintf(command, "copy /V /Y /B %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
349 rc = system(command);
353 EndFirstTimer(CMD_COPYFILES, 0);
354 sprintf(temp, "copy failed on \"%s\"\n", command);
355 LeaveThread(rc, temp, CMD_COPYFILES);
356 sprintf(temp, "FAILURE: Thread %d - copy failed on \"%s\"\n", ProcessNumber, command);
357 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
362 EndFirstTimer(CMD_COPYFILES, 1);
366 int nb_DeleteFile(char *path)
374 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
375 if (strlen(path) == 0)
377 return(LeaveThread(1, "nb_DeleteFile failed to delete files: no path specified\n", CMD_DELETEFILES));
379 strcpy(NewPath, path);
381 memset(command, '\0', sizeof(command));
382 sprintf(command, "del /Q %s", NewPath);
385 rc = system(command);
389 EndFirstTimer(CMD_DELETEFILES, 0);
390 sprintf(temp, "del failed on \"%s\"\n", NewPath);
391 LeaveThread(rc, temp, CMD_DELETEFILES);
392 sprintf(temp, "ERROR: Thread %d - del failed on \"%s\"\n", ProcessNumber, command);
393 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
396 EndFirstTimer(CMD_DELETEFILES, 1);
400 int nb_xcopy(char *Source, char *Destination)
407 char NewDestination[256];
409 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
410 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
412 return(LeaveThread(1, "nb_xcopy failed to xcopy: either source or destination is missing\n", CMD_XCOPY));
414 strcpy(NewSource, Source);
415 strcpy(NewDestination, Destination);
416 memset(command, '\0', sizeof(command));
417 sprintf(command, "xcopy /E /I /V /Y /Q %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
420 rc = SystemCall(command);
424 EndFirstTimer(CMD_XCOPY, 0);
425 sprintf(temp, "xcopy failed on %s\n", command);
426 LeaveThread((int)rc, temp, CMD_XCOPY);
427 sprintf(temp, "FAIURE: Thread %d - xcopy failed on \"%s\"\n", ProcessNumber, command);
428 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
431 EndFirstTimer(CMD_XCOPY, 1);
435 int nb_Move(char *Source, char *Destination)
442 char NewDestination[256];
444 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
445 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
447 return(LeaveThread(1, "nb_Move failed to move: either source or destination is missing\n", CMD_MOVE));
449 strcpy(NewSource, Source);
450 strcpy(NewDestination, Destination);
451 memset(command, '\0', sizeof(command));
452 sprintf(command, "move /Y %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
454 rc = system(command);
458 EndFirstTimer(CMD_MOVE, 0);
459 sprintf(temp, "move failed on \"%s\"\n", command);
460 LeaveThread(rc, temp, CMD_MOVE);
461 sprintf(temp, "FAILURE: Thread %d - move failed on \"%s\"\n", ProcessNumber, command);
462 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
465 EndFirstTimer(CMD_MOVE, 1);
469 int nb_createx(char *fname, unsigned create_options, unsigned create_disposition, int handle)
473 uint32 desired_access;
478 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
480 strcpy(path, AfsLocker);
482 if (create_options & FILE_DIRECTORY_FILE)
484 desired_access = FILE_READ_DATA;
488 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
492 fd = CreateObject(path,
495 FILE_SHARE_READ|FILE_SHARE_WRITE,
499 if (fd == INVALID_HANDLE_VALUE && handle != -1)
501 if (create_options & FILE_DIRECTORY_FILE)
503 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);
513 EndFirstTimer(CMD_NTCREATEX, 0);
514 LeaveThread(0, "", CMD_NTCREATEX);
515 sprintf(temp, "File: unable to create file %s\n", path);
518 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
523 EndFirstTimer(CMD_NTCREATEX, 1);
525 if (fd != INVALID_HANDLE_VALUE && handle == -1)
532 if (fd == INVALID_HANDLE_VALUE && handle == -1)
535 for (i = 0; i < MAX_FILES; i++)
537 if (ftable[i].handle == 0)
542 printf("(%d) file table full for %s\n", LineCount, path);
543 return(LeaveThread(1, "file table is full\n", CMD_NTCREATEX));
545 ftable[i].handle = handle;
548 free(ftable[i].name);
549 ftable[i].name = strdup(path);
550 ftable[i].reads = ftable[i].writes = 0;
554 int nb_writex(int handle, int offset, int size, int ret_size)
560 unsigned char magic = (unsigned char)getpid();
562 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
564 if (IoBuffer[0] != magic ||
565 IoBuffer[1] != magic ||
566 IoBuffer[2] != magic ||
567 IoBuffer[3] != magic)
568 memset(IoBuffer, magic, BufferSize);
570 if ((i = FindHandle(handle)) == -1)
573 status = nb_write(ftable[i].fd, IoBuffer, offset, size);
574 if (status != ret_size)
576 EndFirstTimer(CMD_WRITEX, 0);
577 LeaveThread(0, "", CMD_WRITEX);
579 sprintf(temp, "File: %s. wrote %d bytes, got %d bytes\n", ftable[i].name, size, status);
581 sprintf(temp, "File: %s. On write, cannot set file pointer\n", ftable[i].name);
585 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
588 EndFirstTimer(CMD_WRITEX, 1);
594 int nb_lock(int handle, int offset, int size, int timeout, unsigned char locktype, NTSTATUS exp)
602 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
604 if ((i = FindHandle(handle)) == -1)
606 LeaveThread(0, "", CMD_LOCKINGX);
607 sprintf(temp, "File unlock: Cannot find handle for %s", ftable[i].name);
610 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
615 ret = cli_locktype(c, i, offset, size, timeout, locktype);
619 EndFirstTimer(CMD_LOCKINGX, 0);
620 LeaveThread(0, "", CMD_LOCKINGX);
621 sprintf(temp, "(%d) ERROR: lock failed on handle %d ofs=%d size=%d timeout= %d exp=%d fd %d errno %d (%s)\n",
622 LineCount, handle, offset, size, timeout, exp, ftable[i].fd, exp, "");
623 sprintf(temp, "File unlock: lock failed %s", ftable[i].name);
626 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
629 EndFirstTimer(CMD_LOCKINGX, 1);
634 int nb_readx(int handle, int offset, int size, int ret_size)
641 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
643 if ((i = FindHandle(handle)) == -1)
647 ret = nb_read(ftable[i].fd, IoBuffer, offset, size);
649 if ((ret != size) && (ret != ret_size))
651 EndFirstTimer(CMD_READX, 0);
652 LeaveThread(0, "", CMD_READX);
654 sprintf(temp, "File: read failed on index=%d, offset=%d ReadSize=%d ActualRead=%d handle=%p GLE(0x%x)\n",
655 handle, offset, size, ret, ftable[i].fd, GetLastError());
657 sprintf(temp, "File: %s. On read, cannot set file pointer GLE(0x%x)\n", ftable[i].name, GetLastError());
661 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
664 EndFirstTimer(CMD_READX, 1);
669 int nb_close(int handle)
676 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
678 if ((i = FindHandle(handle)) == -1)
682 ret = nb_close1(ftable[i].fd);
683 EndFirstTimer(CMD_CLOSE, ret);
686 LeaveThread(0, "", CMD_CLOSE);
687 sprintf(temp, "(%d) close failed on handle %d\n", LineCount, handle);
690 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
694 ftable[i].handle = 0;
697 free(ftable[i].name);
698 ftable[i].name = NULL;
702 int nb_rmdir(char *fname)
709 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
711 strcpy(path, AfsLocker);
715 rc = RemoveDirectory(path);
716 EndFirstTimer(CMD_RMDIR, rc);
720 LeaveThread(0, "", CMD_RMDIR);
721 sprintf(temp, "Directory: RemoveDirectory %s failed GLE(0x%x)\n", fname, GetLastError());
724 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
730 int nb_rename(char *old, char *New)
738 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
740 strcpy(opath, AfsLocker);
742 strcpy(npath, AfsLocker);
746 rc = MoveFileEx(opath, npath, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
747 EndFirstTimer(CMD_RENAME, rc);
751 LeaveThread(0, "", CMD_RENAME);
752 sprintf(temp, "File: rename %s %s failed GLE(0x%x)\n", old, New, GetLastError());
755 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
762 * Type is used to determine whether the file is expected
763 * to exist or not. It is overloaded (temporarily) to control
764 * Flag which indicates whether an error is treated as an error
765 * or not. The StreamFiles.txt script does not have the Type
766 * parameter set correctly for all 120,000+ lines. As a result
767 * it is not possible to enforce the presence test throughout
770 int nb_qpathinfo(char *fname, int Type)
779 EnforcePathInfoErrors = 1;
781 } else if (Type == 1001) {
782 EnforcePathInfoErrors = 0;
784 } else if (Type == 1000) {
785 EnforcePathInfoErrors = 0;
787 } else if (Type == 1110) {
788 EnforcePathInfoErrors = 1;
792 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
794 strcpy(path, AfsLocker);
798 rc = GetPathInfo(path, NULL, NULL, NULL, NULL, NULL);
800 gle = GetLastError();
801 if (strstr(fname, "~TS"))
809 if (!EnforcePathInfoErrors)
820 EndFirstTimer(CMD_QUERY_PATH_INFO, 0);
821 LeaveThread(0, "", CMD_QUERY_PATH_INFO);
822 sprintf(temp, "File: qpathinfo failed for %s type %d GLE(0x%x)\n", path, Type, gle);
825 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
829 EndFirstTimer(CMD_QUERY_PATH_INFO, 1);
833 int nb_qfileinfo(int handle)
840 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
842 if ((i = FindHandle(handle)) == -1)
846 rc = GetFileInfo(ftable[i].name, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL);
850 EndFirstTimer(CMD_QUERY_FILE_INFO, 0);
851 LeaveThread(0, "", CMD_QUERY_FILE_INFO);
852 sprintf(temp, "File: qfileinfo failed for %s GLE(0x%x)\n", ftable[i].name, GetLastError());
855 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
858 EndFirstTimer(CMD_QUERY_FILE_INFO, 1);
863 int nb_qfsinfo(int level)
873 ULARGE_INTEGER FreeBytesAvailable;
874 ULARGE_INTEGER TotalNumberOfBytes;
875 ULARGE_INTEGER TotalNumberOfFreeBytes;
877 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
878 sprintf(Path, "%s\\%s%05d", AfsLocker, HostName, LogID);
881 rc = GetDiskFreeSpaceEx(Path, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
882 // rc = cli_dskattr(c, &bsize, &total, &avail);
886 EndFirstTimer(CMD_QUERY_FS_INFO, 0);
887 LeaveThread(0, "", CMD_QUERY_FS_INFO);
888 sprintf(temp, "File: Disk free space failed GLE(0x%x)\n", GetLastError());
891 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
894 EndFirstTimer(CMD_QUERY_FS_INFO, 1);
899 void find_fn(file_info *finfo, char *name, void *state)
904 int nb_findfirst(char *mask)
911 if (strstr(mask, "<.JNK"))
914 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
916 strcpy(NewMask, AfsLocker);
917 strcat(NewMask, mask);
920 rc = GetFileList(NewMask, (void *)find_fn, NULL);
924 EndFirstTimer(CMD_FIND_FIRST, 0);
925 sprintf(temp, "File: findfirst cannot find for %s\n", mask);
928 LeaveThread(1, temp, CMD_FIND_FIRST);
929 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
932 EndFirstTimer(CMD_FIND_FIRST, 1);
936 int nb_flush(int handle)
940 if ((i = FindHandle(handle)) == -1)
943 FlushFileBuffers(ftable[i].fd);
947 static int total_deleted;
949 void delete_fn(file_info *finfo, const char *name, void *state)
956 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
958 if (finfo->mode & aDIR)
961 sprintf(s2, "%s\\*", name);
962 GetFileList(s2, delete_fn, NULL);
963 sprintf(s, "%s", &name[strlen(AfsLocker)]);
968 rc = DeleteFile(name);
971 LeaveThread(0, "", CMD_UNLINK);
972 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", name, GetLastError());
975 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
982 int nb_deltree(char *dname)
989 strcpy(path, AfsLocker);
991 sprintf(mask, "%s\\*", path);
996 GetFileList(mask, delete_fn, NULL);
998 // pstrcpy(path, AfsLocker);
999 // pstrcat(path, dname);
1000 rc = RemoveDirectory(path);
1001 EndFirstTimer(CMD_DELTREE, rc);
1008 rc = GetLastError();
1009 if ((rc != ERROR_FILE_NOT_FOUND) && (rc != ERROR_PATH_NOT_FOUND))
1012 LeaveThread(0, "", CMD_DELTREE);
1013 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1014 sprintf(temp, "ERROR: Thread %d - Unable to remove %s.\n", ProcessNumber, path);
1015 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1025 int nb_cleanup(char *cname)
1029 strcpy(temp, "\\clients\\client1");
1030 SubstituteString(temp, "client1", cname, sizeof(temp));
1031 SubstituteString(temp, "clients", HostName, sizeof(temp));
1036 int LeaveThread(int status, char *Reason, int cmd)
1043 ++ThreadCommandInfo[cmd].ErrorCount;
1045 if (strlen(Reason) == 0)
1048 rc = GetLastError();
1051 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1054 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1055 pExitStatus->Reason,
1056 sizeof(pExitStatus->Reason),
1058 LastKnownError = rc;
1061 strcpy(pExitStatus->Reason, Reason);
1063 if (strlen(pExitStatus->Reason) == 0)
1064 strcpy(pExitStatus->Reason, "\n");
1065 if (pExitStatus->Reason[strlen(pExitStatus->Reason) - 1] != '\n')
1066 strcat(pExitStatus->Reason, "\n");
1067 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1068 if (strlen(Reason) == 0)
1069 sprintf(temp, "ERROR(%d): Thread %d - (%d) %s", LineCount, ProcessNumber, rc, pExitStatus->Reason);
1071 sprintf(temp, "ERROR(%d): Thread %d - %s", LineCount, ProcessNumber, pExitStatus->Reason);
1072 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1075 pExitStatus->ExitStatus = status;
1077 DumpAFSLog(HostName, LogID);
1078 (*pThreadStatus) = 0;
1082 void StartFirstTimer(void)
1085 EndSecondTime(CMD_NONAFS);
1086 TickCount1 = GetTickCount();
1089 void EndFirstTimer(int cmd, int Type)
1094 ThreadCommandInfo[cmd].count++;
1096 MilliTick = GetTickCount() - TickCount1;
1099 StartSecondTime(CMD_NONAFS);
1103 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1104 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1106 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1112 ThreadCommandInfo[cmd].min_sec = cmd_time;
1113 StartSecondTime(CMD_NONAFS);
1118 ThreadCommandInfo[cmd].ErrorTime += cmd_time;
1121 ThreadCommandInfo[cmd].total_sec += cmd_time;
1122 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1123 ThreadCommandInfo[cmd].min_sec = cmd_time;
1124 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1125 ThreadCommandInfo[cmd].max_sec = cmd_time;
1128 StartSecondTime(CMD_NONAFS);
1131 void StartSecondTime(int cmd)
1134 TickCount2 = GetTickCount();
1138 void EndSecondTime(int cmd)
1143 ThreadCommandInfo[cmd].count++;
1145 MilliTick = GetTickCount() - TickCount2;
1150 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1151 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1153 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1158 ThreadCommandInfo[cmd].min_sec = cmd_time;
1161 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1162 ThreadCommandInfo[cmd].min_sec = cmd_time;
1163 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1164 ThreadCommandInfo[cmd].max_sec = cmd_time;
1165 ThreadCommandInfo[cmd].total_sec += cmd_time;
1169 intptr_t SystemCall(char *command)
1174 argv[0] = getenv("COMSPEC");
1177 argv[3] = (char *)command;
1180 rc = spawnve(_P_WAIT,argv[0],argv,NULL);
1181 // != -1 || (errno != ENOENT && errno != EACCES))
1185 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
1186 uint32 FileAttributes, uint32 ShareAccess,
1187 uint32 CreateDisposition, uint32 CreateOptions)
1190 DWORD dwCreateDisposition = 0;
1191 DWORD dwDesiredAccess = 0;
1192 DWORD dwShareAccess = 0;
1195 if (CreateOptions & FILE_DIRECTORY_FILE)
1199 if (!CreateDirectory(fname, NULL) && (rc = GetLastError()) != ERROR_ALREADY_EXISTS)
1202 fd = INVALID_HANDLE_VALUE;
1206 fd = CreateFile(fname, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1211 dwDesiredAccess = 0;
1212 if (DesiredAccess & FILE_READ_DATA)
1213 dwDesiredAccess |= GENERIC_READ;
1214 if (DesiredAccess & FILE_WRITE_DATA)
1215 dwDesiredAccess |= GENERIC_WRITE;
1216 dwShareAccess = ShareAccess;
1217 dwShareAccess |= FILE_SHARE_DELETE;
1218 dwCreateDisposition = OPEN_ALWAYS;
1219 if (CreateDisposition == 1)
1220 dwCreateDisposition = OPEN_EXISTING;
1221 fd = CreateFile(fname, dwDesiredAccess, ShareAccess, NULL, dwCreateDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1227 BOOL nb_close1(HANDLE fd)
1232 if (fd != INVALID_HANDLE_VALUE)
1234 if (rc = GetHandleInformation(fd, &dwFlags))
1240 /****************************************************************************
1241 do a directory listing, calling fn on each file found
1242 this uses the old SMBsearch interface. It is needed for testing Samba,
1243 but should otherwise not be used
1244 ****************************************************************************/
1245 int nb_list_old(const char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1247 int num_received = 0;
1250 char cFileName[1024];
1251 int dwFileAttributes;
1258 if (!strcmp(&mask[strlen(mask)-2], "\"*"))
1260 strcpy(&mask[strlen(mask)-2], "*");
1263 dwFileAttributes = 0;
1264 memset(cFileName, '\0', sizeof(cFileName));
1265 hFind = WinFindFirstFile(mask, &FileData, cFileName, &dwFileAttributes);
1266 if (hFind == INVALID_HANDLE_VALUE)
1270 mask[strlen(mask) - 1] = '\0';
1273 if (cFileName[0] != '.')
1277 memset(&finfo, '\0', sizeof(finfo));
1278 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1280 strcpy(finfo.name, cFileName);
1281 sprintf(temp, "%s%s", mask, cFileName);
1282 fn(&finfo, temp, state);
1285 memset(cFileName, '\0', sizeof(cFileName));
1286 dwFileAttributes = 0;
1287 if (!WinFindNextFile(hFind, &FileData, cFileName, &dwFileAttributes))
1291 return(num_received);
1295 /****************************************************************************
1296 do a directory listing, calling fn on each file found
1297 this auto-switches between old and new style
1298 ****************************************************************************/
1299 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1301 return(nb_list_old(Mask, fn, state));
1304 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes)
1307 static WIN32_FIND_DATAW FileDataW;
1308 static WIN32_FIND_DATAA FileDataA;
1310 memset(&FileDataA, '\0', sizeof(FileDataA));
1311 rc = FindFirstFile(Mask, &FileDataA);
1312 if (rc != INVALID_HANDLE_VALUE)
1314 (*FileData) = (void *)&FileDataA;
1315 (*dwFileAttributes) = FileDataA.dwFileAttributes;
1316 strcpy(cFileName, FileDataA.cFileName);
1322 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes)
1325 WIN32_FIND_DATAA *FileDataA;
1327 FileDataA = (WIN32_FIND_DATAA *)(*FileData);
1328 if (!(rc = FindNextFile(hFind, FileDataA)))
1332 (*dwFileAttributes) = FileDataA->dwFileAttributes;
1333 strcpy(cFileName, FileDataA->cFileName);
1338 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len)
1343 if (!insert || !pattern || !s) return;
1345 ls = (ssize_t)strlen(s);
1346 lp = (ssize_t)strlen(pattern);
1347 li = (ssize_t)strlen(insert);
1349 if (!*pattern) return;
1351 while (lp <= ls && (p = strstr(s,pattern)))
1353 if (len && (ls + (li-lp) >= (int)len))
1359 memmove(p+li,p+lp,strlen(p+lp)+1);
1361 memcpy(p, insert, li);
1367 #define CHANGE_TIME(A,B) \
1368 B.tm_hour = A.wHour; \
1369 B.tm_sec = A.wSecond; \
1370 B.tm_min = A.wMinute; \
1371 B.tm_mon = A.wMonth - 1; \
1373 B.tm_year = A.wYear - 1900; \
1374 B.tm_wday = A.wDayOfWeek - 1; \
1379 BOOL GetPathInfo(const char *fname,
1380 time_t *c_time, time_t *a_time, time_t *m_time,
1381 size_t *size, uint16 *mode)
1383 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1385 SYSTEMTIME SystemTime;
1388 rc = GetFileAttributesEx(fname, GetFileExInfoStandard, &FileInfo);
1393 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1394 CHANGE_TIME(SystemTime, tm_time)
1395 (*c_time) = mktime(&tm_time);
1399 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1400 CHANGE_TIME(SystemTime, tm_time)
1401 (*a_time) = mktime(&tm_time);
1405 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1406 CHANGE_TIME(SystemTime, tm_time)
1407 (*m_time) = mktime(&tm_time);
1412 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1413 *size = FileInfo.nFileSizeLow;
1424 /****************************************************************************
1425 send a qfileinfo call
1426 ****************************************************************************/
1427 BOOL GetFileInfo(char *FileName, HANDLE fd,
1428 uint16 *mode, size_t *size,
1429 time_t *c_time, time_t *a_time, time_t *m_time,
1432 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1434 SYSTEMTIME SystemTime;
1437 rc = GetFileAttributesEx(FileName, GetFileExInfoStandard, &FileInfo);
1442 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1443 CHANGE_TIME(SystemTime, tm_time)
1444 (*c_time) = mktime(&tm_time);
1448 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1449 CHANGE_TIME(SystemTime, tm_time)
1450 (*a_time) = mktime(&tm_time);
1454 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1455 CHANGE_TIME(SystemTime, tm_time)
1456 (*m_time) = mktime(&tm_time);
1461 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1462 *size = FileInfo.nFileSizeLow;
1473 /****************************************************************************
1474 Read size bytes at offset offset using SMBreadX.
1475 ****************************************************************************/
1477 ssize_t nb_read(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1486 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1488 if (LowDword == INVALID_SET_FILE_POINTER)
1490 rc = ReadFile(fd, IoBuffer, (DWORD)size, &total, NULL);
1497 /****************************************************************************
1499 ****************************************************************************/
1501 ssize_t nb_write(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1507 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1508 if (LowDword == INVALID_SET_FILE_POINTER)
1510 rc = WriteFile(fd, IoBuffer, (DWORD)size, &bwritten, NULL);
1513 FlushFileBuffers(fd);