@@ -205,6 +205,58 @@ bool ltfs_is_interrupted(void)
205205 return interrupted ;
206206}
207207
208+ bool caught_sigcont = false;
209+ void _ltfs_sigcont (int signal )
210+ {
211+ ltfsmsg (LTFS_INFO , 17294 I , signal );
212+ ltfs_sigcont_set (true);
213+ }
214+
215+ void ltfs_sigcont_set (bool sig_val )
216+ {
217+ caught_sigcont = sig_val ;
218+ }
219+
220+ bool ltfs_caught_sigcont (void )
221+ {
222+ return caught_sigcont ;
223+ }
224+
225+ int ltfs_extra_signal_handlers (void )
226+ #ifdef mingw_PLATFORM
227+ {
228+ return 0 ;
229+ }
230+ #else
231+ {
232+ ltfs_sighandler_t ret ;
233+ ret = signal (SIGCONT , _ltfs_sigcont );
234+ if (ret == SIG_ERR ) {
235+ return - LTFS_SIG_HANDLER_ERR ;
236+ }
237+
238+ return 0 ;
239+ }
240+ #endif
241+
242+ int ltfs_unset_extra_signal_handler (void )
243+ #ifdef mingw_PLATFORM
244+ {
245+ return 0 ;
246+ }
247+ #else
248+ {
249+ ltfs_sighandler_t rc ;
250+ int ret = 0 ;
251+
252+ rc = signal (SIGCONT , SIG_DFL );
253+ if (rc == SIG_ERR )
254+ ret = - LTFS_SIG_HANDLER_ERR ;
255+
256+ return ret ;
257+ }
258+ #endif
259+
208260/**
209261 * This function can be used to enable libltfs signal handler
210262 * to kill ltfs, mkltfs, ltfsck cleanly
@@ -2383,12 +2435,13 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol)
23832435 struct tape_offset old_selfptr , old_backptr ;
23842436 struct ltfs_timespec modtime_old = { .tv_sec = 0 , .tv_nsec = 0 };
23852437 bool generation_inc = false;
2386- struct tc_position physical_selfptr ;
2438+ struct tc_position physical_selfptr , current_position ;
23872439 char * cache_path_save = NULL ;
23882440 bool write_perm = (strcmp (reason , SYNC_WRITE_PERM ) == 0 );
23892441 bool update_vollock = false;
23902442 int volstat = -1 , new_volstat = 0 ;
23912443 char * bc_print = NULL ;
2444+ unsigned long long diff ;
23922445
23932446 CHECK_ARG_NULL (vol , - LTFS_NULL_ARG );
23942447
@@ -2505,6 +2558,24 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol)
25052558 vol -> index -> backptr = old_backptr ;
25062559 goto out_write_perm ;
25072560 }
2561+
2562+ /* Get the tape position from the tape drive by using the SCSI command READPOS*/
2563+ ret = tape_get_position_from_drive (vol -> device , & current_position );
2564+ if (ret < 0 ) {
2565+ /* Return error since the current tape position was unable to be determined, so there could be an undetected position mismatch */
2566+ ltfsmsg (LTFS_ERR , 11081E , ret );
2567+ return -1 ;
2568+ }
2569+
2570+ /* Prior to writing the index, compare the current location of the head position to the head location
2571+ that is kept in the cache of ltfs (physical_selfptr). If they are different return error (-1) */
2572+ diff = ((unsigned long long )physical_selfptr .block - (unsigned long long )current_position .block );
2573+ if (diff ) {
2574+ /* Position mismatch, diff not equal zero */
2575+ ltfsmsg (LTFS_INFO , 17293E , (unsigned long long )physical_selfptr .block , (unsigned long long )current_position .block );
2576+ return -1 ;
2577+ }
2578+
25082579 old_selfptr = vol -> index -> selfptr ;
25092580 vol -> index -> selfptr .partition = partition ;
25102581 vol -> index -> selfptr .partition = vol -> label -> part_num2id [physical_selfptr .partition ];
0 commit comments