2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1997-1998
5 Copyright (C) Richard Sharpe 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 __declspec( thread ) extern int ProcessNumber;
30 __declspec( thread ) extern int LineCount;
31 __declspec( thread ) extern int AfsTrace;
32 __declspec( thread ) extern int *pThreadStatus;
33 __declspec( thread ) extern int LogID;
34 __declspec( thread ) extern char *IoBuffer;
35 __declspec( thread ) extern char AfsLocker[256];
36 __declspec( thread ) extern char OriginalAfsLocker[256];
37 __declspec( thread ) extern char HostName[256];
38 __declspec( thread ) extern DWORD TickCount1, TickCount2, MilliTickStart;
39 __declspec( thread ) extern FTABLE ftable[MAX_FILES];
40 __declspec( thread ) extern struct cmd_struct ThreadCommandInfo[CMD_MAX_CMD + 1];
41 __declspec( thread ) extern EXIT_STATUS *pExitStatus;
42 __declspec( thread ) extern DWORD LastKnownError;
43 extern void LogMessage(int ProcessNumber, char *HostName, char *FileName, char *message, int LogID);
45 int CreateObject(const char *fname, uint32 DesiredAccess,
46 uint32 FileAttributes, uint32 ShareAccess,
47 uint32 CreateDisposition, uint32 CreateOptions);
48 void DumpAFSLog(char * HostName, int LogID);
49 int FindHandle(int handle);
50 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state);
51 BOOL GetFileInfo(char *FileName, int fnum, uint16 *mode, size_t *size,
52 time_t *c_time, time_t *a_time, time_t *m_time,
54 BOOL GetPathInfo(const char *fname, time_t *c_time, time_t *a_time, time_t *m_time,
55 size_t *size, uint16 *mode);
56 int LeaveThread(int status, char *Reason, int cmd);
57 void StartFirstTimer();
58 void EndFirstTimer(int cmd, int Type);
59 void StartSecondTime(int cmd);
60 void EndSecondTime(int cmd);
61 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len);
62 int SystemCall(char *command);
63 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes);
64 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes);
66 int FindHandle(int handle)
69 for (i=0;i<MAX_FILES;i++)
71 if (ftable[i].handle == handle)
75 printf("(%d) ERROR: handle %d was not found\n", LineCount, handle);
76 return(LeaveThread(1, "", -1));
79 int nb_unlink(char *fname)
86 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
88 strcpy(path, AfsLocker);
92 rc = DeleteFile(path);
93 EndFirstTimer(CMD_UNLINK, 1);
96 LeaveThread(0, "", CMD_UNLINK);
97 sprintf(temp, "FILE: DeleteFile %s failed\n", fname);
100 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
107 int nb_SetLocker(char *Locker)
111 if (strlen(Locker) == 0)
112 strcpy(AfsLocker, OriginalAfsLocker);
114 strcpy(AfsLocker, Locker);
115 EndFirstTimer(CMD_SETLOCKER, 1);
119 int nb_Xrmdir(char *Directory, char *type)
124 char NewDirectory[256];
127 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
128 if (strlen(Directory) == 0)
130 return(LeaveThread(1, "rmdir failed no path found\n", CMD_XRMDIR));
132 strcpy(NewDirectory, Directory);
133 memset(command, '\0', sizeof(command));
134 strcpy(command,"rmdir /Q ");
135 if (!stricmp(type, "all"))
137 strcat(command, "/S ");
139 strcat(command, NewDirectory);
142 rc = system(command);
144 if ((rc) && (rc != 2) && (rc != 3))
146 EndFirstTimer(CMD_XRMDIR, 0);
147 sprintf(temp, "rmdir failed on %s\n", command);
148 LeaveThread(rc, temp, CMD_XRMDIR);
149 sprintf(temp, "FAILURE: Thread %d - rmdir failed on\n %s\n", ProcessNumber, command);
150 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
153 EndFirstTimer(CMD_XRMDIR, 1);
158 int nb_Mkdir(char *Directory)
163 char NewDirectory[256];
166 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
167 if (strlen(Directory) == 0)
169 return(LeaveThread(1, "mkdir failed on no path found\n", CMD_MKDIR));
171 strcpy(NewDirectory, Directory);
172 memset(command, '\0', sizeof(command));
173 strcpy(command,"mkdir ");
174 strcat(command, NewDirectory);
177 rc = system(command);
181 EndFirstTimer(CMD_MKDIR, 0);
182 sprintf(temp, "mkdir failed on %s\n", command);
183 LeaveThread(rc, temp, CMD_MKDIR);
184 sprintf(temp, "ERROR: Thread %d - mkdir failed on\n %s\n", ProcessNumber, command);
185 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
188 EndFirstTimer(CMD_MKDIR, 1);
192 int nb_Attach(char *Locker, char *Drive)
199 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
200 if (strlen(Locker) == 0)
202 return(LeaveThread(1, "attach failed no locker found\n", CMD_ATTACH));
204 memset(command, '\0', sizeof(command));
205 strcpy(command,"attach -q ");
207 if (strlen(Drive) != 0)
209 sprintf(temp, "-D %s ", Drive);
210 strcat(command, temp);
212 strcat(command, Locker);
215 rc = system(command);
219 EndFirstTimer(CMD_ATTACH, 0);
220 sprintf(pExitStatus->Reason, "attach failed on %s\n", command);
221 pExitStatus->ExitStatus = rc;
222 sprintf(temp, "ERROR: Thread %d - attach failed on\n %s\n", ProcessNumber, command);
223 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
225 EndFirstTimer(CMD_ATTACH, 1);
229 int nb_Detach(char *Name, char *type)
236 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
237 memset(command, '\0', sizeof(command));
238 strcpy(command,"detach -q ");
240 if (!stricmp(type, "drive"))
242 sprintf(temp, "-D %s ", Name);
243 strcat(command, temp);
245 else if (!stricmp(type, "locker"))
247 strcat(command, Name);
251 return(LeaveThread(1, "nb_Detach failed unknown type: %s\n", CMD_DETACH));
255 rc = system(command);
259 EndFirstTimer(CMD_DETACH, 0);
260 sprintf(temp, "detach failed on %s\n", command);
261 LeaveThread(rc, temp, CMD_DETACH);
262 sprintf(temp, "ERROR: Thread %d - detach failed on\n %s\n", ProcessNumber, command);
263 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
266 EndFirstTimer(CMD_DETACH, 1);
270 int nb_CreateFile(char *path, DWORD size)
282 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
283 if (strlen(path) == 0)
285 return(LeaveThread(1, "nb_DeleteFile failed no path found\n", CMD_CREATEFILE));
288 strcpy(NewPath, path);
291 fHandle = CreateFile(NewPath,
292 GENERIC_READ | GENERIC_WRITE | STANDARD_RIGHTS_ALL,
293 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
296 FILE_ATTRIBUTE_NORMAL,
299 if (fHandle == INVALID_HANDLE_VALUE)
301 EndFirstTimer(CMD_CREATEFILE, 0);
302 sprintf(temp, "Create file failed on %s\n", NewPath);
303 LeaveThread(0, "", CMD_CREATEFILE);
304 sprintf(temp, "ERROR: Thread %d - Create file failed on\n %s\n", ProcessNumber, NewPath);
305 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
308 EndFirstTimer(CMD_CREATEFILE, 1);
309 Moved = SetFilePointer(fHandle,
313 memset(Buffer, 'A', sizeof(Buffer));
314 BytesToWrite = sizeof(Buffer);
315 rc = WriteFile(fHandle, Buffer, BytesToWrite, &BytesWritten, NULL);
317 FlushFileBuffers(fHandle);
318 CloseHandle(fHandle);
323 int nb_CopyFile(char *Source, char *Destination)
330 char NewDestination[256];
332 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
333 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
335 return(LeaveThread(1, "nb_CopyFile failed to copy files: either source or destination path not found\n", CMD_COPYFILES));
337 strcpy(NewSource, Source);
338 strcpy(NewDestination, Destination);
340 memset(command, '\0', sizeof(command));
341 sprintf(command, "copy /V /Y /B %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
344 rc = system(command);
348 EndFirstTimer(CMD_COPYFILES, 0);
349 sprintf(temp, "copy failed on %s\n", command);
350 LeaveThread(rc, temp, CMD_COPYFILES);
351 sprintf(temp, "FAILURE: Thread %d - copy failed on\n %s\n", ProcessNumber, command);
352 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
357 EndFirstTimer(CMD_COPYFILES, 1);
361 int nb_DeleteFile(char *path)
369 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
370 if (strlen(path) == 0)
372 return(LeaveThread(1, "nb_DeleteFile failed to delete files: no path found\n", CMD_DELETEFILES));
374 strcpy(NewPath, path);
376 memset(command, '\0', sizeof(command));
377 sprintf(command, "del /Q %s", NewPath);
380 rc = system(command);
384 EndFirstTimer(CMD_DELETEFILES, 0);
385 sprintf(temp, "del failed on %s\n", NewPath);
386 LeaveThread(rc, temp, CMD_DELETEFILES);
387 sprintf(temp, "ERROR: Thread %d - del failed on\n %s\n", ProcessNumber, command);
388 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
391 EndFirstTimer(CMD_DELETEFILES, 1);
395 int nb_xcopy(char *Source, char *Destination)
402 char NewDestination[256];
404 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
405 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
407 return(LeaveThread(1, "nb_xcopy failed to xcopy: either source or destination is missing\n", CMD_XCOPY));
409 strcpy(NewSource, Source);
410 strcpy(NewDestination, Destination);
411 memset(command, '\0', sizeof(command));
412 sprintf(command, "xcopy /E /I /V /Y /Q %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
415 rc = SystemCall(command);
419 EndFirstTimer(CMD_XCOPY, 0);
420 // sprintf(temp, "xcopy failed on %s\n", command);
421 // LeaveThread(rc, temp, CMD_XCOPY);
422 LeaveThread(0, "", CMD_XCOPY);
423 sprintf(temp, "FAIURE: Thread %d - xcopy failed on\n %s\n", ProcessNumber, command);
424 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
427 EndFirstTimer(CMD_XCOPY, 1);
431 int nb_Move(char *Source, char *Destination)
438 char NewDestination[256];
440 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
441 if ((strlen(Source) == 0) || (strlen(Destination) == 0))
443 return(LeaveThread(1, "nb_Move failed to move: either source or destination is missing\n", CMD_MOVE));
445 strcpy(NewSource, Source);
446 strcpy(NewDestination, Destination);
447 memset(command, '\0', sizeof(command));
448 sprintf(command, "move /Y %s %s > .\\test\\%s%d", NewSource, NewDestination, HostName, ProcessNumber);
450 rc = system(command);
454 EndFirstTimer(CMD_MOVE, 0);
455 sprintf(temp, "move failed on %s\n", command);
456 LeaveThread(rc, temp, CMD_MOVE);
457 sprintf(temp, "FAILURE: Thread %d - move failed on\n %s\n", ProcessNumber, command);
458 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
461 EndFirstTimer(CMD_MOVE, 1);
465 int nb_createx(char *fname, unsigned create_options, unsigned create_disposition, int handle)
469 uint32 desired_access;
474 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
476 strcpy(path, AfsLocker);
478 if (create_options & FILE_DIRECTORY_FILE)
480 desired_access = FILE_READ_DATA;
484 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
488 fd = CreateObject(path,
491 FILE_SHARE_READ|FILE_SHARE_WRITE,
495 if (fd == -1 && handle != -1)
497 if (create_options & FILE_DIRECTORY_FILE)
502 if ((rc != ERROR_FILE_NOT_FOUND) && (rc != ERROR_PATH_NOT_FOUND))
503 if (rc != ERROR_ALREADY_EXISTS)
505 EndFirstTimer(CMD_NTCREATEX, 0);
507 LeaveThread(0, "", CMD_NTCREATEX);
508 sprintf(temp, "Directory: unable to create directory %s\n", path);
511 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
518 EndFirstTimer(CMD_NTCREATEX, 0);
519 LeaveThread(0, "", CMD_NTCREATEX);
520 sprintf(temp, "File: unable to create file %s\n", path);
523 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
528 EndFirstTimer(CMD_NTCREATEX, 1);
530 if (create_options & FILE_DIRECTORY_FILE)
533 if (fd != -1 && handle == -1)
536 CloseHandle((HANDLE)fd);
541 if (fd == -1 && handle == -1)
544 for (i = 0; i < MAX_FILES; i++)
546 if (ftable[i].handle == 0)
551 printf("(%d) file table full for %s\n", LineCount, path);
552 return(LeaveThread(1, "file table is full\n", CMD_NTCREATEX));
554 ftable[i].handle = handle;
557 free(ftable[i].name);
558 ftable[i].name = strdup(path);
559 ftable[i].reads = ftable[i].writes = 0;
563 int nb_writex(int handle, int offset, int size, int ret_size)
570 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
572 if (IoBuffer[0] == 0)
573 memset(IoBuffer, 1, sizeof(IoBuffer));
575 if ((i = FindHandle(handle)) == -1)
578 status = nb_write(ftable[i].fd, IoBuffer, offset, size);
580 if (status != ret_size)
582 EndFirstTimer(CMD_WRITEX, 0);
583 LeaveThread(0, "", CMD_WRITEX);
585 sprintf(temp, "File: %s. wrote %d bytes, got %d bytes\n", ftable[i].name, size, status);
587 sprintf(temp, "File: %s. On write, cannot set file pointer\n", ftable[i].name);
591 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
594 EndFirstTimer(CMD_WRITEX, 1);
600 int nb_lock(int handle, int offset, int size, int timeout, unsigned char locktype, NTSTATUS exp)
608 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
610 if ((i = FindHandle(handle)) == -1)
612 LeaveThread(0, "", CMD_LOCKINGX);
613 sprintf(temp, "File unlock: Cannot find handle for %s", ftable[i].name);
616 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
621 ret = cli_locktype(c, i, offset, size, timeout, locktype);
625 EndFirstTimer(CMD_LOCKINGX, 0);
626 LeaveThread(0, "", CMD_LOCKINGX);
627 sprintf(temp, "(%d) ERROR: lock failed on handle %d ofs=%d size=%d timeout= %d exp=%d fd %d errno %d (%s)\n",
628 LineCount, handle, offset, size, timeout, exp, ftable[i].fd, exp, "");
629 sprintf(temp, "File unlock: lock failed %s", ftable[i].name);
632 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
635 EndFirstTimer(CMD_LOCKINGX, 1);
640 int nb_readx(int handle, int offset, int size, int ret_size)
647 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
649 if ((i = FindHandle(handle)) == -1)
653 ret = nb_read(ftable[i].fd, IoBuffer, offset, size);
655 if ((ret != size) && (ret != ret_size))
657 EndFirstTimer(CMD_READX, 0);
658 LeaveThread(0, "", CMD_READX);
660 sprintf(temp, "File: read failed on index=%d, offset=%d ReadSize=%d ActualRead=%d handle=%d\n",
661 handle, offset, size, ret, ftable[i].fd);
663 sprintf(temp, "File: %s. On read, cannot set file pointer\n", ftable[i].name);
667 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
670 EndFirstTimer(CMD_READX, 1);
675 int nb_close(int handle)
682 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
684 if ((i = FindHandle(handle)) == -1)
688 ret = nb_close1(ftable[i].fd);
689 EndFirstTimer(CMD_CLOSE, ret);
692 LeaveThread(0, "", CMD_CLOSE);
693 sprintf(temp, "(%d) close failed on handle %d\n", LineCount, handle);
696 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
700 ftable[i].handle = 0;
703 free(ftable[i].name);
704 ftable[i].name = NULL;
708 int nb_rmdir(char *fname)
715 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
717 strcpy(path, AfsLocker);
721 rc = RemoveDirectory(path);
722 EndFirstTimer(CMD_RMDIR, rc);
726 LeaveThread(0, "", CMD_RMDIR);
727 sprintf(temp, "Directory: RemoveDirectory %s failed\n", fname);
730 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
736 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 EndFirstTimer(CMD_RENAME, rc);
757 LeaveThread(0, "", CMD_RENAME);
758 sprintf(temp, "File: rename %s %s failed\n", old, New);
761 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
768 int nb_qpathinfo(char *fname, int Type)
779 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
781 strcpy(path, AfsLocker);
785 rc = GetPathInfo(path, NULL, NULL, NULL, NULL, NULL);
787 if (strstr(fname, "~TS"))
806 EndFirstTimer(CMD_QUERY_PATH_INFO, 0);
807 LeaveThread(0, "", CMD_QUERY_PATH_INFO);
808 sprintf(temp, "File: qpathinfo failed for %s\n", path);
811 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
815 EndFirstTimer(CMD_QUERY_PATH_INFO, 1);
819 int nb_qfileinfo(int fnum)
826 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
828 if ((i = FindHandle(fnum)) == -1)
832 rc = GetFileInfo(ftable[i].name, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL);
836 EndFirstTimer(CMD_QUERY_FILE_INFO, 0);
837 LeaveThread(0, "", CMD_QUERY_FILE_INFO);
838 sprintf(temp, "File: qfileinfo failed for %s\n", ftable[i].name);
841 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
844 EndFirstTimer(CMD_QUERY_FILE_INFO, 1);
849 int nb_qfsinfo(int level)
859 ULARGE_INTEGER FreeBytesAvailable;
860 ULARGE_INTEGER TotalNumberOfBytes;
861 ULARGE_INTEGER TotalNumberOfFreeBytes;
863 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
864 sprintf(Path, "%s\\%s%05d", AfsLocker, HostName, LogID);
867 rc = GetDiskFreeSpaceEx(Path, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
868 // rc = cli_dskattr(c, &bsize, &total, &avail);
872 EndFirstTimer(CMD_QUERY_FS_INFO, 0);
873 LeaveThread(0, "", CMD_QUERY_FS_INFO);
874 strcpy(temp, "File: Disk free space failed\n");
877 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
880 EndFirstTimer(CMD_QUERY_FS_INFO, 1);
885 void find_fn(file_info *finfo, char *name, void *state)
890 int nb_findfirst(char *mask)
897 if (strstr(mask, "<.JNK"))
900 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
902 strcpy(NewMask, AfsLocker);
903 strcat(NewMask, mask);
906 rc = GetFileList(NewMask, (void *)find_fn, NULL);
910 EndFirstTimer(CMD_FIND_FIRST, 0);
911 sprintf(temp, "File: findfirst cannot find for %s\n", mask);
914 LeaveThread(1, temp, CMD_FIND_FIRST);
915 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
918 EndFirstTimer(CMD_FIND_FIRST, 1);
922 int nb_flush(int fnum)
926 if ((i = FindHandle(fnum)) == -1)
929 /* hmmm, we don't have cli_flush() yet */
932 static int total_deleted;
934 void delete_fn(file_info *finfo, const char *name, void *state)
941 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
943 if (finfo->mode & aDIR)
946 sprintf(s2, "%s\\*", name);
947 GetFileList(s2, delete_fn, NULL);
948 sprintf(s, "%s", &name[strlen(AfsLocker)]);
953 rc = DeleteFile(name);
956 LeaveThread(0, "", CMD_UNLINK);
957 sprintf(temp, "FILE: DeleteFile %s failed\n", name);
960 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
967 int nb_deltree(char *dname)
974 strcpy(path, AfsLocker);
976 sprintf(mask, "%s\\*", path);
981 GetFileList(mask, delete_fn, NULL);
983 // pstrcpy(path, AfsLocker);
984 // pstrcat(path, dname);
985 rc = RemoveDirectory(path);
986 EndFirstTimer(CMD_DELTREE, rc);
994 if ((rc != ERROR_FILE_NOT_FOUND) && (rc != ERROR_PATH_NOT_FOUND))
997 LeaveThread(0, "", CMD_DELTREE);
998 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
999 sprintf(temp, "ERROR: Thread %d - Unable to remove %s.\n", ProcessNumber, path);
1000 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1010 int nb_cleanup(char *cname)
1014 strcpy(temp, "\\clients\\client1");
1015 SubstituteString(temp, "client1", cname, sizeof(temp));
1016 SubstituteString(temp, "clients", HostName, sizeof(temp));
1021 int LeaveThread(int status, char *Reason, int cmd)
1028 ++ThreadCommandInfo[cmd].ErrorCount;
1030 if (strlen(Reason) == 0)
1033 rc = GetLastError();
1036 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1039 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1040 pExitStatus->Reason,
1041 sizeof(pExitStatus->Reason),
1043 LastKnownError = rc;
1046 strcpy(pExitStatus->Reason, Reason);
1048 if (strlen(pExitStatus->Reason) == 0)
1049 strcpy(pExitStatus->Reason, "\n");
1050 if (pExitStatus->Reason[strlen(pExitStatus->Reason) - 1] != '\n')
1051 strcat(pExitStatus->Reason, "\n");
1052 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1053 if (strlen(Reason) == 0)
1054 sprintf(temp, "ERROR(%d): Thread %d - (%d) %s", LineCount, ProcessNumber, rc, pExitStatus->Reason);
1056 sprintf(temp, "ERROR(%d): Thread %d - %s", LineCount, ProcessNumber, pExitStatus->Reason);
1057 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1060 pExitStatus->ExitStatus = status;
1062 DumpAFSLog(HostName, LogID);
1063 (*pThreadStatus) = 0;
1067 void StartFirstTimer(void)
1070 EndSecondTime(CMD_NONAFS);
1071 TickCount1 = GetTickCount();
1074 void EndFirstTimer(int cmd, int Type)
1079 ThreadCommandInfo[cmd].count++;
1081 MilliTick = GetTickCount() - TickCount1;
1084 StartSecondTime(CMD_NONAFS);
1088 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1089 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1091 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1097 ThreadCommandInfo[cmd].min_sec = cmd_time;
1098 StartSecondTime(CMD_NONAFS);
1103 ThreadCommandInfo[cmd].ErrorTime += cmd_time;
1106 ThreadCommandInfo[cmd].total_sec += cmd_time;
1107 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1108 ThreadCommandInfo[cmd].min_sec = cmd_time;
1109 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1110 ThreadCommandInfo[cmd].max_sec = cmd_time;
1113 StartSecondTime(CMD_NONAFS);
1116 void StartSecondTime(int cmd)
1119 TickCount2 = GetTickCount();
1123 void EndSecondTime(int cmd)
1128 ThreadCommandInfo[cmd].count++;
1130 MilliTick = GetTickCount() - TickCount2;
1135 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1136 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1138 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1143 ThreadCommandInfo[cmd].min_sec = cmd_time;
1146 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1147 ThreadCommandInfo[cmd].min_sec = cmd_time;
1148 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1149 ThreadCommandInfo[cmd].max_sec = cmd_time;
1150 ThreadCommandInfo[cmd].total_sec += cmd_time;
1154 int SystemCall(char *command)
1159 argv[0] = getenv("COMSPEC");
1162 argv[3] = (char *)command;
1165 rc = spawnve(_P_WAIT,argv[0],argv,NULL);
1166 // != -1 || (errno != ENOENT && errno != EACCES))
1170 int CreateObject(const char *fname, uint32 DesiredAccess,
1171 uint32 FileAttributes, uint32 ShareAccess,
1172 uint32 CreateDisposition, uint32 CreateOptions)
1175 DWORD dwCreateDisposition = 0;
1176 DWORD dwDesiredAccess = 0;
1177 DWORD dwShareAccess = 0;
1179 if (CreateOptions & FILE_DIRECTORY_FILE)
1181 fd = CreateDirectory(fname, NULL);
1187 dwDesiredAccess = 0;
1188 if (DesiredAccess & FILE_READ_DATA)
1189 dwDesiredAccess |= GENERIC_READ;
1190 if (DesiredAccess & FILE_WRITE_DATA)
1191 dwDesiredAccess |= GENERIC_WRITE;
1192 dwShareAccess = ShareAccess;
1193 dwShareAccess |= FILE_SHARE_DELETE;
1194 dwCreateDisposition = OPEN_ALWAYS;
1195 if (CreateDisposition == 1)
1196 dwCreateDisposition = OPEN_EXISTING;
1197 fd = (int)CreateFile(fname, dwDesiredAccess, ShareAccess, NULL, dwCreateDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1203 BOOL nb_close1(int fnum)
1210 if (rc = GetHandleInformation((HANDLE)fnum, &dwFlags))
1211 CloseHandle((HANDLE)fnum);
1216 /****************************************************************************
1217 do a directory listing, calling fn on each file found
1218 this uses the old SMBsearch interface. It is needed for testing Samba,
1219 but should otherwise not be used
1220 ****************************************************************************/
1221 int nb_list_old(const char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1223 int num_received = 0;
1226 char cFileName[1024];
1227 int dwFileAttributes;
1234 if (!strcmp(&mask[strlen(mask)-2], "\"*"))
1236 strcpy(&mask[strlen(mask)-2], "*");
1239 dwFileAttributes = 0;
1240 memset(cFileName, '\0', sizeof(cFileName));
1241 hFind = WinFindFirstFile(mask, &FileData, cFileName, &dwFileAttributes);
1242 if (hFind == INVALID_HANDLE_VALUE)
1246 mask[strlen(mask) - 1] = '\0';
1249 if (cFileName[0] != '.')
1253 memset(&finfo, '\0', sizeof(finfo));
1254 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1256 strcpy(finfo.name, cFileName);
1257 sprintf(temp, "%s%s", mask, cFileName);
1258 fn(&finfo, temp, state);
1261 memset(cFileName, '\0', sizeof(cFileName));
1262 dwFileAttributes = 0;
1263 if (!WinFindNextFile(hFind, &FileData, cFileName, &dwFileAttributes))
1267 return(num_received);
1271 /****************************************************************************
1272 do a directory listing, calling fn on each file found
1273 this auto-switches between old and new style
1274 ****************************************************************************/
1275 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1277 return(nb_list_old(Mask, fn, state));
1280 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes)
1283 static WIN32_FIND_DATAW FileDataW;
1284 static WIN32_FIND_DATAA FileDataA;
1286 memset(&FileDataA, '\0', sizeof(FileDataA));
1287 rc = FindFirstFile(Mask, &FileDataA);
1288 if (rc != INVALID_HANDLE_VALUE)
1290 (*FileData) = (void *)&FileDataA;
1291 (*dwFileAttributes) = FileDataA.dwFileAttributes;
1292 strcpy(cFileName, FileDataA.cFileName);
1298 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes)
1301 WIN32_FIND_DATAA *FileDataA;
1303 FileDataA = (WIN32_FIND_DATAA *)(*FileData);
1304 if (!(rc = FindNextFile(hFind, FileDataA)))
1308 (*dwFileAttributes) = FileDataA->dwFileAttributes;
1309 strcpy(cFileName, FileDataA->cFileName);
1314 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len)
1319 if (!insert || !pattern || !s) return;
1321 ls = (ssize_t)strlen(s);
1322 lp = (ssize_t)strlen(pattern);
1323 li = (ssize_t)strlen(insert);
1325 if (!*pattern) return;
1327 while (lp <= ls && (p = strstr(s,pattern)))
1329 if (len && (ls + (li-lp) >= (int)len))
1335 memmove(p+li,p+lp,strlen(p+lp)+1);
1337 memcpy(p, insert, li);
1343 #define CHANGE_TIME(A,B) \
1344 B.tm_hour = A.wHour; \
1345 B.tm_sec = A.wSecond; \
1346 B.tm_min = A.wMinute; \
1347 B.tm_mon = A.wMonth - 1; \
1349 B.tm_year = A.wYear - 1900; \
1350 B.tm_wday = A.wDayOfWeek - 1; \
1355 BOOL GetPathInfo(const char *fname,
1356 time_t *c_time, time_t *a_time, time_t *m_time,
1357 size_t *size, uint16 *mode)
1359 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1361 SYSTEMTIME SystemTime;
1364 // rc = WinGetFileAttributesEx(UseUnicode, (char *)fname, &FileInfo);
1365 rc = GetFileAttributesEx(fname, GetFileExInfoStandard, &FileInfo);
1370 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1371 CHANGE_TIME(SystemTime, tm_time)
1372 (*c_time) = mktime(&tm_time);
1376 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1377 CHANGE_TIME(SystemTime, tm_time)
1378 (*a_time) = mktime(&tm_time);
1382 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1383 CHANGE_TIME(SystemTime, tm_time)
1384 (*m_time) = mktime(&tm_time);
1389 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1390 *size = FileInfo.nFileSizeLow;
1401 /****************************************************************************
1402 send a qfileinfo call
1403 ****************************************************************************/
1404 BOOL GetFileInfo(char *FileName, int fnum,
1405 uint16 *mode, size_t *size,
1406 time_t *c_time, time_t *a_time, time_t *m_time,
1409 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1411 SYSTEMTIME SystemTime;
1414 rc = GetFileAttributesEx(FileName, GetFileExInfoStandard, &FileInfo);
1419 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1420 CHANGE_TIME(SystemTime, tm_time)
1421 (*c_time) = mktime(&tm_time);
1425 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1426 CHANGE_TIME(SystemTime, tm_time)
1427 (*a_time) = mktime(&tm_time);
1431 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1432 CHANGE_TIME(SystemTime, tm_time)
1433 (*m_time) = mktime(&tm_time);
1438 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1439 *size = FileInfo.nFileSizeLow;
1450 /****************************************************************************
1451 Read size bytes at offset offset using SMBreadX.
1452 ****************************************************************************/
1454 ssize_t nb_read(int fnum, char *IoBuffer, off_t offset, size_t size)
1463 LowDword = SetFilePointer((HANDLE)fnum, offset, 0, FILE_BEGIN);
1465 if (LowDword == INVALID_SET_FILE_POINTER)
1467 rc = ReadFile((HANDLE)fnum, IoBuffer, size, &total, NULL);
1474 /****************************************************************************
1476 ****************************************************************************/
1478 ssize_t nb_write(int fnum, char *IoBuffer, off_t offset, size_t size)
1484 LowDword = SetFilePointer((HANDLE)fnum, offset, 0, FILE_BEGIN);
1485 if (LowDword == INVALID_SET_FILE_POINTER)
1487 rc = WriteFile((HANDLE)fnum, IoBuffer, size, &bwritten, NULL);
1491 FlushFileBuffers((HANDLE)fnum);