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)
91 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
93 strcpy(path, AfsLocker);
97 rc = DeleteFile(path);
99 EndFirstTimer(CMD_UNLINK, 1);
102 LeaveThread(0, "", CMD_UNLINK);
103 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", path, gle);
106 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
113 int nb_SetLocker(char *Locker)
117 if (strlen(Locker) == 0)
118 strcpy(AfsLocker, OriginalAfsLocker);
120 strcpy(AfsLocker, Locker);
121 EndFirstTimer(CMD_SETLOCKER, 1);
125 int nb_Xrmdir(char *Directory, char *type)
130 char NewDirectory[256];
133 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
134 if (strlen(Directory) == 0)
136 return(LeaveThread(1, "rmdir failed no path specified\n", CMD_XRMDIR));
138 strcpy(NewDirectory, Directory);
139 memset(command, '\0', sizeof(command));
140 strcpy(command,"rmdir /Q ");
141 if (!stricmp(type, "all"))
143 strcat(command, "/S ");
145 strcat(command, NewDirectory);
148 rc = system(command);
150 if ((rc) && (rc != 2) && (rc != 3))
152 EndFirstTimer(CMD_XRMDIR, 0);
153 sprintf(temp, "rmdir failed on %s\n", command);
154 LeaveThread(rc, temp, CMD_XRMDIR);
155 sprintf(temp, "FAILURE: Thread %d - Xrmdir failed on \"%s\"\n", ProcessNumber, command);
156 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
159 EndFirstTimer(CMD_XRMDIR, 1);
164 int nb_Mkdir(char *Directory)
169 char NewDirectory[256];
172 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
173 if (strlen(Directory) == 0)
175 return(LeaveThread(1, "mkdir failed on no path specified\n", CMD_MKDIR));
177 strcpy(NewDirectory, Directory);
178 memset(command, '\0', sizeof(command));
179 strcpy(command,"mkdir ");
180 strcat(command, NewDirectory);
183 rc = system(command);
187 EndFirstTimer(CMD_MKDIR, 0);
188 sprintf(temp, "mkdir failed on %s\n", command);
189 LeaveThread(rc, temp, CMD_MKDIR);
190 sprintf(temp, "ERROR: Thread %d - mkdir failed on \"%s\"\n", ProcessNumber, command);
191 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
194 EndFirstTimer(CMD_MKDIR, 1);
198 int nb_Attach(char *Locker, char *Drive)
205 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
206 if (strlen(Locker) == 0)
208 return(LeaveThread(1, "attach failed no locker specified\n", CMD_ATTACH));
210 memset(command, '\0', sizeof(command));
211 strcpy(command,"attach -q ");
213 if (strlen(Drive) != 0)
215 sprintf(temp, "-D %s ", Drive);
216 strcat(command, temp);
218 strcat(command, Locker);
221 rc = system(command);
225 EndFirstTimer(CMD_ATTACH, 0);
226 sprintf(pExitStatus->Reason, "attach failed on %s\n", command);
227 pExitStatus->ExitStatus = rc;
228 sprintf(temp, "ERROR: Thread %d - attach failed on \"%s\"\n", ProcessNumber, command);
229 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
231 EndFirstTimer(CMD_ATTACH, 1);
235 int nb_Detach(char *Name, char *type)
242 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
243 memset(command, '\0', sizeof(command));
244 strcpy(command,"detach -q ");
246 if (!stricmp(type, "drive"))
248 sprintf(temp, "-D %s ", Name);
249 strcat(command, temp);
251 else if (!stricmp(type, "locker"))
253 strcat(command, Name);
257 return(LeaveThread(1, "nb_Detach failed unknown type: %s\n", CMD_DETACH));
261 rc = system(command);
265 EndFirstTimer(CMD_DETACH, 0);
266 sprintf(temp, "detach failed on %s\n", command);
267 LeaveThread(rc, temp, CMD_DETACH);
268 sprintf(temp, "ERROR: Thread %d - detach failed on \"%s\"\n", ProcessNumber, command);
269 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
272 EndFirstTimer(CMD_DETACH, 1);
276 int nb_CreateFile(char *path, DWORD size)
289 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
290 if (strlen(path) == 0)
292 return(LeaveThread(1, "nb_CreateFile failed no path specified\n", CMD_CREATEFILE));
295 strcpy(NewPath, path);
298 fHandle = CreateFile(NewPath,
299 GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_ALL,
300 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
303 FILE_ATTRIBUTE_NORMAL,
305 gle = GetLastError();
306 if (fHandle == INVALID_HANDLE_VALUE)
308 EndFirstTimer(CMD_CREATEFILE, 0);
309 sprintf(temp, "Create file failed on \"%s\" GLE(0x%x)\n", NewPath, gle);
310 LeaveThread(0, temp, CMD_CREATEFILE);
311 sprintf(temp, "ERROR: Thread %d - Create file failed on \"%s\" GLE(0x%x)\n", ProcessNumber, NewPath, gle);
312 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
315 EndFirstTimer(CMD_CREATEFILE, 1);
316 Moved = SetFilePointer(fHandle,
317 size - sizeof(Buffer) <= 0 ? 0 : size - sizeof(Buffer),
320 memset(Buffer, 'A', sizeof(Buffer));
321 BytesToWrite = sizeof(Buffer);
322 rc = WriteFile(fHandle, Buffer, BytesToWrite, &BytesWritten, NULL);
324 FlushFileBuffers(fHandle);
325 CloseHandle(fHandle);
330 int nb_CopyFile(char *Source, char *Destination)
337 char NewDestination[256];
339 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
340 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
342 return(LeaveThread(1, "nb_CopyFile failed to copy files: either source or destination path not specified\n", CMD_COPYFILES));
344 strcpy(NewSource, Source);
345 strcpy(NewDestination, Destination);
347 memset(command, '\0', sizeof(command));
348 sprintf(command, "copy /V /Y /B %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
351 rc = system(command);
355 EndFirstTimer(CMD_COPYFILES, 0);
356 sprintf(temp, "copy failed on \"%s\"\n", command);
357 LeaveThread(rc, temp, CMD_COPYFILES);
358 sprintf(temp, "FAILURE: Thread %d - copy failed on \"%s\"\n", ProcessNumber, command);
359 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
364 EndFirstTimer(CMD_COPYFILES, 1);
368 int nb_DeleteFile(char *path)
376 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
377 if (strlen(path) == 0)
379 return(LeaveThread(1, "nb_DeleteFile failed to delete files: no path specified\n", CMD_DELETEFILES));
381 strcpy(NewPath, path);
383 memset(command, '\0', sizeof(command));
384 sprintf(command, "del /Q %s", NewPath);
387 rc = system(command);
391 EndFirstTimer(CMD_DELETEFILES, 0);
392 sprintf(temp, "del failed on \"%s\"\n", NewPath);
393 LeaveThread(rc, temp, CMD_DELETEFILES);
394 sprintf(temp, "ERROR: Thread %d - del failed on \"%s\"\n", ProcessNumber, command);
395 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
398 EndFirstTimer(CMD_DELETEFILES, 1);
402 int nb_xcopy(char *Source, char *Destination)
409 char NewDestination[256];
411 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
412 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
414 return(LeaveThread(1, "nb_xcopy failed to xcopy: either source or destination is missing\n", CMD_XCOPY));
416 strcpy(NewSource, Source);
417 strcpy(NewDestination, Destination);
418 memset(command, '\0', sizeof(command));
419 sprintf(command, "xcopy /E /I /V /Y /Q %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
422 rc = SystemCall(command);
426 EndFirstTimer(CMD_XCOPY, 0);
427 sprintf(temp, "xcopy failed on %s\n", command);
428 LeaveThread((int)rc, temp, CMD_XCOPY);
429 sprintf(temp, "FAIURE: Thread %d - xcopy failed on \"%s\"\n", ProcessNumber, command);
430 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
433 EndFirstTimer(CMD_XCOPY, 1);
437 int nb_Move(char *Source, char *Destination)
444 char NewDestination[256];
446 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
447 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
449 return(LeaveThread(1, "nb_Move failed to move: either source or destination is missing\n", CMD_MOVE));
451 strcpy(NewSource, Source);
452 strcpy(NewDestination, Destination);
453 memset(command, '\0', sizeof(command));
454 sprintf(command, "move /Y %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
456 rc = system(command);
460 EndFirstTimer(CMD_MOVE, 0);
461 sprintf(temp, "move failed on \"%s\"\n", command);
462 LeaveThread(rc, temp, CMD_MOVE);
463 sprintf(temp, "FAILURE: Thread %d - move failed on \"%s\"\n", ProcessNumber, command);
464 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
467 EndFirstTimer(CMD_MOVE, 1);
471 int nb_createx(char *fname, unsigned create_options, unsigned create_disposition, int handle)
475 uint32 desired_access;
480 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
482 strcpy(path, AfsLocker);
484 if (create_options & FILE_DIRECTORY_FILE)
486 desired_access = FILE_READ_DATA;
490 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
494 fd = CreateObject(path,
497 FILE_SHARE_READ|FILE_SHARE_WRITE,
501 if (fd == INVALID_HANDLE_VALUE && handle != -1)
503 if (create_options & FILE_DIRECTORY_FILE)
505 EndFirstTimer(CMD_NTCREATEX, 0);
506 LeaveThread(0, "", CMD_NTCREATEX);
507 sprintf(temp, "Directory: unable to create directory %s\n", path);
510 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 (fd != INVALID_HANDLE_VALUE && handle == -1)
534 if (fd == INVALID_HANDLE_VALUE && handle == -1)
537 for (i = 0; i < MAX_FILES; i++)
539 if (ftable[i].handle == 0)
544 printf("(%d) file table full for %s\n", LineCount, path);
545 return(LeaveThread(1, "file table is full\n", CMD_NTCREATEX));
547 ftable[i].handle = handle;
550 free(ftable[i].name);
551 ftable[i].name = strdup(path);
552 ftable[i].reads = ftable[i].writes = 0;
556 int nb_writex(int handle, int offset, int size, int ret_size)
562 unsigned char magic = (unsigned char)getpid();
564 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
566 if (IoBuffer[0] != magic ||
567 IoBuffer[1] != magic ||
568 IoBuffer[2] != magic ||
569 IoBuffer[3] != magic)
570 memset(IoBuffer, magic, BufferSize);
572 if ((i = FindHandle(handle)) == -1)
575 status = nb_write(ftable[i].fd, IoBuffer, offset, size);
576 if (status != ret_size)
578 EndFirstTimer(CMD_WRITEX, 0);
579 LeaveThread(0, "", CMD_WRITEX);
581 sprintf(temp, "File: %s. wrote %d bytes, got %d bytes\n", ftable[i].name, size, status);
583 sprintf(temp, "File: %s. On write, cannot set file pointer\n", ftable[i].name);
587 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
590 EndFirstTimer(CMD_WRITEX, 1);
596 int nb_lock(int handle, int offset, int size, int timeout, unsigned char locktype, NTSTATUS exp)
604 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
606 if ((i = FindHandle(handle)) == -1)
608 LeaveThread(0, "", CMD_LOCKINGX);
609 sprintf(temp, "File unlock: Cannot find handle for %s", ftable[i].name);
612 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
617 ret = cli_locktype(c, i, offset, size, timeout, locktype);
621 EndFirstTimer(CMD_LOCKINGX, 0);
622 LeaveThread(0, "", CMD_LOCKINGX);
623 sprintf(temp, "(%d) ERROR: lock failed on handle %d ofs=%d size=%d timeout= %d exp=%d fd %d errno %d (%s)\n",
624 LineCount, handle, offset, size, timeout, exp, ftable[i].fd, exp, "");
625 sprintf(temp, "File unlock: lock failed %s", ftable[i].name);
628 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
631 EndFirstTimer(CMD_LOCKINGX, 1);
636 int nb_readx(int handle, int offset, int size, int ret_size)
644 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
646 if ((i = FindHandle(handle)) == -1)
650 ret = nb_read(ftable[i].fd, IoBuffer, offset, size);
651 gle = GetLastError();
652 if ((ret != size) && (ret != ret_size))
654 EndFirstTimer(CMD_READX, 0);
655 LeaveThread(0, "", CMD_READX);
657 sprintf(temp, "File: read failed on index=%d, offset=%d ReadSize=%d ActualRead=%d handle=%p GLE(0x%x)\n",
658 handle, offset, size, ret, ftable[i].fd, gle);
660 sprintf(temp, "File: %s. On read, cannot set file pointer GLE(0x%x)\n", ftable[i].name, gle);
664 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
667 EndFirstTimer(CMD_READX, 1);
672 int nb_close(int handle)
679 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
681 if ((i = FindHandle(handle)) == -1)
685 ret = nb_close1(ftable[i].fd);
686 EndFirstTimer(CMD_CLOSE, ret);
689 LeaveThread(0, "", CMD_CLOSE);
690 sprintf(temp, "(%d) close failed on handle %d\n", LineCount, handle);
693 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
697 ftable[i].handle = 0;
700 free(ftable[i].name);
701 ftable[i].name = NULL;
705 int nb_rmdir(char *fname)
713 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
715 strcpy(path, AfsLocker);
719 rc = RemoveDirectory(path);
720 gle = GetLastError();
721 EndFirstTimer(CMD_RMDIR, rc);
725 LeaveThread(0, "", CMD_RMDIR);
726 sprintf(temp, "Directory: RemoveDirectory %s failed GLE(0x%x)\n", fname, gle);
729 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
735 int nb_rename(char *old, char *New)
744 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
746 strcpy(opath, AfsLocker);
748 strcpy(npath, AfsLocker);
752 rc = MoveFileEx(opath, npath, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
753 gle = GetLastError();
754 EndFirstTimer(CMD_RENAME, rc);
758 LeaveThread(0, "", CMD_RENAME);
759 sprintf(temp, "File: rename %s %s failed GLE(0x%x)\n", old, New, gle);
762 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
769 * Type is used to determine whether the file is expected
770 * to exist or not. It is overloaded (temporarily) to control
771 * Flag which indicates whether an error is treated as an error
772 * or not. The StreamFiles.txt script does not have the Type
773 * parameter set correctly for all 120,000+ lines. As a result
774 * it is not possible to enforce the presence test throughout
777 int nb_qpathinfo(char *fname, int Type)
786 EnforcePathInfoErrors = 1;
788 } else if (Type == 1001) {
789 EnforcePathInfoErrors = 0;
791 } else if (Type == 1000) {
792 EnforcePathInfoErrors = 0;
794 } else if (Type == 1110) {
795 EnforcePathInfoErrors = 1;
799 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
801 strcpy(path, AfsLocker);
805 rc = GetPathInfo(path, NULL, NULL, NULL, NULL, NULL);
807 gle = GetLastError();
808 if (strstr(fname, "~TS"))
816 if (!EnforcePathInfoErrors)
827 EndFirstTimer(CMD_QUERY_PATH_INFO, 0);
828 LeaveThread(0, "", CMD_QUERY_PATH_INFO);
829 sprintf(temp, "File: qpathinfo failed for %s type %d GLE(0x%x)\n", path, Type, gle);
832 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
836 EndFirstTimer(CMD_QUERY_PATH_INFO, 1);
840 int nb_qfileinfo(int handle)
848 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
850 if ((i = FindHandle(handle)) == -1)
854 rc = GetFileInfo(ftable[i].name, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL);
855 gle = GetLastError();
858 EndFirstTimer(CMD_QUERY_FILE_INFO, 0);
859 LeaveThread(0, "", CMD_QUERY_FILE_INFO);
860 sprintf(temp, "File: qfileinfo failed for %s GLE(0x%x)\n", ftable[i].name, gle);
863 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
866 EndFirstTimer(CMD_QUERY_FILE_INFO, 1);
871 int nb_qfsinfo(int level)
881 ULARGE_INTEGER FreeBytesAvailable;
882 ULARGE_INTEGER TotalNumberOfBytes;
883 ULARGE_INTEGER TotalNumberOfFreeBytes;
886 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
887 sprintf(Path, "%s\\%s%05d", AfsLocker, HostName, LogID);
890 rc = GetDiskFreeSpaceEx(Path, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
891 // rc = cli_dskattr(c, &bsize, &total, &avail);
892 gle = GetLastError();
896 EndFirstTimer(CMD_QUERY_FS_INFO, 0);
897 LeaveThread(0, "", CMD_QUERY_FS_INFO);
898 sprintf(temp, "File: Disk free space failed GLE(0x%x)\n", gle);
901 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
904 EndFirstTimer(CMD_QUERY_FS_INFO, 1);
909 void find_fn(file_info *finfo, char *name, void *state)
914 int nb_findfirst(char *mask)
921 if (strstr(mask, "<.JNK"))
924 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
926 strcpy(NewMask, AfsLocker);
927 strcat(NewMask, mask);
930 rc = GetFileList(NewMask, (void *)find_fn, NULL);
934 EndFirstTimer(CMD_FIND_FIRST, 0);
935 sprintf(temp, "File: findfirst cannot find for %s\n", mask);
938 LeaveThread(1, temp, CMD_FIND_FIRST);
939 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
942 EndFirstTimer(CMD_FIND_FIRST, 1);
946 int nb_flush(int handle)
950 if ((i = FindHandle(handle)) == -1)
953 FlushFileBuffers(ftable[i].fd);
957 static int total_deleted;
959 void delete_fn(file_info *finfo, const char *name, void *state)
967 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
969 if (finfo->mode & aDIR)
972 sprintf(s2, "%s\\*", name);
973 GetFileList(s2, delete_fn, NULL);
974 sprintf(s, "%s", &name[strlen(AfsLocker)]);
979 rc = DeleteFile(name);
980 gle = GetLastError();
983 LeaveThread(0, "", CMD_UNLINK);
984 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", name, gle);
987 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
994 int nb_deltree(char *dname)
1001 strcpy(path, AfsLocker);
1002 strcat(path, dname);
1003 sprintf(mask, "%s\\*", path);
1008 GetFileList(mask, delete_fn, NULL);
1010 // pstrcpy(path, AfsLocker);
1011 // pstrcat(path, dname);
1012 rc = RemoveDirectory(path);
1013 gle = GetLastError();
1014 EndFirstTimer(CMD_DELTREE, rc);
1020 if ((gle != ERROR_FILE_NOT_FOUND) && (gle != ERROR_PATH_NOT_FOUND))
1022 LeaveThread(0, "", CMD_DELTREE);
1023 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1024 sprintf(temp, "ERROR: Thread %d - Unable to remove %s.\n", ProcessNumber, path);
1025 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1036 int nb_cleanup(char *cname)
1040 strcpy(temp, "\\clients\\client1");
1041 SubstituteString(temp, "client1", cname, sizeof(temp));
1042 SubstituteString(temp, "clients", HostName, sizeof(temp));
1047 int LeaveThread(int status, char *Reason, int cmd)
1054 ++ThreadCommandInfo[cmd].ErrorCount;
1056 if (strlen(Reason) == 0)
1059 rc = GetLastError();
1062 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1065 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1066 pExitStatus->Reason,
1067 sizeof(pExitStatus->Reason),
1069 LastKnownError = rc;
1072 strcpy(pExitStatus->Reason, Reason);
1074 if (strlen(pExitStatus->Reason) == 0)
1075 strcpy(pExitStatus->Reason, "\n");
1076 if (pExitStatus->Reason[strlen(pExitStatus->Reason) - 1] != '\n')
1077 strcat(pExitStatus->Reason, "\n");
1078 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1079 if (strlen(Reason) == 0)
1080 sprintf(temp, "ERROR(%d): Thread %d - (%d) %s", LineCount, ProcessNumber, rc, pExitStatus->Reason);
1082 sprintf(temp, "ERROR(%d): Thread %d - %s", LineCount, ProcessNumber, pExitStatus->Reason);
1083 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1086 pExitStatus->ExitStatus = status;
1088 DumpAFSLog(HostName, LogID);
1089 (*pThreadStatus) = 0;
1093 void StartFirstTimer(void)
1096 EndSecondTime(CMD_NONAFS);
1097 TickCount1 = GetTickCount();
1100 void EndFirstTimer(int cmd, int Type)
1105 ThreadCommandInfo[cmd].count++;
1107 MilliTick = GetTickCount() - TickCount1;
1110 StartSecondTime(CMD_NONAFS);
1114 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1115 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1117 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1123 ThreadCommandInfo[cmd].min_sec = cmd_time;
1124 StartSecondTime(CMD_NONAFS);
1129 ThreadCommandInfo[cmd].ErrorTime += cmd_time;
1132 ThreadCommandInfo[cmd].total_sec += cmd_time;
1133 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1134 ThreadCommandInfo[cmd].min_sec = cmd_time;
1135 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1136 ThreadCommandInfo[cmd].max_sec = cmd_time;
1139 StartSecondTime(CMD_NONAFS);
1142 void StartSecondTime(int cmd)
1145 TickCount2 = GetTickCount();
1149 void EndSecondTime(int cmd)
1154 ThreadCommandInfo[cmd].count++;
1156 MilliTick = GetTickCount() - TickCount2;
1161 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1162 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1164 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1169 ThreadCommandInfo[cmd].min_sec = cmd_time;
1172 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1173 ThreadCommandInfo[cmd].min_sec = cmd_time;
1174 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1175 ThreadCommandInfo[cmd].max_sec = cmd_time;
1176 ThreadCommandInfo[cmd].total_sec += cmd_time;
1180 intptr_t SystemCall(char *command)
1185 argv[0] = getenv("COMSPEC");
1188 argv[3] = (char *)command;
1191 rc = spawnve(_P_WAIT,argv[0],argv,NULL);
1192 // != -1 || (errno != ENOENT && errno != EACCES))
1196 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
1197 uint32 FileAttributes, uint32 ShareAccess,
1198 uint32 CreateDisposition, uint32 CreateOptions)
1201 DWORD dwCreateDisposition = 0;
1202 DWORD dwDesiredAccess = 0;
1203 DWORD dwShareAccess = 0;
1206 if (CreateOptions & FILE_DIRECTORY_FILE)
1210 if (!CreateDirectory(fname, NULL) && (rc = GetLastError()) != ERROR_ALREADY_EXISTS)
1213 fd = INVALID_HANDLE_VALUE;
1217 fd = CreateFile(fname, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1222 dwDesiredAccess = 0;
1223 if (DesiredAccess & FILE_READ_DATA)
1224 dwDesiredAccess |= GENERIC_READ;
1225 if (DesiredAccess & FILE_WRITE_DATA)
1226 dwDesiredAccess |= GENERIC_WRITE;
1227 dwShareAccess = ShareAccess;
1228 dwShareAccess |= FILE_SHARE_DELETE;
1229 dwCreateDisposition = OPEN_ALWAYS;
1230 if (CreateDisposition == 1)
1231 dwCreateDisposition = OPEN_EXISTING;
1232 fd = CreateFile(fname, dwDesiredAccess, ShareAccess, NULL, dwCreateDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1238 BOOL nb_close1(HANDLE fd)
1243 if (fd != INVALID_HANDLE_VALUE)
1245 if (rc = GetHandleInformation(fd, &dwFlags))
1251 /****************************************************************************
1252 do a directory listing, calling fn on each file found
1253 this uses the old SMBsearch interface. It is needed for testing Samba,
1254 but should otherwise not be used
1255 ****************************************************************************/
1256 int nb_list_old(const char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1258 int num_received = 0;
1261 char cFileName[1024];
1262 int dwFileAttributes;
1269 if (!strcmp(&mask[strlen(mask)-2], "\"*"))
1271 strcpy(&mask[strlen(mask)-2], "*");
1274 dwFileAttributes = 0;
1275 memset(cFileName, '\0', sizeof(cFileName));
1276 hFind = WinFindFirstFile(mask, &FileData, cFileName, &dwFileAttributes);
1277 if (hFind == INVALID_HANDLE_VALUE)
1281 mask[strlen(mask) - 1] = '\0';
1284 if (cFileName[0] != '.')
1288 memset(&finfo, '\0', sizeof(finfo));
1289 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1291 strcpy(finfo.name, cFileName);
1292 sprintf(temp, "%s%s", mask, cFileName);
1293 fn(&finfo, temp, state);
1296 memset(cFileName, '\0', sizeof(cFileName));
1297 dwFileAttributes = 0;
1298 if (!WinFindNextFile(hFind, &FileData, cFileName, &dwFileAttributes))
1302 return(num_received);
1306 /****************************************************************************
1307 do a directory listing, calling fn on each file found
1308 this auto-switches between old and new style
1309 ****************************************************************************/
1310 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1312 return(nb_list_old(Mask, fn, state));
1315 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes)
1318 static WIN32_FIND_DATAW FileDataW;
1319 static WIN32_FIND_DATAA FileDataA;
1321 memset(&FileDataA, '\0', sizeof(FileDataA));
1322 rc = FindFirstFile(Mask, &FileDataA);
1323 if (rc != INVALID_HANDLE_VALUE)
1325 (*FileData) = (void *)&FileDataA;
1326 (*dwFileAttributes) = FileDataA.dwFileAttributes;
1327 strcpy(cFileName, FileDataA.cFileName);
1333 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes)
1336 WIN32_FIND_DATAA *FileDataA;
1338 FileDataA = (WIN32_FIND_DATAA *)(*FileData);
1339 if (!(rc = FindNextFile(hFind, FileDataA)))
1343 (*dwFileAttributes) = FileDataA->dwFileAttributes;
1344 strcpy(cFileName, FileDataA->cFileName);
1349 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len)
1354 if (!insert || !pattern || !s) return;
1356 ls = (ssize_t)strlen(s);
1357 lp = (ssize_t)strlen(pattern);
1358 li = (ssize_t)strlen(insert);
1360 if (!*pattern) return;
1362 while (lp <= ls && (p = strstr(s,pattern)))
1364 if (len && (ls + (li-lp) >= (int)len))
1370 memmove(p+li,p+lp,strlen(p+lp)+1);
1372 memcpy(p, insert, li);
1378 #define CHANGE_TIME(A,B) \
1379 B.tm_hour = A.wHour; \
1380 B.tm_sec = A.wSecond; \
1381 B.tm_min = A.wMinute; \
1382 B.tm_mon = A.wMonth - 1; \
1384 B.tm_year = A.wYear - 1900; \
1385 B.tm_wday = A.wDayOfWeek - 1; \
1390 BOOL GetPathInfo(const char *fname,
1391 time_t *c_time, time_t *a_time, time_t *m_time,
1392 size_t *size, uint16 *mode)
1394 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1396 SYSTEMTIME SystemTime;
1399 rc = GetFileAttributesEx(fname, GetFileExInfoStandard, &FileInfo);
1404 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1405 CHANGE_TIME(SystemTime, tm_time)
1406 (*c_time) = mktime(&tm_time);
1410 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1411 CHANGE_TIME(SystemTime, tm_time)
1412 (*a_time) = mktime(&tm_time);
1416 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1417 CHANGE_TIME(SystemTime, tm_time)
1418 (*m_time) = mktime(&tm_time);
1423 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1424 *size = FileInfo.nFileSizeLow;
1435 /****************************************************************************
1436 send a qfileinfo call
1437 ****************************************************************************/
1438 BOOL GetFileInfo(char *FileName, HANDLE fd,
1439 uint16 *mode, size_t *size,
1440 time_t *c_time, time_t *a_time, time_t *m_time,
1443 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1445 SYSTEMTIME SystemTime;
1448 rc = GetFileAttributesEx(FileName, GetFileExInfoStandard, &FileInfo);
1453 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1454 CHANGE_TIME(SystemTime, tm_time)
1455 (*c_time) = mktime(&tm_time);
1459 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1460 CHANGE_TIME(SystemTime, tm_time)
1461 (*a_time) = mktime(&tm_time);
1465 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1466 CHANGE_TIME(SystemTime, tm_time)
1467 (*m_time) = mktime(&tm_time);
1472 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1473 *size = FileInfo.nFileSizeLow;
1484 /****************************************************************************
1485 Read size bytes at offset offset using SMBreadX.
1486 ****************************************************************************/
1488 ssize_t nb_read(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1497 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1499 if (LowDword == INVALID_SET_FILE_POINTER)
1501 rc = ReadFile(fd, IoBuffer, (DWORD)size, &total, NULL);
1508 /****************************************************************************
1510 ****************************************************************************/
1512 ssize_t nb_write(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1518 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1519 if (LowDword == INVALID_SET_FILE_POINTER)
1521 rc = WriteFile(fd, IoBuffer, (DWORD)size, &bwritten, NULL);
1524 FlushFileBuffers(fd);