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(gle, "", 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(gle, 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;
497 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
499 strcpy(path, AfsLocker);
501 if (create_options & FILE_DIRECTORY_FILE)
503 desired_access = FILE_READ_DATA;
507 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
511 fd = CreateObject(path,
514 FILE_SHARE_READ|FILE_SHARE_WRITE,
518 if (fd == INVALID_HANDLE_VALUE && handle != -1)
520 gle = GetLastError();
521 if (create_options & FILE_DIRECTORY_FILE)
523 EndFirstTimer(CMD_NTCREATEX, 0);
524 LeaveThread(gle, "", CMD_NTCREATEX);
525 sprintf(temp, "Directory: unable to create directory %s\n", path);
528 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
533 EndFirstTimer(CMD_NTCREATEX, 0);
534 LeaveThread(gle, "", CMD_NTCREATEX);
535 sprintf(temp, "File: unable to create file %s\n", path);
538 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
543 EndFirstTimer(CMD_NTCREATEX, 1);
545 if (fd != INVALID_HANDLE_VALUE && handle == -1)
552 if (fd == INVALID_HANDLE_VALUE && handle == -1)
555 for (i = 0; i < MAX_FILES; i++)
557 if (ftable[i].handle == 0)
562 printf("(%d) file table full for %s\n", LineCount, path);
563 return(LeaveThread(1, "file table is full\n", CMD_NTCREATEX));
565 ftable[i].handle = handle;
568 free(ftable[i].name);
569 ftable[i].name = strdup(path);
570 ftable[i].reads = ftable[i].writes = 0;
574 int nb_writex(int handle, int offset, int size, int ret_size)
580 unsigned char magic = (unsigned char)getpid();
583 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
585 if (IoBuffer[0] != magic ||
586 IoBuffer[1] != magic ||
587 IoBuffer[2] != magic ||
588 IoBuffer[3] != magic)
589 memset(IoBuffer, magic, BufferSize);
591 if ((i = FindHandle(handle)) == -1)
594 status = nb_write(ftable[i].fd, IoBuffer, offset, size);
595 if (status != ret_size)
597 gle = GetLastError();
598 EndFirstTimer(CMD_WRITEX, 0);
599 LeaveThread(gle, "", CMD_WRITEX);
601 sprintf(temp, "File: %s. wrote %d bytes, got %d bytes\n", ftable[i].name, size, status);
603 sprintf(temp, "File: %s. On write, cannot set file pointer\n", ftable[i].name);
609 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
612 EndFirstTimer(CMD_WRITEX, 1);
618 int nb_lock(int handle, int offset, int size, int timeout, unsigned char locktype, NTSTATUS exp)
626 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
628 if ((i = FindHandle(handle)) == -1)
630 LeaveThread(0, "", CMD_LOCKINGX);
631 sprintf(temp, "File unlock: Cannot find handle for %s", ftable[i].name);
634 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
639 ret = cli_locktype(c, i, offset, size, timeout, locktype);
643 EndFirstTimer(CMD_LOCKINGX, 0);
644 LeaveThread(0, "", CMD_LOCKINGX);
645 sprintf(temp, "(%d) ERROR: lock failed on handle %d ofs=%d size=%d timeout= %d exp=%d fd %d errno %d (%s)\n",
646 LineCount, handle, offset, size, timeout, exp, ftable[i].fd, exp, "");
647 sprintf(temp, "File unlock: lock failed %s", ftable[i].name);
650 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
653 EndFirstTimer(CMD_LOCKINGX, 1);
658 int nb_readx(int handle, int offset, int size, int ret_size)
666 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
668 if ((i = FindHandle(handle)) == -1)
672 ret = nb_read(ftable[i].fd, IoBuffer, offset, size);
673 gle = GetLastError();
674 if ((ret != size) && (ret != ret_size))
676 EndFirstTimer(CMD_READX, 0);
677 LeaveThread(gle, "", CMD_READX);
679 sprintf(temp, "File: read failed on index=%d, offset=%d ReadSize=%d ActualRead=%d handle=%p GLE(0x%x)\n",
680 handle, offset, size, ret, ftable[i].fd, gle);
682 sprintf(temp, "File: %s. On read, cannot set file pointer GLE(0x%x)\n", ftable[i].name, gle);
686 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
689 EndFirstTimer(CMD_READX, 1);
694 int nb_close(int handle)
702 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
704 if ((i = FindHandle(handle)) == -1)
708 ret = nb_close1(ftable[i].fd);
709 gle = GetLastError();
710 EndFirstTimer(CMD_CLOSE, ret);
713 LeaveThread(gle, "", CMD_CLOSE);
714 sprintf(temp, "(%d) close failed on handle %d\n", LineCount, handle);
717 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
721 ftable[i].handle = 0;
724 free(ftable[i].name);
725 ftable[i].name = NULL;
729 int nb_rmdir(char *fname)
737 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
739 strcpy(path, AfsLocker);
743 rc = RemoveDirectory(path);
744 gle = GetLastError();
745 EndFirstTimer(CMD_RMDIR, rc);
749 LeaveThread(gle, "", CMD_RMDIR);
750 sprintf(temp, "Directory: RemoveDirectory %s failed GLE(0x%x)\n", fname, gle);
753 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
759 int nb_rename(char *old, char *New)
768 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
770 strcpy(opath, AfsLocker);
772 strcpy(npath, AfsLocker);
776 rc = MoveFileEx(opath, npath, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
777 gle = GetLastError();
778 EndFirstTimer(CMD_RENAME, rc);
782 LeaveThread(gle, "", CMD_RENAME);
783 sprintf(temp, "File: rename %s %s failed GLE(0x%x)\n", old, New, gle);
786 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
793 * Type is used to determine whether the file is expected
794 * to exist or not. It is overloaded (temporarily) to control
795 * Flag which indicates whether an error is treated as an error
796 * or not. The StreamFiles.txt script does not have the Type
797 * parameter set correctly for all 120,000+ lines. As a result
798 * it is not possible to enforce the presence test throughout
801 int nb_qpathinfo(char *fname, int Type)
810 EnforcePathInfoErrors = 1;
812 } else if (Type == 1001) {
813 EnforcePathInfoErrors = 0;
815 } else if (Type == 1000) {
816 EnforcePathInfoErrors = 0;
818 } else if (Type == 1110) {
819 EnforcePathInfoErrors = 1;
823 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
825 strcpy(path, AfsLocker);
829 rc = GetPathInfo(path, NULL, NULL, NULL, NULL, NULL);
831 gle = GetLastError();
832 if (strstr(fname, "~TS"))
840 if (!EnforcePathInfoErrors)
851 EndFirstTimer(CMD_QUERY_PATH_INFO, 0);
852 LeaveThread(gle, "", CMD_QUERY_PATH_INFO);
853 sprintf(temp, "File: qpathinfo failed for %s type %d GLE(0x%x)\n", path, Type, gle);
856 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
860 EndFirstTimer(CMD_QUERY_PATH_INFO, 1);
864 int nb_qfileinfo(int handle)
872 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
874 if ((i = FindHandle(handle)) == -1)
878 rc = GetFileInfo(ftable[i].name, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL);
879 gle = GetLastError();
882 EndFirstTimer(CMD_QUERY_FILE_INFO, 0);
883 LeaveThread(gle, "", CMD_QUERY_FILE_INFO);
884 sprintf(temp, "File: qfileinfo failed for %s GLE(0x%x)\n", ftable[i].name, gle);
887 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
890 EndFirstTimer(CMD_QUERY_FILE_INFO, 1);
895 int nb_qfsinfo(int level)
905 ULARGE_INTEGER FreeBytesAvailable;
906 ULARGE_INTEGER TotalNumberOfBytes;
907 ULARGE_INTEGER TotalNumberOfFreeBytes;
910 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
911 sprintf(Path, "%s\\%s%05d", AfsLocker, HostName, LogID);
914 rc = GetDiskFreeSpaceEx(Path, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
915 // rc = cli_dskattr(c, &bsize, &total, &avail);
916 gle = GetLastError();
920 EndFirstTimer(CMD_QUERY_FS_INFO, 0);
921 LeaveThread(gle, "", CMD_QUERY_FS_INFO);
922 sprintf(temp, "File: Disk free space failed GLE(0x%x)\n", gle);
925 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
928 EndFirstTimer(CMD_QUERY_FS_INFO, 1);
933 void find_fn(file_info *finfo, char *name, void *state)
938 int nb_findfirst(char *mask)
945 if (strstr(mask, "<.JNK"))
948 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
950 strcpy(NewMask, AfsLocker);
951 strcat(NewMask, mask);
954 rc = GetFileList(NewMask, (void *)find_fn, NULL);
958 EndFirstTimer(CMD_FIND_FIRST, 0);
959 sprintf(temp, "File: findfirst cannot find for %s\n", mask);
962 LeaveThread(1, temp, CMD_FIND_FIRST);
963 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
966 EndFirstTimer(CMD_FIND_FIRST, 1);
970 int nb_flush(int handle)
974 if ((i = FindHandle(handle)) == -1)
977 FlushFileBuffers(ftable[i].fd);
981 static int total_deleted;
983 void delete_fn(file_info *finfo, const char *name, void *state)
991 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
993 if (finfo->mode & aDIR)
996 sprintf(s2, "%s\\*", name);
997 GetFileList(s2, delete_fn, NULL);
998 sprintf(s, "%s", &name[strlen(AfsLocker)]);
1003 rc = DeleteFile(name);
1004 gle = GetLastError();
1007 LeaveThread(gle, "", CMD_UNLINK);
1008 sprintf(temp, "FILE: DeleteFile %s failed GLE(0x%x)\n", name, gle);
1011 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1018 int nb_deltree(char *dname)
1025 strcpy(path, AfsLocker);
1026 strcat(path, dname);
1027 sprintf(mask, "%s\\*", path);
1032 GetFileList(mask, delete_fn, NULL);
1034 // pstrcpy(path, AfsLocker);
1035 // pstrcat(path, dname);
1036 rc = RemoveDirectory(path);
1037 gle = GetLastError();
1038 EndFirstTimer(CMD_DELTREE, rc);
1044 if ((gle != ERROR_FILE_NOT_FOUND) && (gle != ERROR_PATH_NOT_FOUND))
1046 LeaveThread(gle, "", CMD_DELTREE);
1047 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1048 sprintf(temp, "ERROR: Thread %d - Unable to remove %s.\n", ProcessNumber, path);
1049 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1060 int nb_cleanup(char *cname)
1064 strcpy(temp, "\\clients\\client1");
1065 SubstituteString(temp, "client1", cname, sizeof(temp));
1066 SubstituteString(temp, "clients", HostName, sizeof(temp));
1071 int LeaveThread(int status, char *Reason, int cmd)
1078 ++ThreadCommandInfo[cmd].ErrorCount;
1080 if (strlen(Reason) == 0)
1083 rc = GetLastError();
1086 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
1089 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1090 pExitStatus->Reason,
1091 sizeof(pExitStatus->Reason),
1093 LastKnownError = rc;
1096 strcpy(pExitStatus->Reason, Reason);
1098 if (strlen(pExitStatus->Reason) == 0)
1099 strcpy(pExitStatus->Reason, "\n");
1100 if (pExitStatus->Reason[strlen(pExitStatus->Reason) - 1] != '\n')
1101 strcat(pExitStatus->Reason, "\n");
1102 sprintf(FileName, "Thread_%05d.log", ProcessNumber);
1103 if (strlen(Reason) == 0)
1104 sprintf(temp, "ERROR(%d): Thread %d - (%d) %s", LineCount, ProcessNumber, rc, pExitStatus->Reason);
1106 sprintf(temp, "ERROR(%d): Thread %d - %s", LineCount, ProcessNumber, pExitStatus->Reason);
1107 LogMessage(ProcessNumber, HostName, FileName, temp, LogID);
1110 pExitStatus->ExitStatus = status;
1112 DumpAFSLog(HostName, LogID);
1113 (*pThreadStatus) = 0;
1117 void StartFirstTimer(void)
1120 EndSecondTime(CMD_NONAFS);
1121 TickCount1 = GetTickCount();
1124 void EndFirstTimer(int cmd, int Type)
1129 ThreadCommandInfo[cmd].count++;
1131 MilliTick = GetTickCount() - TickCount1;
1134 StartSecondTime(CMD_NONAFS);
1138 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1139 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1141 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1147 ThreadCommandInfo[cmd].min_sec = cmd_time;
1148 StartSecondTime(CMD_NONAFS);
1153 ThreadCommandInfo[cmd].ErrorTime += cmd_time;
1156 ThreadCommandInfo[cmd].total_sec += cmd_time;
1157 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1158 ThreadCommandInfo[cmd].min_sec = cmd_time;
1159 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1160 ThreadCommandInfo[cmd].max_sec = cmd_time;
1163 StartSecondTime(CMD_NONAFS);
1166 void StartSecondTime(int cmd)
1169 TickCount2 = GetTickCount();
1173 void EndSecondTime(int cmd)
1178 ThreadCommandInfo[cmd].count++;
1180 MilliTick = GetTickCount() - TickCount2;
1185 ThreadCommandInfo[cmd].MilliSeconds += MilliTick;
1186 while (ThreadCommandInfo[cmd].MilliSeconds > 1000)
1188 ThreadCommandInfo[cmd].MilliSeconds -= 1000;
1193 ThreadCommandInfo[cmd].min_sec = cmd_time;
1196 if (cmd_time < (int)ThreadCommandInfo[cmd].min_sec)
1197 ThreadCommandInfo[cmd].min_sec = cmd_time;
1198 if ((int)ThreadCommandInfo[cmd].max_sec < cmd_time)
1199 ThreadCommandInfo[cmd].max_sec = cmd_time;
1200 ThreadCommandInfo[cmd].total_sec += cmd_time;
1204 intptr_t SystemCall(char *command)
1209 argv[0] = getenv("COMSPEC");
1212 argv[3] = (char *)command;
1215 rc = spawnve(_P_WAIT,argv[0],argv,NULL);
1216 // != -1 || (errno != ENOENT && errno != EACCES))
1220 HANDLE CreateObject(const char *fname, uint32 DesiredAccess,
1221 uint32 FileAttributes, uint32 ShareAccess,
1222 uint32 CreateDisposition, uint32 CreateOptions)
1225 DWORD dwCreateDisposition = 0;
1226 DWORD dwDesiredAccess = 0;
1227 DWORD dwShareAccess = 0;
1230 if (CreateOptions & FILE_DIRECTORY_FILE)
1234 if (!CreateDirectory(fname, NULL) && (rc = GetLastError()) != ERROR_ALREADY_EXISTS)
1237 fd = INVALID_HANDLE_VALUE;
1241 fd = CreateFile(fname, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1246 dwDesiredAccess = 0;
1247 if (DesiredAccess & FILE_READ_DATA)
1248 dwDesiredAccess |= GENERIC_READ;
1249 if (DesiredAccess & FILE_WRITE_DATA)
1250 dwDesiredAccess |= GENERIC_WRITE;
1251 dwShareAccess = ShareAccess;
1252 dwShareAccess |= FILE_SHARE_DELETE;
1253 dwCreateDisposition = OPEN_ALWAYS;
1254 if (CreateDisposition == 1)
1255 dwCreateDisposition = OPEN_EXISTING;
1256 fd = CreateFile(fname, dwDesiredAccess, ShareAccess, NULL, dwCreateDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
1262 BOOL nb_close1(HANDLE fd)
1267 if (fd != INVALID_HANDLE_VALUE)
1269 if (rc = GetHandleInformation(fd, &dwFlags))
1275 /****************************************************************************
1276 do a directory listing, calling fn on each file found
1277 this uses the old SMBsearch interface. It is needed for testing Samba,
1278 but should otherwise not be used
1279 ****************************************************************************/
1280 int nb_list_old(const char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1282 int num_received = 0;
1285 char cFileName[1024];
1286 int dwFileAttributes;
1293 if (!strcmp(&mask[strlen(mask)-2], "\"*"))
1295 strcpy(&mask[strlen(mask)-2], "*");
1298 dwFileAttributes = 0;
1299 memset(cFileName, '\0', sizeof(cFileName));
1300 hFind = WinFindFirstFile(mask, &FileData, cFileName, &dwFileAttributes);
1301 if (hFind == INVALID_HANDLE_VALUE)
1305 mask[strlen(mask) - 1] = '\0';
1308 if (cFileName[0] != '.')
1312 memset(&finfo, '\0', sizeof(finfo));
1313 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1315 strcpy(finfo.name, cFileName);
1316 sprintf(temp, "%s%s", mask, cFileName);
1317 fn(&finfo, temp, state);
1320 memset(cFileName, '\0', sizeof(cFileName));
1321 dwFileAttributes = 0;
1322 if (!WinFindNextFile(hFind, &FileData, cFileName, &dwFileAttributes))
1326 return(num_received);
1330 /****************************************************************************
1331 do a directory listing, calling fn on each file found
1332 this auto-switches between old and new style
1333 ****************************************************************************/
1334 int GetFileList(char *Mask, void (*fn)(file_info *, const char *, void *), void *state)
1336 return(nb_list_old(Mask, fn, state));
1339 HANDLE WinFindFirstFile(char *Mask, void **FileData, char *cFileName, int *dwFileAttributes)
1342 static WIN32_FIND_DATAW FileDataW;
1343 static WIN32_FIND_DATAA FileDataA;
1345 memset(&FileDataA, '\0', sizeof(FileDataA));
1346 rc = FindFirstFile(Mask, &FileDataA);
1347 if (rc != INVALID_HANDLE_VALUE)
1349 (*FileData) = (void *)&FileDataA;
1350 (*dwFileAttributes) = FileDataA.dwFileAttributes;
1351 strcpy(cFileName, FileDataA.cFileName);
1357 int WinFindNextFile(HANDLE hFind, void **FileData, char *cFileName, int *dwFileAttributes)
1360 WIN32_FIND_DATAA *FileDataA;
1362 FileDataA = (WIN32_FIND_DATAA *)(*FileData);
1363 if (!(rc = FindNextFile(hFind, FileDataA)))
1367 (*dwFileAttributes) = FileDataA->dwFileAttributes;
1368 strcpy(cFileName, FileDataA->cFileName);
1373 void SubstituteString(char *s,const char *pattern,const char *insert, size_t len)
1378 if (!insert || !pattern || !s) return;
1380 ls = (ssize_t)strlen(s);
1381 lp = (ssize_t)strlen(pattern);
1382 li = (ssize_t)strlen(insert);
1384 if (!*pattern) return;
1386 while (lp <= ls && (p = strstr(s,pattern)))
1388 if (len && (ls + (li-lp) >= (int)len))
1394 memmove(p+li,p+lp,strlen(p+lp)+1);
1396 memcpy(p, insert, li);
1402 #define CHANGE_TIME(A,B) \
1403 B.tm_hour = A.wHour; \
1404 B.tm_sec = A.wSecond; \
1405 B.tm_min = A.wMinute; \
1406 B.tm_mon = A.wMonth - 1; \
1408 B.tm_year = A.wYear - 1900; \
1409 B.tm_wday = A.wDayOfWeek - 1; \
1414 BOOL GetPathInfo(const char *fname,
1415 time_t *c_time, time_t *a_time, time_t *m_time,
1416 size_t *size, uint16 *mode)
1418 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1420 SYSTEMTIME SystemTime;
1423 rc = GetFileAttributesEx(fname, GetFileExInfoStandard, &FileInfo);
1428 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1429 CHANGE_TIME(SystemTime, tm_time)
1430 (*c_time) = mktime(&tm_time);
1434 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1435 CHANGE_TIME(SystemTime, tm_time)
1436 (*a_time) = mktime(&tm_time);
1440 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1441 CHANGE_TIME(SystemTime, tm_time)
1442 (*m_time) = mktime(&tm_time);
1447 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1448 *size = FileInfo.nFileSizeLow;
1459 /****************************************************************************
1460 send a qfileinfo call
1461 ****************************************************************************/
1462 BOOL GetFileInfo(char *FileName, HANDLE fd,
1463 uint16 *mode, size_t *size,
1464 time_t *c_time, time_t *a_time, time_t *m_time,
1467 WIN32_FILE_ATTRIBUTE_DATA FileInfo;
1469 SYSTEMTIME SystemTime;
1472 rc = GetFileAttributesEx(FileName, GetFileExInfoStandard, &FileInfo);
1477 rc = FileTimeToSystemTime(&FileInfo.ftCreationTime, &SystemTime);
1478 CHANGE_TIME(SystemTime, tm_time)
1479 (*c_time) = mktime(&tm_time);
1483 rc = FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SystemTime);
1484 CHANGE_TIME(SystemTime, tm_time)
1485 (*a_time) = mktime(&tm_time);
1489 rc = FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SystemTime);
1490 CHANGE_TIME(SystemTime, tm_time)
1491 (*m_time) = mktime(&tm_time);
1496 if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1497 *size = FileInfo.nFileSizeLow;
1508 /****************************************************************************
1509 Read size bytes at offset offset using SMBreadX.
1510 ****************************************************************************/
1512 ssize_t nb_read(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1521 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1523 if (LowDword == INVALID_SET_FILE_POINTER)
1525 rc = ReadFile(fd, IoBuffer, (DWORD)size, &total, NULL);
1532 /****************************************************************************
1534 ****************************************************************************/
1536 ssize_t nb_write(HANDLE fd, char *IoBuffer, off_t offset, size_t size)
1542 LowDword = SetFilePointer(fd, offset, 0, FILE_BEGIN);
1543 if (LowDword == INVALID_SET_FILE_POINTER)
1545 rc = WriteFile(fd, IoBuffer, (DWORD)size, &bwritten, NULL);
1548 FlushFileBuffers(fd);