Skip to content

Commit

Permalink
issue 325
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Nov 8, 2024
1 parent 99fe8d6 commit 4961287
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2024-11-08 Richard Frith-Macdonald <[email protected]>

* Headers/Foundation/NSFileHandle.h:
* Source/GSFileHandle.m:
* Source/NSFileHandle.m:
Add -truncateAtOffset:error: method (issue 325)

2024-11-03 Richard Frith-Macdonald <[email protected]>

* Source/NSFileManager.m: Fix case where positive result of asking
Expand Down
5 changes: 5 additions & 0 deletions Headers/Foundation/NSFileHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ GS_EXPORT_CLASS
- (void) synchronizeFile;
- (void) truncateFileAtOffset: (unsigned long long)pos;

#if OS_API_VERSION(MAC_OS_X_VERSION_10_15, GS_API_LATEST)
- (BOOL) truncateAtOffset: (unsigned long long)offset
error: (out NSError **)error;
#endif

@end

// Notification names.
Expand Down
16 changes: 15 additions & 1 deletion Source/GSFileHandle.m
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,6 @@ - (void) truncateFileAtOffset: (unsigned long long)pos
{
[NSException raise: NSFileHandleOperationException
format: @"attempt to truncate invalid file"];

}
if (ftruncate(descriptor, pos) < 0)
{
Expand All @@ -1732,6 +1731,21 @@ - (void) truncateFileAtOffset: (unsigned long long)pos
[self seekToFileOffset: pos];
}

- (BOOL) truncateAtOffset: (unsigned long long)offset
error: (out NSError **)error
{
if (ftruncate((isStandardFile ? descriptor : -1), offset) < 0)
{
if (error)
{
*error = [NSError _last];
}
return NO;
}
[self seekToFileOffset: offset];
return YES;
}

- (void) writeInBackgroundAndNotify: (NSData*)item forModes: (NSArray*)modes
{
NSMutableDictionary* info;
Expand Down
6 changes: 6 additions & 0 deletions Source/NSFileHandle.m
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ - (void) truncateFileAtOffset: (unsigned long long)pos
[self subclassResponsibility: _cmd];
}

- (BOOL) truncateAtOffset: (unsigned long long)offset
error: (out NSError **)error
{
[self subclassResponsibility: _cmd];
return NO;
}

@end

Expand Down

0 comments on commit 4961287

Please sign in to comment.