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);
158 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
161 EndFirstTimer(CMD_XRMDIR, 1);
166 int nb_Mkdir(char *Directory)
171 char NewDirectory[256];
174 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
175 if (strlen(Directory) == 0)
177 return(LeaveThread(1, "mkdir failed on no path specified\n", CMD_MKDIR));
179 strcpy(NewDirectory, Directory);
180 memset(command, '\0', sizeof(command));
181 strcpy(command,"mkdir ");
182 strcat(command, NewDirectory);
185 rc = system(command);
189 EndFirstTimer(CMD_MKDIR, 0);
190 sprintf(temp, "mkdir failed on %s\n", command);
191 LeaveThread(rc, temp, CMD_MKDIR);
192 sprintf(temp, "ERROR: Thread %d - mkdir failed on \"%s\"\n", ProcessNumber, command);
195 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
198 EndFirstTimer(CMD_MKDIR, 1);
202 int nb_Attach(char *Locker, char *Drive)
209 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
210 if (strlen(Locker) == 0)
212 return(LeaveThread(1, "attach failed no locker specified\n", CMD_ATTACH));
214 memset(command, '\0', sizeof(command));
215 strcpy(command,"attach -q ");
217 if (strlen(Drive) != 0)
219 sprintf(temp, "-D %s ", Drive);
220 strcat(command, temp);
222 strcat(command, Locker);
225 rc = system(command);
229 EndFirstTimer(CMD_ATTACH, 0);
230 sprintf(pExitStatus->Reason, "attach failed on %s\n", command);
231 pExitStatus->ExitStatus = rc;
232 sprintf(temp, "ERROR: Thread %d - attach failed on \"%s\"\n", ProcessNumber, command);
235 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
237 EndFirstTimer(CMD_ATTACH, 1);
241 int nb_Detach(char *Name, char *type)
248 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
249 memset(command, '\0', sizeof(command));
250 strcpy(command,"detach -q ");
252 if (!stricmp(type, "drive"))
254 sprintf(temp, "-D %s ", Name);
255 strcat(command, temp);
257 else if (!stricmp(type, "locker"))
259 strcat(command, Name);
263 return(LeaveThread(1, "nb_Detach failed unknown type: %s\n", CMD_DETACH));
267 rc = system(command);
271 EndFirstTimer(CMD_DETACH, 0);
272 sprintf(temp, "detach failed on %s\n", command);
273 LeaveThread(rc, temp, CMD_DETACH);
274 sprintf(temp, "ERROR: Thread %d - detach failed on \"%s\"\n", ProcessNumber, command);
277 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
280 EndFirstTimer(CMD_DETACH, 1);
284 int nb_CreateFile(char *path, DWORD size)
297 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
298 if (strlen(path) == 0)
300 return(LeaveThread(1, "nb_CreateFile failed no path specified\n", CMD_CREATEFILE));
303 strcpy(NewPath, path);
306 fHandle = CreateFile(NewPath,
307 GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_ALL,
308 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
311 FILE_ATTRIBUTE_NORMAL,
313 gle = GetLastError();
314 if (fHandle == INVALID_HANDLE_VALUE)
316 EndFirstTimer(CMD_CREATEFILE, 0);
317 sprintf(temp, "Create file failed on \"%s\" GLE(0x%x)\n", NewPath, gle);
318 LeaveThread(0, temp, CMD_CREATEFILE);
319 sprintf(temp, "ERROR: Thread %d - Create file failed on \"%s\" GLE(0x%x)\n", ProcessNumber, NewPath, gle);
322 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
325 EndFirstTimer(CMD_CREATEFILE, 1);
326 Moved = SetFilePointer(fHandle,
327 size - sizeof(Buffer) <= 0 ? 0 : size - sizeof(Buffer),
330 memset(Buffer, 'A', sizeof(Buffer));
331 BytesToWrite = sizeof(Buffer);
332 rc = WriteFile(fHandle, Buffer, BytesToWrite, &BytesWritten, NULL);
334 FlushFileBuffers(fHandle);
335 CloseHandle(fHandle);
340 int nb_CopyFile(char *Source, char *Destination)
347 char NewDestination[256];
349 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
350 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
352 return(LeaveThread(1, "nb_CopyFile failed to copy files: either source or destination path not specified\n", CMD_COPYFILES));
354 strcpy(NewSource, Source);
355 strcpy(NewDestination, Destination);
357 memset(command, '\0', sizeof(command));
358 sprintf(command, "copy /V /Y /B %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
361 rc = system(command);
365 EndFirstTimer(CMD_COPYFILES, 0);
366 sprintf(temp, "copy failed on \"%s\"\n", command);
367 LeaveThread(rc, temp, CMD_COPYFILES);
368 sprintf(temp, "FAILURE: Thread %d - copy failed on \"%s\"\n", ProcessNumber, command);
369 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
374 EndFirstTimer(CMD_COPYFILES, 1);
378 int nb_DeleteFile(char *path)
386 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
387 if (strlen(path) == 0)
389 return(LeaveThread(1, "nb_DeleteFile failed to delete files: no path specified\n", CMD_DELETEFILES));
391 strcpy(NewPath, path);
393 memset(command, '\0', sizeof(command));
394 sprintf(command, "del /Q %s", NewPath);
397 rc = system(command);
401 EndFirstTimer(CMD_DELETEFILES, 0);
402 sprintf(temp, "del failed on \"%s\"\n", NewPath);
403 LeaveThread(rc, temp, CMD_DELETEFILES);
404 sprintf(temp, "ERROR: Thread %d - del failed on \"%s\"\n", ProcessNumber, command);
407 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
410 EndFirstTimer(CMD_DELETEFILES, 1);
414 int nb_xcopy(char *Source, char *Destination)
421 char NewDestination[256];
423 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
424 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
426 return(LeaveThread(1, "nb_xcopy failed to xcopy: either source or destination is missing\n", CMD_XCOPY));
428 strcpy(NewSource, Source);
429 strcpy(NewDestination, Destination);
430 memset(command, '\0', sizeof(command));
431 sprintf(command, "xcopy /E /I /V /Y /Q %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
434 rc = SystemCall(command);
438 EndFirstTimer(CMD_XCOPY, 0);
439 sprintf(temp, "xcopy failed on %s\n", command);
440 LeaveThread((int)rc, temp, CMD_XCOPY);
441 sprintf(temp, "FAIURE: Thread %d - xcopy failed on \"%s\"\n", ProcessNumber, command);
444 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
447 EndFirstTimer(CMD_XCOPY, 1);
451 int nb_Move(char *Source, char *Destination)
458 char NewDestination[256];
460 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
461 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
463 return(LeaveThread(1, "nb_Move failed to move: either source or destination is missing\n", CMD_MOVE));
465 strcpy(NewSource, Source);
466 strcpy(NewDestination, Destination);
467 memset(command, '\0', sizeof(command));
468 sprintf(command, "move /Y %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
470 rc = system(command);
474 EndFirstTimer(CMD_MOVE, 0);
475 sprintf(temp, "move failed on \"%s\"\n", command);
476 LeaveThread(rc, temp, CMD_MOVE);
477 sprintf(temp, "FAILURE: Thread %d - move failed on \"%s\"\n", ProcessNumber, command);
480 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
483 EndFirstTimer(CMD_MOVE, 1);
487 int nb_createx(char *fname, unsigned create_options, unsigned create_disposition, int handle)
491 uint32 desired_access;
496 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
498 strcpy(path, AfsLocker);
500 if (create_options & FILE_DIRECTORY_FILE)
502 desired_access = FILE_READ_DATA;
506 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
510 fd = CreateObject(path,
513 FILE_SHARE_READ|FILE_SHARE_WRITE,
517 if (fd == INVALID_HANDLE_VALUE && handle != -1)
519 if (create_options & FILE_DIRECTORY_FILE)
521 EndFirstTimer(CMD_NTCREATEX, 0);
522 LeaveThread(0, "", CMD_NTCREATEX);
523 sprintf(temp, "Directory: unable to create directory %s\n", path);
526 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
531 EndFirstTimer(CMD_NTCREATEX, 0);
532 LeaveThread(0, "", CMD_NTCREATEX);
533 sprintf(temp, "File: unable to create file %s\n", path);
536 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
541 EndFirstTimer(CMD_NTCREATEX, 1);
543 if (fd != INVALID_HANDLE_VALUE && handle == -1)
550 if (fd == INVALID_HANDLE_VALUE && handle == -1)
553 for (i = 0; i < MAX_FILES; i++)
555 if (ftable[i].handle == 0)
560 printf("(%d) file table full for %s\n", LineCount, path);
561 return(LeaveThread(1, "file table is full\n", CMD_NTCREATEX));
563 ftable[i].handle = handle;
566 free(ftable[i].name);
567 ftable[i].name = strdup(path);
568 ftable[i].reads = ftable[i].writes = 0;
572 int nb_writex(int handle, int offset, int size, int ret_size)
578 unsigned char magic = (unsigned char)getpid();
580 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
582 if (IoBuffer[0] != magic ||
583 IoBuffer[1] != magic ||
584 IoBuffer[2] != magic ||
585 IoBuffer[3] != magic)
586 memset(IoBuffer, magic, BufferSize);
588 if ((i = FindHandle(handle)) == -1)
591 status = nb_write(ftable[i].fd, IoBuffer, offset, size);
592 if (status != ret_size)
594 EndFirstTimer(CMD_WRITEX, 0);
595 LeaveThread(0, "", CMD_WRITEX);
597 sprintf(temp, "File: %s. wrote %d bytes, got %d bytes\n", ftable[i].name, size, status);
599 sprintf(temp, "File: %s. On write, cannot set file pointer\n", ftable[i].name);
605 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
608 EndFirstTimer(CMD_WRITEX, 1);
614 int nb_lock(int handle, int offset, int size, int timeout, unsigned char locktype, NTSTATUS exp)
622 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
624 if ((i = FindHandle(handle)) == -1)
626 LeaveThread(0, "", CMD_LOCKINGX);
627 sprintf(temp, "File unlock: Cannot find handle for %s", ftable[i].name);
630 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
635 ret = cli_locktype(c, i, offset, size, timeout, locktype);
639 EndFirstTimer(CMD_LOCKINGX, 0);
640 LeaveThread(0, "", CMD_LOCKINGX);
641 sprintf(temp, "(%d) ERROR: lock failed on handle %d ofs=%d size=%d timeout= %d exp=%d fd %d errno %d (%s)\n",
642 LineCount, handle, offset, size, timeout, exp, ftable[i].fd, exp, "");
643 sprintf(temp, "File unlock: lock failed %s", ftable[i].name);
646 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
649 EndFirstTimer(CMD_LOCKINGX, 1);
654 int nb_readx(int handle, int offset, int size, int ret_size)
662 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
664 if ((i = FindHandle(handle)) == -1)
668 ret = nb_read(ftable[i].fd, IoBuffer, offset, size);
669 gle = GetLastError();
670 if ((ret != size) && (ret != ret_size))
672 EndFirstTimer(CMD_READX, 0);
673 LeaveThread(0, "", CMD_READX);
675 sprintf(temp, "File: read failed on index=%d, offset=%d ReadSize=%d ActualRead=%d handle=%p GLE(0x%x)\n",
676 handle, offset, size, ret, ftable[i].fd, gle);
678 sprintf(temp, "File: %s. On read, cannot set file pointer GLE(0x%x)\n", ftable[i].name, gle);
682 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
685 EndFirstTimer(CMD_READX, 1);
690 int nb_close(int handle)
697 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
699 if ((i = FindHandle(handle)) == -1)
703 ret = nb_close1(ftable[i].fd);
704 EndFirstTimer(CMD_CLOSE, ret);
707 LeaveThread(0, "", CMD_CLOSE);
708 sprintf(temp, "(%d) close failed on handle %d\n", LineCount, handle);
711 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
715 ftable[i].handle = 0;
718 free(ftable[i].name);
719 ftable[i].name = NULL;
723 int nb_rmdir(char *fname)
731 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
733 strcpy(path, AfsLocker);
737 rc = RemoveDirectory(path);
738 gle = GetLastError();
739 EndFirstTimer(CMD_RMDIR, rc);
743 LeaveThread(0, "", CMD_RMDIR);
744 sprintf(temp, "Directory: RemoveDirectory %s failed GLE(0x%x)\n", fname, gle);
747 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
753 int nb_rename(char *old, char *New)
762 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
764 strcpy(opath, AfsLocker);
766 strcpy(npath, AfsLocker);
770 rc = MoveFileEx(opath, npath, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
771 gle = GetLastError();
772 EndFirstTimer(CMD_RENAME, rc);
776 LeaveThread(0, "", CMD_RENAME);
777 sprintf(temp, "File: rename %s %s failed GLE(0x%x)\n", old, New, gle);
780 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
787 * Type is used to determine whether the file is expected
788 * to exist or not. It is overloaded (temporarily) to control
789 * Flag which indicates whether an error is treated as an error
790 * or not. The StreamFiles.txt script does not have the Type
791 * parameter set correctly for all 120,000+ lines. As a result
792 * it is not possible to enforce the presence test throughout
795 int nb_qpathinfo(char *fname, int Type)
804 EnforcePathInfoErrors = 1;
806 } else if (Type == 1001) {
807 EnforcePathInfoErrors = 0;
809 } else if (Type == 1000) {
810 EnforcePathInfoErrors = 0;
812 } else if (Type == 1110) {
813 EnforcePathInfoErrors = 1;
817 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
819 strcpy(path, AfsLocker);
823 rc = GetPathInfo(path, NULL, NULL, NULL, NULL, NULL);
825 gle = GetLastError();
826 if (strstr(fname, "~TS"))
834 if (!EnforcePathInfoErrors)
845 EndFirstTimer(CMD_QUERY_PATH_INFO, 0);
846 LeaveThread(0, "", CMD_QUERY_PATH_INFO);
847 sprintf(temp, "File: qpathinfo failed for %s type %d GLE(0x%x)\n", path, Type, gle);
850 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
854 EndFirstTimer(CMD_QUERY_PATH_INFO, 1);
858 int nb_qfileinfo(int handle)
866 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
868 if ((i = FindHandle(handle)) == -1)
872 rc = GetFileInfo(ftable[i].name, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL);
873 gle = GetLastError();
876 EndFirstTimer(CMD_QUERY_FILE_INFO, 0);
877 LeaveThread(0, "", CMD_QUERY_FILE_INFO);
878 sprintf(temp, "File: qfileinfo failed for %s GLE(0x%x)\n", ftable[i].name, gle);
881 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
884 EndFirstTimer(CMD_QUERY_FILE_INFO, 1);
889 int nb_qfsinfo(int level)
899 ULARGE_INTEGER FreeBytesAvailable;
900 ULARGE_INTEGER TotalNumberOfBytes;
901 ULARGE_INTEGER TotalNumberOfFreeBytes;
904 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
905 sprintf(Path, "%s\\%s%05d", AfsLocker, HostName, LogID);
908 rc = GetDiskFreeSpaceEx(Path, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
909 // rc = cli_dskattr(c, &bsize, &total, &avail);
910 gle = GetLastError();
914 EndFirstTimer(CMD_QUERY_FS_INFO, 0);
915 LeaveThread(0, "", CMD_QUERY_FS_INFO);
916 sprintf(temp, "File: Disk free space failed GLE(0x%x)\n", gle);
919 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
922 EndFirstTimer(CMD_QUERY_FS_INFO, 1);
927 void find_fn(file_info *finfo, char *name, void *state)
932 int nb_findfirst(char *mask)
939 if (strstr(mask, "<.JNK"))
942 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
944 strcpy(NewMask, AfsLocker);
945 strcat(NewMask, mask);
948 rc = GetFileList(NewMask, (void *)find_fn, NULL);
952 EndFirstTimer(CMD_FIND_FIRST, 0);
953 sprintf(temp, "File: findfirst cannot find for %s\n", mask);
956 LeaveThread(1, temp, CMD_FIND_FIRST);
957 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
960 EndFirstTimer(CMD_FIND_FIRST, 1);
964 int nb_flush(int handle)
968 if ((i = FindHandle(handle)) == -1)
971 FlushFileBuffers(ftable[i].fd);
975 static int total_deleted;
977 void delete_fn(file_info *finfo, const char *name, void *state)
985 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
987 if (finfo->mode & aDIR)
990 sprintf(s2, "%s\\*", name);
991 GetFileList(s2, delete_fn, NULL);
992 sprintf(s, "%s", &name[strlen(AfsLocker)]);
997 rc = DeleteFile(name);
998 gle = GetLastError();
1001 LeaveThread(0, "", CMD_UNLINK);
1002 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", name, gle);
1005 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1012 int nb_deltree(char *dname)
1019 strcpy(path, AfsLocker);
1020 strcat(path, dname);
1021 sprintf(mask, "%s\\*", path);
1026 GetFileList(mask, delete_fn, NULL);
1028 // pstrcpy(path, AfsLocker);
1029 // pstrcat(path, dname);
1030 rc = RemoveDirectory(path);
1031 gle = GetLastError();
1032 EndFirstTimer(CMD_DELTREE, rc);
1038 if ((gle != ERROR_FILE_NOT_FOUND) && (gle != ERROR_PATH_NOT_FOUND))
1040 LeaveThread(0, "", CMD_DELTREE);
1041 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1042 sprintf(temp, "ERROR: Thread %d - Unable to remove %s.\n", ProcessNumber, path);
1043 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1054 int nb_cleanup(char *cname)
1058 strcpy(temp, "\\clients\\client1");
1059 SubstituteString(temp, "client1", cname, sizeof(temp));
1060 SubstituteString(temp, "clients", HostName, sizeof(temp));
1065 int LeaveThread(int status, char *Reason, int cmd)
1072 ++ThreadCommandInfo[cmd].ErrorCount;
1074 if (strlen(Reason) == 0)
1077 rc = GetLastError();
1080 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1083 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1084 pExitStatus->Reason,
1085 sizeof(pExitStatus->Reason),
1087 LastKnownError = rc;
1090 strcpy(pExitStatus->Reason, Reason);
1092 if (strlen(pExitStatus->Reason) == 0)
1093 strcpy(pExitStatus->Reason, "\n");
1094 if (pExitStatus->Reason[strlen(pExitStatus->Reason) - 1] != '\n')
1095 strcat(pExitStatus->Reason, "\n");
1096 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1097 if (strlen(Reason) == 0)
1098 sprintf(temp, "ERROR(%d): Thread %d - (%d) %s", LineCount, ProcessNumber, rc, pExitStatus->Reason);
1100 sprintf(temp, "ERROR(%d): Thread %d - %s", LineCount, ProcessNumber, pExitStatus->Reason);
1101 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1104 pExitStatus->ExitStatus = status;
1106 DumpAFSLog(HostName, LogID);
1107 (*pThreadStatus) = 0;
1111 void StartFirstTimer(void)
1114 EndSecondTime(CMD_NONAFS);
1115 TickCount1 = GetTickCount();
1118 void EndFirstTimer(int cmd, int Type)
1123 ThreadCommandInfo[cmd].count++;
1125 MilliTick = GetTickCount() - TickCount1;
1128 StartSecondTime(CMD_NONAFS);
1132 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1133 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1135 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1141 ThreadCommandInfo[cmd].min_sec = cmd_time;
1142 StartSecondTime(CMD_NONAFS);
1147 ThreadCommandInfo[cmd].ErrorTime += cmd_time;
1150 ThreadCommandInfo[cmd].total_sec += cmd_time;
1151 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1152 ThreadCommandInfo[cmd].min_sec = cmd_time;
1153 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1154 ThreadCommandInfo[cmd].max_sec = cmd_time;
1157 StartSecondTime(CMD_NONAFS);
1160 void StartSecondTime(int cmd)
1163 TickCount2 = GetTickCount();
1167 void EndSecondTime(int cmd)
1172 ThreadCommandInfo[cmd].count++;
1174 MilliTick = GetTickCount() - TickCount2;
1179 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1180 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1182 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1187 ThreadCommandInfo[cmd].min_sec = cmd_time;
1190 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1191 ThreadCommandInfo[cmd].min_sec = cmd_time;
1192 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1193 ThreadCommandInfo[cmd].max_sec = cmd_time;
1194 ThreadCommandInfo[cmd].total_sec += cmd_time;
1198 intptr_t SystemCall(char *command)
1203 argv[0] = getenv("COMSPEC");
1206 argv[3] = (char *)command;
1209 rc = spawnve(_P_WAIT,argv[0],argv,NULL);
1210 // != -1 || (errno != ENOENT && errno != EACCES))
1214 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
1215 uint32 FileAttributes, uint32 ShareAccess,
1216 uint32 CreateDisposition, uint32 CreateOptions)
1219 DWORD dwCreateDisposition = 0;
1220 DWORD dwDesiredAccess = 0;
1221 DWORD dwShareAccess = 0;
1224 if (CreateOptions & FILE_DIRECTORY_FILE)
1228 if (!CreateDirectory(fname, NULL) && (rc = GetLastError()) != ERROR_ALREADY_EXISTS)
1231 fd = INVALID_HANDLE_VALUE;
1235 fd = CreateFile(fname, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1240 dwDesiredAccess = 0;
1241 if (DesiredAccess & FILE_READ_DATA)
1242 dwDesiredAccess |= GENERIC_READ;
1243 if (DesiredAccess & FILE_WRITE_DATA)
1244 dwDesiredAccess |= GENERIC_WRITE;
1245 dwShareAccess = ShareAccess;
1246 dwShareAccess |= FILE_SHARE_DELETE;
1247 dwCreateDisposition = OPEN_ALWAYS;
1248 if (CreateDisposition == 1)
1249 dwCreateDisposition = OPEN_EXISTING;
1250 fd = CreateFile(fname, dwDesiredAccess, ShareAccess, NULL, dwCreateDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1256 BOOL nb_close1(HANDLE fd)
1261 if (fd != INVALID_HANDLE_VALUE)
1263 if (rc = GetHandleInformation(fd, &dwFlags))
1269 /****************************************************************************
1270 do a directory listing, calling fn on each file found
1271 this uses the old SMBsearch interface. It is needed for testing Samba,
1272 but should otherwise not be used
1273 ****************************************************************************/
1274 int nb_list_old(const char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1276 int num_received = 0;
1279 char cFileName[1024];
1280 int dwFileAttributes;
1287 if (!strcmp(&mask[strlen(mask)-2], "\"*"))
1289 strcpy(&mask[strlen(mask)-2], "*");
1292 dwFileAttributes = 0;
1293 memset(cFileName, '\0', sizeof(cFileName));
1294 hFind = WinFindFirstFile(mask, &FileData, cFileName, &dwFileAttributes);
1295 if (hFind == INVALID_HANDLE_VALUE)
1299 mask[strlen(mask) - 1] = '\0';
1302 if (cFileName[0] != '.')
1306 memset(&finfo, '\0', sizeof(finfo));
1307 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1309 strcpy(finfo.name, cFileName);
1310 sprintf(temp, "%s%s", mask, cFileName);
1311 fn(&finfo, temp, state);
1314 memset(cFileName, '\0', sizeof(cFileName));
1315 dwFileAttributes = 0;
1316 if (!WinFindNextFile(hFind, &FileData, cFileName, &dwFileAttributes))
1320 return(num_received);
1324 /****************************************************************************
1325 do a directory listing, calling fn on each file found
1326 this auto-switches between old and new style
1327 ****************************************************************************/
1328 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1330 return(nb_list_old(Mask, fn, state));
1333 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes)
1336 static WIN32_FIND_DATAW FileDataW;
1337 static WIN32_FIND_DATAA FileDataA;
1339 memset(&FileDataA, '\0', sizeof(FileDataA));
1340 rc = FindFirstFile(Mask, &FileDataA);
1341 if (rc != INVALID_HANDLE_VALUE)
1343 (*FileData) = (void *)&FileDataA;
1344 (*dwFileAttributes) = FileDataA.dwFileAttributes;
1345 strcpy(cFileName, FileDataA.cFileName);
1351 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes)
1354 WIN32_FIND_DATAA *FileDataA;
1356 FileDataA = (WIN32_FIND_DATAA *)(*FileData);
1357 if (!(rc = FindNextFile(hFind, FileDataA)))
1361 (*dwFileAttributes) = FileDataA->dwFileAttributes;
1362 strcpy(cFileName, FileDataA->cFileName);
1367 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len)
1372 if (!insert || !pattern || !s) return;
1374 ls = (ssize_t)strlen(s);
1375 lp = (ssize_t)strlen(pattern);
1376 li = (ssize_t)strlen(insert);
1378 if (!*pattern) return;
1380 while (lp <= ls && (p = strstr(s,pattern)))
1382 if (len && (ls + (li-lp) >= (int)len))
1388 memmove(p+li,p+lp,strlen(p+lp)+1);
1390 memcpy(p, insert, li);
1396 #define CHANGE_TIME(A,B) \
1397 B.tm_hour = A.wHour; \
1398 B.tm_sec = A.wSecond; \
1399 B.tm_min = A.wMinute; \
1400 B.tm_mon = A.wMonth - 1; \
1402 B.tm_year = A.wYear - 1900; \
1403 B.tm_wday = A.wDayOfWeek - 1; \
1408 BOOL GetPathInfo(const char *fname,
1409 time_t *c_time, time_t *a_time, time_t *m_time,
1410 size_t *size, uint16 *mode)
1412 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1414 SYSTEMTIME SystemTime;
1417 rc = GetFileAttributesEx(fname, GetFileExInfoStandard, &FileInfo);
1422 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1423 CHANGE_TIME(SystemTime, tm_time)
1424 (*c_time) = mktime(&tm_time);
1428 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1429 CHANGE_TIME(SystemTime, tm_time)
1430 (*a_time) = mktime(&tm_time);
1434 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1435 CHANGE_TIME(SystemTime, tm_time)
1436 (*m_time) = mktime(&tm_time);
1441 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1442 *size = FileInfo.nFileSizeLow;
1453 /****************************************************************************
1454 send a qfileinfo call
1455 ****************************************************************************/
1456 BOOL GetFileInfo(char *FileName, HANDLE fd,
1457 uint16 *mode, size_t *size,
1458 time_t *c_time, time_t *a_time, time_t *m_time,
1461 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1463 SYSTEMTIME SystemTime;
1466 rc = GetFileAttributesEx(FileName, GetFileExInfoStandard, &FileInfo);
1471 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1472 CHANGE_TIME(SystemTime, tm_time)
1473 (*c_time) = mktime(&tm_time);
1477 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1478 CHANGE_TIME(SystemTime, tm_time)
1479 (*a_time) = mktime(&tm_time);
1483 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1484 CHANGE_TIME(SystemTime, tm_time)
1485 (*m_time) = mktime(&tm_time);
1490 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1491 *size = FileInfo.nFileSizeLow;
1502 /****************************************************************************
1503 Read size bytes at offset offset using SMBreadX.
1504 ****************************************************************************/
1506 ssize_t nb_read(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1515 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1517 if (LowDword == INVALID_SET_FILE_POINTER)
1519 rc = ReadFile(fd, IoBuffer, (DWORD)size, &total, NULL);
1526 /****************************************************************************
1528 ****************************************************************************/
1530 ssize_t nb_write(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1536 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1537 if (LowDword == INVALID_SET_FILE_POINTER)
1539 rc = WriteFile(fd, IoBuffer, (DWORD)size, &bwritten, NULL);
1542 FlushFileBuffers(fd);