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 int BufferSize;
36 __declspec( thread ) extern char AfsLocker[256];
37 __declspec( thread ) extern char OriginalAfsLocker[256];
38 __declspec( thread ) extern char HostName[256];
39 __declspec( thread ) extern DWORD TickCount1, TickCount2, MilliTickStart;
40 __declspec( thread ) extern FTABLE ftable[MAX_FILES];
41 __declspec( thread ) extern struct cmd_struct ThreadCommandInfo[CMD_MAX_CMD + 1];
42 __declspec( thread ) extern EXIT_STATUS *pExitStatus;
43 __declspec( thread ) extern DWORD LastKnownError;
44 extern void LogMessage(int ProcessNumber, char *HostName, char *FileName, char *message, int LogID);
46 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
47 uint32 FileAttributes, uint32 ShareAccess,
48 uint32 CreateDisposition, uint32 CreateOptions);
49 void DumpAFSLog(char * HostName, int LogID);
50 int FindHandle(int handle);
51 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state);
52 BOOL GetFileInfo(char *FileName, HANDLE fd, uint16 *mode, size_t *size,
53 time_t *c_time, time_t *a_time, time_t *m_time,
55 BOOL GetPathInfo(const char *fname, time_t *c_time, time_t *a_time, time_t *m_time,
56 size_t *size, uint16 *mode);
57 int LeaveThread(int status, char *Reason, int cmd);
58 void StartFirstTimer();
59 void EndFirstTimer(int cmd, int Type);
60 void StartSecondTime(int cmd);
61 void EndSecondTime(int cmd);
62 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len);
63 intptr_t SystemCall(char *command);
64 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes);
65 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes);
67 int FindHandle(int handle)
70 for (i=0;i<MAX_FILES;i++)
72 if (ftable[i].handle == handle)
76 printf("(%d) ERROR: handle %d was not found\n", LineCount, handle);
77 return(LeaveThread(1, "", -1));
80 int nb_unlink(char *fname)
87 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
89 strcpy(path, AfsLocker);
93 rc = DeleteFile(path);
94 EndFirstTimer(CMD_UNLINK, 1);
97 LeaveThread(0, "", CMD_UNLINK);
98 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", fname, GetLastError());
101 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
108 int nb_SetLocker(char *Locker)
112 if (strlen(Locker) == 0)
113 strcpy(AfsLocker, OriginalAfsLocker);
115 strcpy(AfsLocker, Locker);
116 EndFirstTimer(CMD_SETLOCKER, 1);
120 int nb_Xrmdir(char *Directory, char *type)
125 char NewDirectory[256];
128 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
129 if (strlen(Directory) == 0)
131 return(LeaveThread(1, "rmdir failed no path specified\n", CMD_XRMDIR));
133 strcpy(NewDirectory, Directory);
134 memset(command, '\0', sizeof(command));
135 strcpy(command,"rmdir /Q ");
136 if (!stricmp(type, "all"))
138 strcat(command, "/S ");
140 strcat(command, NewDirectory);
143 rc = system(command);
145 if ((rc) && (rc != 2) && (rc != 3))
147 EndFirstTimer(CMD_XRMDIR, 0);
148 sprintf(temp, "rmdir failed on %s\n", command);
149 LeaveThread(rc, temp, CMD_XRMDIR);
150 sprintf(temp, "FAILURE: Thread %d - Xrmdir failed on \"%s\"\n", ProcessNumber, command);
151 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
154 EndFirstTimer(CMD_XRMDIR, 1);
159 int nb_Mkdir(char *Directory)
164 char NewDirectory[256];
167 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
168 if (strlen(Directory) == 0)
170 return(LeaveThread(1, "mkdir failed on no path specified\n", CMD_MKDIR));
172 strcpy(NewDirectory, Directory);
173 memset(command, '\0', sizeof(command));
174 strcpy(command,"mkdir ");
175 strcat(command, NewDirectory);
178 rc = system(command);
182 EndFirstTimer(CMD_MKDIR, 0);
183 sprintf(temp, "mkdir failed on %s\n", command);
184 LeaveThread(rc, temp, CMD_MKDIR);
185 sprintf(temp, "ERROR: Thread %d - mkdir failed on \"%s\"\n", ProcessNumber, command);
186 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
189 EndFirstTimer(CMD_MKDIR, 1);
193 int nb_Attach(char *Locker, char *Drive)
200 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
201 if (strlen(Locker) == 0)
203 return(LeaveThread(1, "attach failed no locker specified\n", CMD_ATTACH));
205 memset(command, '\0', sizeof(command));
206 strcpy(command,"attach -q ");
208 if (strlen(Drive) != 0)
210 sprintf(temp, "-D %s ", Drive);
211 strcat(command, temp);
213 strcat(command, Locker);
216 rc = system(command);
220 EndFirstTimer(CMD_ATTACH, 0);
221 sprintf(pExitStatus->Reason, "attach failed on %s\n", command);
222 pExitStatus->ExitStatus = rc;
223 sprintf(temp, "ERROR: Thread %d - attach failed on \"%s\"\n", ProcessNumber, command);
224 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
226 EndFirstTimer(CMD_ATTACH, 1);
230 int nb_Detach(char *Name, char *type)
237 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
238 memset(command, '\0', sizeof(command));
239 strcpy(command,"detach -q ");
241 if (!stricmp(type, "drive"))
243 sprintf(temp, "-D %s ", Name);
244 strcat(command, temp);
246 else if (!stricmp(type, "locker"))
248 strcat(command, Name);
252 return(LeaveThread(1, "nb_Detach failed unknown type: %s\n", CMD_DETACH));
256 rc = system(command);
260 EndFirstTimer(CMD_DETACH, 0);
261 sprintf(temp, "detach failed on %s\n", command);
262 LeaveThread(rc, temp, CMD_DETACH);
263 sprintf(temp, "ERROR: Thread %d - detach failed on \"%s\"\n", ProcessNumber, command);
264 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
267 EndFirstTimer(CMD_DETACH, 1);
271 int nb_CreateFile(char *path, DWORD size)
283 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
284 if (strlen(path) == 0)
286 return(LeaveThread(1, "nb_DeleteFile failed no path specified\n", CMD_CREATEFILE));
289 strcpy(NewPath, path);
292 fHandle = CreateFile(NewPath,
293 GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_ALL,
294 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
297 FILE_ATTRIBUTE_NORMAL,
300 if (fHandle == INVALID_HANDLE_VALUE)
303 EndFirstTimer(CMD_CREATEFILE, 0);
304 sprintf(temp, "Create file failed on \"%s\" GLE(0x%x)\n", NewPath, rc);
305 LeaveThread(0, temp, CMD_CREATEFILE);
306 sprintf(temp, "ERROR: Thread %d - Create file failed on \"%s\" GLE(0x%x)\n", ProcessNumber, NewPath, rc);
307 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
310 EndFirstTimer(CMD_CREATEFILE, 1);
311 Moved = SetFilePointer(fHandle,
315 memset(Buffer, 'A', sizeof(Buffer));
316 BytesToWrite = sizeof(Buffer);
317 rc = WriteFile(fHandle, Buffer, BytesToWrite, &BytesWritten, NULL);
319 FlushFileBuffers(fHandle);
320 CloseHandle(fHandle);
325 int nb_CopyFile(char *Source, char *Destination)
332 char NewDestination[256];
334 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
335 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
337 return(LeaveThread(1, "nb_CopyFile failed to copy files: either source or destination path not specified\n", CMD_COPYFILES));
339 strcpy(NewSource, Source);
340 strcpy(NewDestination, Destination);
342 memset(command, '\0', sizeof(command));
343 sprintf(command, "copy /V /Y /B %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
346 rc = system(command);
350 EndFirstTimer(CMD_COPYFILES, 0);
351 sprintf(temp, "copy failed on \"%s\"\n", command);
352 LeaveThread(rc, temp, CMD_COPYFILES);
353 sprintf(temp, "FAILURE: Thread %d - copy failed on \"%s\"\n", ProcessNumber, command);
354 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
359 EndFirstTimer(CMD_COPYFILES, 1);
363 int nb_DeleteFile(char *path)
371 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
372 if (strlen(path) == 0)
374 return(LeaveThread(1, "nb_DeleteFile failed to delete files: no path specified\n", CMD_DELETEFILES));
376 strcpy(NewPath, path);
378 memset(command, '\0', sizeof(command));
379 sprintf(command, "del /Q %s", NewPath);
382 rc = system(command);
386 EndFirstTimer(CMD_DELETEFILES, 0);
387 sprintf(temp, "del failed on \"%s\"\n", NewPath);
388 LeaveThread(rc, temp, CMD_DELETEFILES);
389 sprintf(temp, "ERROR: Thread %d - del failed on \"%s\"\n", ProcessNumber, command);
390 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
393 EndFirstTimer(CMD_DELETEFILES, 1);
397 int nb_xcopy(char *Source, char *Destination)
404 char NewDestination[256];
406 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
407 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
409 return(LeaveThread(1, "nb_xcopy failed to xcopy: either source or destination is missing\n", CMD_XCOPY));
411 strcpy(NewSource, Source);
412 strcpy(NewDestination, Destination);
413 memset(command, '\0', sizeof(command));
414 sprintf(command, "xcopy /E /I /V /Y /Q %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
417 rc = SystemCall(command);
421 EndFirstTimer(CMD_XCOPY, 0);
422 sprintf(temp, "xcopy failed on %s\n", command);
423 LeaveThread((int)rc, temp, CMD_XCOPY);
424 sprintf(temp, "FAIURE: Thread %d - xcopy failed on \"%s\"\n", ProcessNumber, command);
425 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
428 EndFirstTimer(CMD_XCOPY, 1);
432 int nb_Move(char *Source, char *Destination)
439 char NewDestination[256];
441 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
442 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
444 return(LeaveThread(1, "nb_Move failed to move: either source or destination is missing\n", CMD_MOVE));
446 strcpy(NewSource, Source);
447 strcpy(NewDestination, Destination);
448 memset(command, '\0', sizeof(command));
449 sprintf(command, "move /Y %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
451 rc = system(command);
455 EndFirstTimer(CMD_MOVE, 0);
456 sprintf(temp, "move failed on \"%s\"\n", command);
457 LeaveThread(rc, temp, CMD_MOVE);
458 sprintf(temp, "FAILURE: Thread %d - move failed on \"%s\"\n", ProcessNumber, command);
459 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
462 EndFirstTimer(CMD_MOVE, 1);
466 int nb_createx(char *fname, unsigned create_options, unsigned create_disposition, int handle)
470 uint32 desired_access;
475 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
477 strcpy(path, AfsLocker);
479 if (create_options & FILE_DIRECTORY_FILE)
481 desired_access = FILE_READ_DATA;
485 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
489 fd = CreateObject(path,
492 FILE_SHARE_READ|FILE_SHARE_WRITE,
496 if (fd == INVALID_HANDLE_VALUE && handle != -1)
498 if (create_options & FILE_DIRECTORY_FILE)
500 EndFirstTimer(CMD_NTCREATEX, 0);
501 LeaveThread(0, "", CMD_NTCREATEX);
502 sprintf(temp, "Directory: unable to create directory %s\n", path);
505 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
510 EndFirstTimer(CMD_NTCREATEX, 0);
511 LeaveThread(0, "", CMD_NTCREATEX);
512 sprintf(temp, "File: unable to create file %s\n", path);
515 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
520 EndFirstTimer(CMD_NTCREATEX, 1);
522 if (fd != INVALID_HANDLE_VALUE && handle == -1)
529 if (fd == INVALID_HANDLE_VALUE && handle == -1)
532 for (i = 0; i < MAX_FILES; i++)
534 if (ftable[i].handle == 0)
539 printf("(%d) file table full for %s\n", LineCount, path);
540 return(LeaveThread(1, "file table is full\n", CMD_NTCREATEX));
542 ftable[i].handle = handle;
545 free(ftable[i].name);
546 ftable[i].name = strdup(path);
547 ftable[i].reads = ftable[i].writes = 0;
551 int nb_writex(int handle, int offset, int size, int ret_size)
558 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
560 if (IoBuffer[0] == 0)
561 memset(IoBuffer, 1, BufferSize);
563 if ((i = FindHandle(handle)) == -1)
566 status = nb_write(ftable[i].fd, IoBuffer, offset, size);
567 if (status != ret_size)
569 EndFirstTimer(CMD_WRITEX, 0);
570 LeaveThread(0, "", CMD_WRITEX);
572 sprintf(temp, "File: %s. wrote %d bytes, got %d bytes\n", ftable[i].name, size, status);
574 sprintf(temp, "File: %s. On write, cannot set file pointer\n", ftable[i].name);
578 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
581 EndFirstTimer(CMD_WRITEX, 1);
587 int nb_lock(int handle, int offset, int size, int timeout, unsigned char locktype, NTSTATUS exp)
595 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
597 if ((i = FindHandle(handle)) == -1)
599 LeaveThread(0, "", CMD_LOCKINGX);
600 sprintf(temp, "File unlock: Cannot find handle for %s", ftable[i].name);
603 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
608 ret = cli_locktype(c, i, offset, size, timeout, locktype);
612 EndFirstTimer(CMD_LOCKINGX, 0);
613 LeaveThread(0, "", CMD_LOCKINGX);
614 sprintf(temp, "(%d) ERROR: lock failed on handle %d ofs=%d size=%d timeout= %d exp=%d fd %d errno %d (%s)\n",
615 LineCount, handle, offset, size, timeout, exp, ftable[i].fd, exp, "");
616 sprintf(temp, "File unlock: lock failed %s", ftable[i].name);
619 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
622 EndFirstTimer(CMD_LOCKINGX, 1);
627 int nb_readx(int handle, int offset, int size, int ret_size)
634 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
636 if ((i = FindHandle(handle)) == -1)
640 ret = nb_read(ftable[i].fd, IoBuffer, offset, size);
642 if ((ret != size) && (ret != ret_size))
644 EndFirstTimer(CMD_READX, 0);
645 LeaveThread(0, "", CMD_READX);
647 sprintf(temp, "File: read failed on index=%d, offset=%d ReadSize=%d ActualRead=%d handle=%p GLE(0x%x)\n",
648 handle, offset, size, ret, ftable[i].fd, GetLastError());
650 sprintf(temp, "File: %s. On read, cannot set file pointer GLE(0x%x)\n", ftable[i].name, GetLastError());
654 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
657 EndFirstTimer(CMD_READX, 1);
662 int nb_close(int handle)
669 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
671 if ((i = FindHandle(handle)) == -1)
675 ret = nb_close1(ftable[i].fd);
676 EndFirstTimer(CMD_CLOSE, ret);
679 LeaveThread(0, "", CMD_CLOSE);
680 sprintf(temp, "(%d) close failed on handle %d\n", LineCount, handle);
683 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
687 ftable[i].handle = 0;
690 free(ftable[i].name);
691 ftable[i].name = NULL;
695 int nb_rmdir(char *fname)
702 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
704 strcpy(path, AfsLocker);
708 rc = RemoveDirectory(path);
709 EndFirstTimer(CMD_RMDIR, rc);
713 LeaveThread(0, "", CMD_RMDIR);
714 sprintf(temp, "Directory: RemoveDirectory %s failed GLE(0x%x)\n", fname, GetLastError());
717 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
723 int nb_rename(char *old, char *New)
731 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
733 strcpy(opath, AfsLocker);
735 strcpy(npath, AfsLocker);
739 rc = MoveFileEx(opath, npath, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
740 EndFirstTimer(CMD_RENAME, rc);
744 LeaveThread(0, "", CMD_RENAME);
745 sprintf(temp, "File: rename %s %s failed GLE(0x%x)\n", old, New, GetLastError());
748 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
755 int nb_qpathinfo(char *fname, int Type)
766 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
768 strcpy(path, AfsLocker);
772 rc = GetPathInfo(path, NULL, NULL, NULL, NULL, NULL);
774 if (strstr(fname, "~TS"))
793 EndFirstTimer(CMD_QUERY_PATH_INFO, 0);
794 LeaveThread(0, "", CMD_QUERY_PATH_INFO);
795 sprintf(temp, "File: qpathinfo failed for %s GLE(0x%x)\n", path, GetLastError());
798 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
802 EndFirstTimer(CMD_QUERY_PATH_INFO, 1);
806 int nb_qfileinfo(int handle)
813 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
815 if ((i = FindHandle(handle)) == -1)
819 rc = GetFileInfo(ftable[i].name, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL);
823 EndFirstTimer(CMD_QUERY_FILE_INFO, 0);
824 LeaveThread(0, "", CMD_QUERY_FILE_INFO);
825 sprintf(temp, "File: qfileinfo failed for %s GLE(0x%x)\n", ftable[i].name, GetLastError());
828 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
831 EndFirstTimer(CMD_QUERY_FILE_INFO, 1);
836 int nb_qfsinfo(int level)
846 ULARGE_INTEGER FreeBytesAvailable;
847 ULARGE_INTEGER TotalNumberOfBytes;
848 ULARGE_INTEGER TotalNumberOfFreeBytes;
850 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
851 sprintf(Path, "%s\\%s%05d", AfsLocker, HostName, LogID);
854 rc = GetDiskFreeSpaceEx(Path, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
855 // rc = cli_dskattr(c, &bsize, &total, &avail);
859 EndFirstTimer(CMD_QUERY_FS_INFO, 0);
860 LeaveThread(0, "", CMD_QUERY_FS_INFO);
861 sprintf(temp, "File: Disk free space failed GLE(0x%x)\n", GetLastError());
864 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
867 EndFirstTimer(CMD_QUERY_FS_INFO, 1);
872 void find_fn(file_info *finfo, char *name, void *state)
877 int nb_findfirst(char *mask)
884 if (strstr(mask, "<.JNK"))
887 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
889 strcpy(NewMask, AfsLocker);
890 strcat(NewMask, mask);
893 rc = GetFileList(NewMask, (void *)find_fn, NULL);
897 EndFirstTimer(CMD_FIND_FIRST, 0);
898 sprintf(temp, "File: findfirst cannot find for %s\n", mask);
901 LeaveThread(1, temp, CMD_FIND_FIRST);
902 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
905 EndFirstTimer(CMD_FIND_FIRST, 1);
909 int nb_flush(int handle)
913 if ((i = FindHandle(handle)) == -1)
916 FlushFileBuffers(ftable[i].fd);
920 static int total_deleted;
922 void delete_fn(file_info *finfo, const char *name, void *state)
929 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
931 if (finfo->mode & aDIR)
934 sprintf(s2, "%s\\*", name);
935 GetFileList(s2, delete_fn, NULL);
936 sprintf(s, "%s", &name[strlen(AfsLocker)]);
941 rc = DeleteFile(name);
944 LeaveThread(0, "", CMD_UNLINK);
945 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", name, GetLastError());
948 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
955 int nb_deltree(char *dname)
962 strcpy(path, AfsLocker);
964 sprintf(mask, "%s\\*", path);
969 GetFileList(mask, delete_fn, NULL);
971 // pstrcpy(path, AfsLocker);
972 // pstrcat(path, dname);
973 rc = RemoveDirectory(path);
974 EndFirstTimer(CMD_DELTREE, rc);
982 if ((rc != ERROR_FILE_NOT_FOUND) && (rc != ERROR_PATH_NOT_FOUND))
985 LeaveThread(0, "", CMD_DELTREE);
986 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
987 sprintf(temp, "ERROR: Thread %d - Unable to remove %s.\n", ProcessNumber, path);
988 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
998 int nb_cleanup(char *cname)
1002 strcpy(temp, "\\clients\\client1");
1003 SubstituteString(temp, "client1", cname, sizeof(temp));
1004 SubstituteString(temp, "clients", HostName, sizeof(temp));
1009 int LeaveThread(int status, char *Reason, int cmd)
1016 ++ThreadCommandInfo[cmd].ErrorCount;
1018 if (strlen(Reason) == 0)
1021 rc = GetLastError();
1024 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1027 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1028 pExitStatus->Reason,
1029 sizeof(pExitStatus->Reason),
1031 LastKnownError = rc;
1034 strcpy(pExitStatus->Reason, Reason);
1036 if (strlen(pExitStatus->Reason) == 0)
1037 strcpy(pExitStatus->Reason, "\n");
1038 if (pExitStatus->Reason[strlen(pExitStatus->Reason) - 1] != '\n')
1039 strcat(pExitStatus->Reason, "\n");
1040 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1041 if (strlen(Reason) == 0)
1042 sprintf(temp, "ERROR(%d): Thread %d - (%d) %s", LineCount, ProcessNumber, rc, pExitStatus->Reason);
1044 sprintf(temp, "ERROR(%d): Thread %d - %s", LineCount, ProcessNumber, pExitStatus->Reason);
1045 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1048 pExitStatus->ExitStatus = status;
1050 DumpAFSLog(HostName, LogID);
1051 (*pThreadStatus) = 0;
1055 void StartFirstTimer(void)
1058 EndSecondTime(CMD_NONAFS);
1059 TickCount1 = GetTickCount();
1062 void EndFirstTimer(int cmd, int Type)
1067 ThreadCommandInfo[cmd].count++;
1069 MilliTick = GetTickCount() - TickCount1;
1072 StartSecondTime(CMD_NONAFS);
1076 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1077 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1079 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1085 ThreadCommandInfo[cmd].min_sec = cmd_time;
1086 StartSecondTime(CMD_NONAFS);
1091 ThreadCommandInfo[cmd].ErrorTime += cmd_time;
1094 ThreadCommandInfo[cmd].total_sec += cmd_time;
1095 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1096 ThreadCommandInfo[cmd].min_sec = cmd_time;
1097 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1098 ThreadCommandInfo[cmd].max_sec = cmd_time;
1101 StartSecondTime(CMD_NONAFS);
1104 void StartSecondTime(int cmd)
1107 TickCount2 = GetTickCount();
1111 void EndSecondTime(int cmd)
1116 ThreadCommandInfo[cmd].count++;
1118 MilliTick = GetTickCount() - TickCount2;
1123 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1124 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1126 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1131 ThreadCommandInfo[cmd].min_sec = cmd_time;
1134 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1135 ThreadCommandInfo[cmd].min_sec = cmd_time;
1136 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1137 ThreadCommandInfo[cmd].max_sec = cmd_time;
1138 ThreadCommandInfo[cmd].total_sec += cmd_time;
1142 intptr_t SystemCall(char *command)
1147 argv[0] = getenv("COMSPEC");
1150 argv[3] = (char *)command;
1153 rc = spawnve(_P_WAIT,argv[0],argv,NULL);
1154 // != -1 || (errno != ENOENT && errno != EACCES))
1158 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
1159 uint32 FileAttributes, uint32 ShareAccess,
1160 uint32 CreateDisposition, uint32 CreateOptions)
1163 DWORD dwCreateDisposition = 0;
1164 DWORD dwDesiredAccess = 0;
1165 DWORD dwShareAccess = 0;
1168 if (CreateOptions & FILE_DIRECTORY_FILE)
1172 if (!CreateDirectory(fname, NULL) && (rc = GetLastError()) != ERROR_ALREADY_EXISTS)
1175 fd = INVALID_HANDLE_VALUE;
1179 fd = CreateFile(fname, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
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);