void main( void ) { int handle; unsigned date, time; if( _dos_open( "file", O_RDWR, &handle ) != 0 ) { printf( "Unable to open file\n" ); } else { printf( "Open succeeded\n" ); _dos_getftime( handle, &date, &time ); printf( "The file was last modified on %d/%d/%d", MONTH(date), DAY(date), YEAR(date) ); printf( " at %.2d:%.2d:%.2d\n", HOUR(time), MINUTE(time), SECOND(time) ); /* set the time to 12 noon */ time = (12 << 11) + (0 << 5) + 0; _dos_setftime( handle, date, time ); _dos_getftime( handle, &date, &time ); printf( "The file was last modified on %d/%d/%d", MONTH(date), DAY(date), YEAR(date) ); printf( " at %.2d:%.2d:%.2d\n", HOUR(time), MINUTE(time), SECOND(time) ); _dos_close( handle ); } }
BOOL FCopy (char *src, char *dst) { HANDLE srcfh, dstfh; BOOL result; ATTRIBUTE_TYPE Attributes; unsigned filedate, filetime; GET_ATTRIBUTES(src, Attributes); if (Attributes == FILE_ATTRIBUTE_DIRECTORY) { fprintf( stderr, "\nUnable to open source"); return FALSE; } if (_dos_creatnew( src, _A_RDONLY, &srcfh) != 0) if (_dos_open( src, O_RDONLY, &srcfh) != 0) { fprintf( stderr, "\nUnable to open source, error code %d", GetLastError() ); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); return FALSE; } if (_dos_getftime(srcfh, &filedate, &filetime) != 0) { fprintf( stderr, "\nUnable to get time of source"); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); return FALSE; } if (_dos_creatnew( dst, _A_NORMAL, &dstfh) != 0) if (_dos_open( dst, O_RDWR, &dstfh) != 0) { fprintf( stderr, "\nUnable to create destination, error code %d", GetLastError() ); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); if (dstfh != INVALID_HANDLE_VALUE) CloseHandle( dstfh ); return FALSE; } result = fastcopy( srcfh, dstfh ); if(!result) { if (dstfh != INVALID_HANDLE_VALUE) { CloseHandle( dstfh ); dstfh = INVALID_HANDLE_VALUE; } DeleteFile( dst ); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); fprintf( stderr, "\nUnable to copy file"); return FALSE; } if (_dos_setftime(dstfh, filedate, filetime != 0)) { fprintf( stderr, "\nUnable to set time of destination"); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); if (dstfh != INVALID_HANDLE_VALUE) CloseHandle( dstfh ); return FALSE; } if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); if (dstfh != INVALID_HANDLE_VALUE) CloseHandle( dstfh ); return TRUE; } // FCopy
/* ** int CopyDateTimeStamp(int doshFrom, int doshTo); ** ** Copy date and time stamp from one file to another. ** ** Arguments: doshFrom - date and time stamp source DOS file handle ** doshTo - target DOS file handle ** ** Returns: TRUE if successful. LZERROR_BADINHANDLE or ** LZERROR_BADOUTHANDLE if unsuccessful. ** ** Globals: none ** ** N.b., stream-style I/O routines like fopen() and fclose() may counter the ** intended effect of this function. fclose() writes the current date to any ** file it's called with which was opened in write "w" or append "a" mode. ** One way to get around this in order to modify the date of a file opened ** for writing or appending by fopen() is to fclose() the file and fopen() it ** again in read "r" mode. Then set its date and time stamp with ** CopyDateTimeStamp(). */ INT CopyDateTimeStamp(INT doshFrom, INT doshTo) { #ifdef ORGCODE // DOS prototypes from <dos.h> extern DWORD _dos_getftime(INT dosh, DWORD *puDate, DWORD *puTime); extern DWORD _dos_setftime(INT dosh, DWORD uDate, DWORD uTime); #ifdef LZA_DLL static #endif DWORD uFrom_date, // temporary storage for date uFrom_time; // and time stamps if (_dos_getftime(doshFrom, &uFrom_date, &uFrom_time) != 0u) return((INT)LZERROR_BADINHANDLE); if (_dos_setftime(doshTo, uFrom_date, uFrom_time) != 0u) return((INT)LZERROR_BADOUTHANDLE); #else FILETIME lpCreationTime, lpLastAccessTime, lpLastWriteTime; if(!GetFileTime((HANDLE) doshFrom, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime)){ return((INT)LZERROR_BADINHANDLE); } if(!SetFileTime((HANDLE) doshTo, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime)){ return((INT)LZERROR_BADINHANDLE); } #endif return(TRUE); }
void main(int argc, char *argv[]) { union { struct Date { unsigned int day:5; unsigned int month:4; unsigned int years:7; } bits; unsigned value; } date; union { struct Time { unsigned seconds:5; unsigned minutes:6; unsigned hours:5; } bits; unsigned value; } time; int handle; if (_dos_open(argv[1], O_RDONLY, &handle)) fprintf(stderr, "Error opening source file\n"); else { date.bits.day = 4; date.bits.month = 7; date.bits.years = 14; // 1980 + 14 time.bits.hours = 12; time.bits.minutes = 0; time.bits.seconds = 0; if (_dos_setftime(handle, date.value, time.value)) printf("Error setting date/time stamp\n"); _dos_close(handle); } }
signed char do_restore(nwBackupParms * parms, char * remote_name, char * local_dir, char * logfile) { char ctrl_file_name[L_tmpnam]; FILE * ctrl_file; dirStack_t dstack; dirStruct_t dummy_curr_dir; fileStruct_t dummy_curr_file; nwBackupCodes nw_rc = SUCCESS; int do_restore_rc = 0; int path_eostr; char current_file_name[FILENAME_MAX]; char * path, * path_and_file, * unix_path, \ * unix_path_and_file, * file_buffer, * in_buffer; uint8_t * ctrl_buffer; path = malloc(129 * 4); file_buffer = malloc(FILE_BUF_SIZE); in_buffer = malloc(OUTBUF_SIZE); ctrl_buffer = malloc(BUFSIZ); /* Should be more than enough... handle dynamically sizing it later. */ //dummy_outbuf = calloc(OUTBUF_SIZE, 1); //This doesn't set the first and third byte //to zero! if(path == NULL || file_buffer == NULL || in_buffer == NULL || ctrl_buffer == NULL) { fprintf(stderr, "Could not allocate memory for path or file buffers!\n"); return -1; } /* Don't bother freeing till end of program- if error, program will terminate soon anyway! */ path_and_file = path + 129; unix_path = path_and_file + 129; unix_path_and_file = unix_path + 129; if(initDirStack(&dstack)) { fprintf(stderr, "Directory Stack Initialization failed!\n"); return -2; } if(!strcpy(path, local_dir)) /* if(strncpy(path, argv[1], DIR_MAX_PATH + 1) >= (DIR_MAX_PATH + 1)) */ { /* Shouldn't fail... but Heartbleed convinces me to code defensively. */ fprintf(stderr, "Specified path name is too long...\n" "Actually if we're got this error on DOS, we have more serious\n" "trouble than just a bad path!\n"); } path_eostr = strlen(path); if(path[path_eostr - 1] == 92) { /* Backslash */ path[path_eostr - 1] = '\0'; local_dir[path_eostr - 1] = '\0'; /* We will need local dir again! */ path_eostr--; /* path_eostr now points to the root path's NULL terminator. */ } //full_unix_path = malloc(strlen(parms-> strlen(remote_name) + ); /* If doing an absolute-path restore, the first push should be deferred until the control file header is read. */ if(pushDir(&dstack, &dummy_curr_dir, path)) { fprintf(stderr, "Initial directory push failed!\n"); return -3; } if(tmpnam(ctrl_file_name) == NULL) { fprintf(stderr, "Attempt to allocate tmpnam() for receiving CONTROL failed!\n"); return -4; } ctrl_file = fopen(ctrl_file_name, "wb+"); if(ctrl_file == NULL) { fprintf(stderr, "Attempt to open temp file for receiving CONTROL failed!\n"); return -5; } nw_rc = initRemote(parms); if(nw_rc != SUCCESS) { do_restore_rc = -6; } nw_rc = chDirRemote(remote_name); if(!(nw_rc == SUCCESS)) { do_restore_rc = -7; } if(!do_restore_rc) { /* Grab the control file from the server */ fprintf(stderr, "Receiving control file from the server (no retry)...\n"); setvbuf(ctrl_file, file_buffer, _IOFBF, FILE_BUF_SIZE); if(restore_file(ctrl_file, "CONTROL.NFO", in_buffer, OUTBUF_SIZE)) { fprintf(stderr, "Couldn't receive control file (%s) to the server. Supply" "the control file manually (not implemented) and try again.\n", ctrl_file_name); do_restore_rc = -8; } else { /* Control file was successfully grabbed. We can continue. */ ctrlEntryType_t entry_type; int8_t all_dirs_restored = 0; unsigned int attr, time, date; long unsigned int size; fprintf(stderr, "Control file received successfully from the server.\n"); fclose(ctrl_file); /* Assume this doesn't fail for now */ ctrl_file = fopen(ctrl_file_name, "rb"); entry_type = getNextEntry(ctrl_file, ctrl_buffer, BUFSIZ); while(!all_dirs_restored && do_restore_rc == 0) { FILE * curr_file; int temp; switch(entry_type) { case CTRL_HEADER: /* For now, absolute-path restores are disabled. Restores are relative to the directory in which the program is invoked. In effect, this code does nothing! */ //parseHeaderEntry(ctrl_buffer, path); /* if(pushDir(&dstack, &dummy_curr_dir, path)) { fprintf(stderr, "Initial directory push failed!\n"); do_restore_rc = -16; } */ fprintf(stderr, "Root directory: %s\n", path); break; case CTRL_DIR: temp = parseDirEntry(ctrl_buffer, current_file_name, &attr, &time, &date, &size); /* Change to snprintf soon */ sprintf(path_and_file, "%s\\%s", path, current_file_name); strcpy(path, path_and_file); unix_path[0] = '\0'; /* Skip the leading separator for now... */ if(createUnixName(unix_path, &path[path_eostr + 1]) == NULL) { fprintf(stderr, "Unix directory name creation failed!\n"); do_restore_rc = -9; break; } /* fprintf(stderr, "Return code: %d Curr directory: %s, Attr: %hu\nUnix name: %s\n",\ temp, path, attr, unix_path); */ if(_mkdir(path)) { fprintf(stderr, "Directory creation failed (%s)!\n", path); do_restore_rc = -10; } else { int dos_handle; fprintf(stderr, "Directory created: %s\n", path); if(_dos_open(path, O_RDONLY, &dos_handle)) { fprintf(stderr, "Warning: Could not open directory to set attributes!\n"); } else { if(_dos_setftime(dos_handle, date, time)) { fprintf(stderr, "Warning: Could not reset date/time on directory %s!\n", path); } _dos_close(dos_handle); if(_dos_setfileattr(path_and_file, attr)) { fprintf(stderr, "Warning: Could not set attributes on directory %s!\n", path); } } } //getchar(); break; case CTRL_FILE: /* Should not cause buffer overflow, since sizeof(current_file_name) set to FILENAME_MAX */ temp = parseFileEntry(ctrl_buffer, current_file_name, &attr, &time, &date, &size); /* Skip the leading separator for now... */ sprintf(path_and_file, "%s\\%s", path, current_file_name); if(!strcmp(path, local_dir)) { /* Don't copy a separator if the path is at the root... otherwise the server won't find the file and/or think the file is at the server's root! */ strcpy(unix_path_and_file, current_file_name); } else { sprintf(unix_path_and_file, "%s/%s", unix_path, current_file_name); } /* fprintf(stderr, "Return code: %d Curr directory: %s, Attr: %hu, Time %hu, Date %hu, Size %lu\n" \ "Unix directory: %s\n", temp, path_and_file, attr, time, date, size, unix_path_and_file); */ /* Receive file scope block. */ { int retry_count = 0; int8_t local_error = 0, rcv_done = 0; fprintf(stderr, "Receiving file %s...\n", unix_path_and_file); while(!rcv_done && !local_error && retry_count <= 3) { int8_t rcv_remote_rc; if(retry_count) { fprintf(stderr, "Retrying operation... (%d)\n", rcv_remote_rc); } curr_file = fopen(path_and_file, "wb"); setvbuf(curr_file, file_buffer, _IOFBF, FILE_BUF_SIZE); rcv_remote_rc = restore_file(curr_file, unix_path_and_file, in_buffer, OUTBUF_SIZE); //rcv_remote_rc = 0; fclose(curr_file); /* Close the file no matter what */ switch(rcv_remote_rc) { case 0: rcv_done = 1; break; case -2: fprintf(stderr, "Read error on file: %s! Not continuing.", path_and_file); local_error = 1; /* Local file error. */ break; case -1: /* Recoverable error. */ default: break; } retry_count++; } if(local_error) { /* If file error, we need to break out. */ do_restore_rc = -11; } else if(retry_count > 3) { do_restore_rc = -12; } else { /* File receive ok, try setting attributes now. */ int dos_handle; if(_dos_open(path_and_file, O_RDONLY, &dos_handle)) { fprintf(stderr, "Warning: Could not open file to set attributes!\n"); } else { if(_dos_setftime(dos_handle, date, time)) { fprintf(stderr, "Warning: Could not reset date/time on file %s!\n", path_and_file); } _dos_close(dos_handle); if(_dos_setfileattr(path_and_file, attr)) { fprintf(stderr, "Warning: Could not set attributes on file %s!\n", path_and_file); } } } } break; case CTRL_CDUP: /* Remove the deepest directory of the path, as long as we are not back at the invocation directory. */ //fprintf(stderr, "CDUP occurred.\n"); if(strcmp(path, local_dir)) { char * separator = strrchr(path, 92); if(separator != NULL) { /* Two characters need to be set to null because */ (* separator) = '\0'; //fprintf(stderr, "Path was stripped. ); } fprintf(stderr, "Directory change. New path is: %s\n", path); /* Skip the leading separator for now... we need to recreate the unix path in case a directory does not follow next! */ if(createUnixName(unix_path, &path[path_eostr + 1]) == NULL) { fprintf(stderr, "Unix directory name creation failed!\n"); do_restore_rc = -13; break; } //getchar(); } break; case CTRL_EOF: all_dirs_restored = 1; fprintf(stderr, "End of control file.\n"); break; default: fprintf(stderr, "Unexpected data from control file!\n"); do_restore_rc = -14; break; } entry_type = getNextEntry(ctrl_file, ctrl_buffer, BUFSIZ); fprintf(stderr, "\n"); } } } if(do_restore_rc) { fprintf(stderr, "Full restore failed with status code %d.\n", do_restore_rc); } else { fprintf(stderr, "Full restore completed successfully.\n"); } fclose(ctrl_file); closeRemote(); remove(ctrl_file_name); return do_restore_rc; }
DWORD FAR PASCAL RepSrcToD (const char *szSrcFil, const char *szDstFil, WORD usRepFlg, LPSTR lpWrkBuf, WORD usBufSiz, BFRREPFNC pfRepFnc, void far *lpRepBlk) { short sSrcHdl; short sDstHdl; static char szBakFil[FIOMAXNAM]; static char szTmpFil[FIOMAXNAM]; char * pbWrkPtr; DWORD ulRepCnt = 0L; static int file_date; static int file_time; unsigned int uiErrCod; /********************************************************************/ /********************************************************************/ sSrcHdl = FilHdlOpn (szSrcFil, O_BINARY | O_RDONLY); if (-1 == sSrcHdl) { MsgDspUsr ("Cannot Open Source File: %s\n", szSrcFil); return (0L); } if (usRepFlg & BFRDATFLG) _dos_getftime (sSrcHdl, &file_date, &file_time); /********************************************************************/ /* If null destination, create a temporary file in the current */ /* working directory. */ /* If not null, check for "*" wildcard or drive/dir only spec */ /********************************************************************/ if (!strlen (szDstFil)) { _getcwd (szTmpFil, FIOMAXNAM); if (NULL == (pbWrkPtr = _tempnam (szTmpFil, "B_"))) { FilHdlCls (sSrcHdl); MsgDspUsr ("Cannot Create Temporary File: %s\n", szTmpFil); return (0L); } strcpy (szTmpFil, pbWrkPtr); free (pbWrkPtr); } else { GetWldNam (szSrcFil, szDstFil, szTmpFil); /****************************************************************/ /****************************************************************/ if (0 == _stricmp (szSrcFil, szTmpFil)) { FilHdlCls (sSrcHdl); MsgDspUsr ("Cannot Overwrite Source File: %s\n", szSrcFil); return (0L); } } /********************************************************************/ /* Open a zero length target file */ /********************************************************************/ sDstHdl = FilHdlCre (szTmpFil, O_BINARY | O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE); if (-1 == sDstHdl) { FilHdlCls (sSrcHdl); MsgDspUsr ("Cannot Open Target File: %s\n", szTmpFil); return (0L); } /********************************************************************/ /* Perform replacement */ /* If save date flag set, set Dst file date and time to equal Src */ /********************************************************************/ ulRepCnt = pfRepFnc (sSrcHdl, sDstHdl, szSrcFil, szDstFil, lpWrkBuf, usBufSiz, lpRepBlk); if (usRepFlg & BFRDATFLG) _dos_setftime (sDstHdl, file_date, file_time); FilHdlCls (sSrcHdl); FilHdlCls (sDstHdl); if (-1L == ulRepCnt) { MsgDspUsr ("Cannot replace to target file: %s\n", szTmpFil); return (0L); } /********************************************************************/ /* If target specified AND no replacements, delete empty dest file */ /* If no target specified AND no replacements, delete temp file */ /********************************************************************/ if (0L == ulRepCnt) { remove (szTmpFil); /* Delete empty file */ return (0L); } if (*szDstFil != '\0') return (ulRepCnt); /* Done (explicit dest) */ /********************************************************************/ /* If no target specified AND backup requested, save backup */ /********************************************************************/ if (!(usRepFlg & BFRBKIFLG)) { strcpy (szBakFil, szSrcFil); if ((pbWrkPtr = strrchr (szBakFil, '.')) != NULL) strcpy (pbWrkPtr, ".BK1"); else strcat (szBakFil, ".BK1"); remove (szBakFil); /* Del old backups */ if (rename (szSrcFil, szBakFil)) { /* Rename original */ MsgDspUsr ("Cannot create backup: %s\n", szBakFil); remove (szTmpFil); /* Delete temp file */ return (0L); } } /********************************************************************/ /* Rename original, or copy if in different drive/directory */ /********************************************************************/ if (FilMov (szTmpFil, szSrcFil, lpWrkBuf, usBufSiz, usRepFlg & BFRDATFLG, &uiErrCod, NULL, 0L)) { MsgDspUsr ("Cannot copy to file: %s, attempting recovery...\n", szSrcFil); if (!(usRepFlg & BFRBKIFLG)) { remove (szSrcFil); /* Delete bad copy */ rename (szBakFil, szSrcFil); /* Un-rename orig */ } else rename (szTmpFil, szSrcFil); /* Simple rename */ ulRepCnt = 0L; } /********************************************************************/ /********************************************************************/ return (ulRepCnt); }
int ReceiveFile (char *FileName) { dword FileID = 0, FileSize = 0, CurSize = 0; bool FileNotReceive = true; int hFile = -1; ReceiveACluster (LEN_SEQ); // Init receive if (ErrAudit) printf ("|--Cluster--|-----I----|----II----|----III---|--Summary--|\n"); dword CurBlock = 0; while (FileNotReceive && !StopByUser) { CheckOnFreeComBuffer (); for (dword i = 0; i < MAX_AS_COM; i++) { if (ComTable [i].Semaphore == END_DEC_COM) { byte *ClusterBuffer = (byte*)ComTable [i].Buffer; FileHdr *filehdr = (FileHdr*)ClusterBuffer; if (CurBlock && filehdr->FileID != FileID) { //brk (); continue; // Go to next cluster } bool ClusterNotEmpty = true; dword CountBlock = 0; while (ClusterNotEmpty && FileNotReceive) { BlockHdr *pBH = (BlockHdr*) ((dword)ClusterBuffer + sizeof (FileHdr)); for (dword j = 0; j < filehdr->NumBlock; j++, pBH++) if (pBH->Number == CurBlock) break; // end loop // next block not found in current cluster if (pBH->Number != CurBlock) break; // Go to next cluster // find first block our file by name in file attributs if (!pBH->Number) { char *ArchiveName = (char*) ((dword)ClusterBuffer + pBH->ClusterOffset + sizeof (FileAttr)); FileAttr *fattr = (FileAttr*) ((dword)ClusterBuffer + pBH->ClusterOffset); // Other file - skip // if (stricmp (ArchiveName, FileName)); // { // brk (); // break; // Go to next cluster // } char NameBuff [128]; strcpy (NameBuff, ArchiveName); strcat (NameBuff, ".out"); // Create file if ((hFile = open (NameBuff, O_CREAT | O_BINARY | O_WRONLY, 0222)) == -1) return hFile; _dos_setfileattr (NameBuff, fattr->Attr); _dos_setftime (hFile, fattr->Date, fattr->Time); FileID = filehdr->FileID; // Set hook variable FileAttr *pFA = (FileAttr*)((dword)ClusterBuffer + pBH->ClusterOffset); FileSize = pFA->Size; } else { printf ("Receive block num:%6d, len:%6d\n", pBH->Number, pBH->Len); fflush (stdout); write (hFile, (byte*)((dword)ClusterBuffer + pBH->ClusterOffset), pBH->Len); CurSize += pBH->Len; } CurBlock++; CountBlock++; if (CountBlock == filehdr->NumBlock) { ClusterNotEmpty = false; ReceiveACluster (1); } if (FileSize == CurSize) { close (hFile); FileNotReceive = false; ClusterNotEmpty = false; } } // End while cluster not empty if (!ClusterNotEmpty && !StopByUser) { if (ErrAudit) { printf ("| %8d | %8d | %8d | %8d | %8d |\n", i, ComTable [i].ES.OrdinaryErr, ComTable [i].ES.DupletErr, ComTable [i].ES.TripletErr, ComTable [i].ES.OrdinaryErr + (ComTable [i].ES.DupletErr * 2) + (ComTable [i].ES.TripletErr * 3)); } FreeSemaphor (i); } } // end if find END_DECODE_COMMAND } // end for } // End while FileNotReceive PrintRFStatisics (); return 1; }