Simplest way to truncate a file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Simplest way to truncate a file

Joerg Beekmann, DeepCove Labs (YVR)
This is the simplest way I could see to truncate an external file. Is
there a more obvious approach?

 

(IOAccessor
                openFileNamed: aFilename
                direction: IOAccessor readWrite
                creation: IOAccessor noCreate)
                                truncateTo: anInteger;
                                close

>

-----
Joerg Beekmann
DeepCove Labs
4th floor 595 Howe Street
Vancouver, BC, V6C 2T5
voice +1.604.689.0322
fax   +1.604.689.0311
[hidden email] <blocked::mailto:[hidden email]>


CONFIDENTIALITY NOTICE - This email contains private and confidential
information. If it is not intended for you, delete it and any
attachments.

 

 


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Michael Lucas-Smith-2
On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
> This is the simplest way I could see to truncate an external file. Is
> there a more obvious approach?
>    
You don't need to do the truncateTo:, simply opening an existing file
for write only will truncate it. You just need to close it after that.

(IOAccessor
     openFileNamed: aFilename
     direction: IOAccessor writeOnly
     creation: IOAccessor truncateNoCreate)
         close

Cheers,
Michael
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Colin Putney, DeepCove Labs (YVR)
That'll only truncate to size 0, though. Say a file is 10 bytes long and
you want it to be 7 bytes long. #truncateTo: seems to be the only way to
do that.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On
Behalf Of Michael Lucas-Smith
Sent: Wednesday, March 03, 2010 10:57 AM
To: [hidden email]
Subject: Re: [vwnc] Simplest way to truncate a file

On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
> This is the simplest way I could see to truncate an external file. Is
> there a more obvious approach?
>    
You don't need to do the truncateTo:, simply opening an existing file
for write only will truncate it. You just need to close it after that.

(IOAccessor
     openFileNamed: aFilename
     direction: IOAccessor writeOnly
     creation: IOAccessor truncateNoCreate)
         close

Cheers,
Michael
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Michael Lucas-Smith-2
True, if you want to truncate it to a specific size - then the original
example is probably the fastest way to do it.

On 3/3/10 1:12 PM, Colin Putney, DeepCove Labs (YVR) wrote:

> That'll only truncate to size 0, though. Say a file is 10 bytes long and
> you want it to be 7 bytes long. #truncateTo: seems to be the only way to
> do that.
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Michael Lucas-Smith
> Sent: Wednesday, March 03, 2010 10:57 AM
> To: [hidden email]
> Subject: Re: [vwnc] Simplest way to truncate a file
>
> On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
>    
>> This is the simplest way I could see to truncate an external file. Is
>> there a more obvious approach?
>>
>>      
> You don't need to do the truncateTo:, simply opening an existing file
> for write only will truncate it. You just need to close it after that.
>
> (IOAccessor
>       openFileNamed: aFilename
>       direction: IOAccessor writeOnly
>       creation: IOAccessor truncateNoCreate)
>           close
>
> Cheers,
> Michael
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>    

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Boris Popov, DeepCove Labs (SNN)
In reply to this post by Joerg Beekmann, DeepCove Labs (YVR)
Re: [vwnc] Simplest way to truncate a file

I suspect Joerg and Colin were suggesting that these kinds of APIs deserve to be better exposed via the filename ;)

-Boris (via BlackBerry)

----- Original Message -----
From: [hidden email] <[hidden email]>
To: [hidden email] <[hidden email]>
Sent: Wed Mar 03 13:34:35 2010
Subject: Re: [vwnc] Simplest way to truncate a file

True, if you want to truncate it to a specific size - then the original
example is probably the fastest way to do it.

On 3/3/10 1:12 PM, Colin Putney, DeepCove Labs (YVR) wrote:
> That'll only truncate to size 0, though. Say a file is 10 bytes long and
> you want it to be 7 bytes long. #truncateTo: seems to be the only way to
> do that.
>
> -----Original Message-----
> From: [hidden email] [[hidden email]] On
> Behalf Of Michael Lucas-Smith
> Sent: Wednesday, March 03, 2010 10:57 AM
> To: [hidden email]
> Subject: Re: [vwnc] Simplest way to truncate a file
>
> On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
>   
>> This is the simplest way I could see to truncate an external file. Is
>> there a more obvious approach?
>>
>>     
> You don't need to do the truncateTo:, simply opening an existing file
> for write only will truncate it. You just need to close it after that.
>
> (IOAccessor
>       openFileNamed: aFilename
>       direction: IOAccessor writeOnly
>       creation: IOAccessor truncateNoCreate)
>           close
>
> Cheers,
> Michael
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>   

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Colin Putney, DeepCove Labs (YVR)
Re: [vwnc] Simplest way to truncate a file

Heh. Well, actually, this came up because I wanted to truncate a file that I had an ExternalReadWriteStream on. I figured it would be something like  [stream position: 7; truncate].

 

But #truncate doesn’t exist. So I poked around and found IOAccessor>>truncateTo:, which has no senders. I tried implementing #truncate, but found that buffering gets in the way – as soon as you close the file, the buffer gets flushed, undoing the truncation. Updating the buffer state of an ExternalReadWriteStream is decidedly non-trivial, so I went in a different direction and did something that didn’t require truncating the file.

 

I’m a bit surprised that such a fundamental operation isn’t implemented, but I guess folks find work-arounds, just like I did.

 

Colin

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Boris Popov, DeepCove Labs (YVR)
Sent: Wednesday, March 03, 2010 2:09 PM
To: [hidden email]; [hidden email]
Subject: Re: [vwnc] Simplest way to truncate a file

 

I suspect Joerg and Colin were suggesting that these kinds of APIs deserve to be better exposed via the filename ;)

-Boris (via BlackBerry)

----- Original Message -----
From: [hidden email] <[hidden email]>
To: [hidden email] <[hidden email]>
Sent: Wed Mar 03 13:34:35 2010
Subject: Re: [vwnc] Simplest way to truncate a file

True, if you want to truncate it to a specific size - then the original
example is probably the fastest way to do it.

On 3/3/10 1:12 PM, Colin Putney, DeepCove Labs (YVR) wrote:
> That'll only truncate to size 0, though. Say a file is 10 bytes long and
> you want it to be 7 bytes long. #truncateTo: seems to be the only way to
> do that.
>
> -----Original Message-----
> From: [hidden email] [[hidden email]] On
> Behalf Of Michael Lucas-Smith
> Sent: Wednesday, March 03, 2010 10:57 AM
> To: [hidden email]
> Subject: Re: [vwnc] Simplest way to truncate a file
>
> On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
>   
>> This is the simplest way I could see to truncate an external file. Is
>> there a more obvious approach?
>>
>>     
> You don't need to do the truncateTo:, simply opening an existing file
> for write only will truncate it. You just need to close it after that.
>
> (IOAccessor
>       openFileNamed: aFilename
>       direction: IOAccessor writeOnly
>       creation: IOAccessor truncateNoCreate)
>           close
>
> Cheers,
> Michael
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>   

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Dennis smith-4
Orperhaps people just don't need that capability?

I cannot remember a time in 40years of computing (not all ST :) ) where I needed to do that,
so I would THINK its not that common??

On 3/3/2010 7:30 PM, Colin Putney, DeepCove Labs (YVR) wrote:
Re: [vwnc] Simplest way to truncate a file

Heh. Well, actually, this came up because I wanted to truncate a file that I had an ExternalReadWriteStream on. I figured it would be something like  [stream position: 7; truncate].

 

But #truncate doesn’t exist. So I poked around and found IOAccessor>>truncateTo:, which has no senders. I tried implementing #truncate, but found that buffering gets in the way – as soon as you close the file, the buffer gets flushed, undoing the truncation. Updating the buffer state of an ExternalReadWriteStream is decidedly non-trivial, so I went in a different direction and did something that didn’t require truncating the file.

 

I’m a bit surprised that such a fundamental operation isn’t implemented, but I guess folks find work-arounds, just like I did.

 

Colin

 

 

From: [hidden email] [[hidden email]] On Behalf Of Boris Popov, DeepCove Labs (YVR)
Sent: Wednesday, March 03, 2010 2:09 PM
To: [hidden email]; [hidden email]
Subject: Re: [vwnc] Simplest way to truncate a file

 

I suspect Joerg and Colin were suggesting that these kinds of APIs deserve to be better exposed via the filename ;)

-Boris (via BlackBerry)

----- Original Message -----
From: [hidden email] [hidden email]
To: [hidden email] [hidden email]
Sent: Wed Mar 03 13:34:35 2010
Subject: Re: [vwnc] Simplest way to truncate a file

True, if you want to truncate it to a specific size - then the original
example is probably the fastest way to do it.

On 3/3/10 1:12 PM, Colin Putney, DeepCove Labs (YVR) wrote:
> That'll only truncate to size 0, though. Say a file is 10 bytes long and
> you want it to be 7 bytes long. #truncateTo: seems to be the only way to
> do that.
>
> -----Original Message-----
> From: [hidden email] [[hidden email]] On
> Behalf Of Michael Lucas-Smith
> Sent: Wednesday, March 03, 2010 10:57 AM
> To: [hidden email]
> Subject: Re: [vwnc] Simplest way to truncate a file
>
> On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
>   
>> This is the simplest way I could see to truncate an external file. Is
>> there a more obvious approach?
>>
>>     
> You don't need to do the truncateTo:, simply opening an existing file
> for write only will truncate it. You just need to close it after that.
>
> (IOAccessor
>       openFileNamed: aFilename
>       direction: IOAccessor writeOnly
>       creation: IOAccessor truncateNoCreate)
>           close
>
> Cheers,
> Michael
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>   

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

_______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

-- 
Dennis Smith                 		         +1 416.798.7948
Cherniak Software Development Corporation   Fax: +1 416.798.0948
509-2001 Sheppard Avenue East        [hidden email]
Toronto, ON M2J 4Z8              <a class="moz-txt-link-freetext" href="sip:dennis@CherniakSoftware.com">sip:dennis@...
Canada			         http://www.CherniakSoftware.com
Entrance off Yorkland Blvd south of Sheppard Ave east of the DVP

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Winemiller, Chris
Re: [vwnc] Simplest way to truncate a file

It may not be common, but there is a need for it.

 

I’ve implemented ExternalReadWriteStream >> truncate and PositionableStream >> truncate, and both appear to work successfully. I’ve not experienced any buffering issues that Colin mentioned below.

 

Here are my implementations:

 

PositionableStream >> truncate

            readLimit := position

 

ExternalReadWriteStream >> truncate

            self ioConnection input truncateTo: self position

 

Chris


From: [hidden email] [mailto:[hidden email]] On Behalf Of Dennis Smith
Sent: Wednesday, March 03, 2010 6:45 PM
To: [hidden email]
Subject: Re: [vwnc] Simplest way to truncate a file

 

Orperhaps people just don't need that capability?

I cannot remember a time in 40years of computing (not all ST :) ) where I needed to do that,
so I would THINK its not that common??

On 3/3/2010 7:30 PM, Colin Putney, DeepCove Labs (YVR) wrote:

Heh. Well, actually, this came up because I wanted to truncate a file that I had an ExternalReadWriteStream on. I figured it would be something like  [stream position: 7; truncate].

 

But #truncate doesn’t exist. So I poked around and found IOAccessor>>truncateTo:, which has no senders. I tried implementing #truncate, but found that buffering gets in the way – as soon as you close the file, the buffer gets flushed, undoing the truncation. Updating the buffer state of an ExternalReadWriteStream is decidedly non-trivial, so I went in a different direction and did something that didn’t require truncating the file.

 

I’m a bit surprised that such a fundamental operation isn’t implemented, but I guess folks find work-arounds, just like I did.

 

Colin

 

 

From: [hidden email] [[hidden email]] On Behalf Of Boris Popov, DeepCove Labs (YVR)
Sent: Wednesday, March 03, 2010 2:09 PM
To: [hidden email]; [hidden email]
Subject: Re: [vwnc] Simplest way to truncate a file

 

I suspect Joerg and Colin were suggesting that these kinds of APIs deserve to be better exposed via the filename ;)

-Boris (via BlackBerry)

----- Original Message -----
From: [hidden email] [hidden email]
To: [hidden email] [hidden email]
Sent: Wed Mar 03 13:34:35 2010
Subject: Re: [vwnc] Simplest way to truncate a file

True, if you want to truncate it to a specific size - then the original
example is probably the fastest way to do it.

On 3/3/10 1:12 PM, Colin Putney, DeepCove Labs (YVR) wrote:
> That'll only truncate to size 0, though. Say a file is 10 bytes long and
> you want it to be 7 bytes long. #truncateTo: seems to be the only way to
> do that.
>
> -----Original Message-----
> From: [hidden email] [[hidden email]] On
> Behalf Of Michael Lucas-Smith
> Sent: Wednesday, March 03, 2010 10:57 AM
> To: [hidden email]
> Subject: Re: [vwnc] Simplest way to truncate a file
>
> On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
>   
>> This is the simplest way I could see to truncate an external file. Is
>> there a more obvious approach?
>>
>>     
> You don't need to do the truncateTo:, simply opening an existing file
> for write only will truncate it. You just need to close it after that.
>
> (IOAccessor
>       openFileNamed: aFilename
>       direction: IOAccessor writeOnly
>       creation: IOAccessor truncateNoCreate)
>           close
>
> Cheers,
> Michael
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>   

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

 
 
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
  



-- 
Dennis Smith                                   +1 416.798.7948
Cherniak Software Development Corporation   Fax: +1 416.798.0948
509-2001 Sheppard Avenue East        [hidden email]
Toronto, ON M2J 4Z8              <a
href="sip:dennis@CherniakSoftware.com">sip:dennis@...
Canada                          http://www.CherniakSoftware.com
Entrance off Yorkland Blvd south of Sheppard Ave east of the DVP

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Simplest way to truncate a file

Joerg Beekmann, DeepCove Labs (YVR)
In reply to this post by Boris Popov, DeepCove Labs (SNN)
Re: [vwnc] Simplest way to truncate a file
Indeed, it was one of those "really, there is no high level way to do that, that's surprising" kind of moments. So much so that I thought perhaps we had missed something. That said the the low-level API does work.
 
Joerg


From: [hidden email] [mailto:[hidden email]] On Behalf Of Boris Popov, DeepCove Labs (YVR)
Sent: Wednesday, March 03, 2010 2:09 PM
To: [hidden email]; [hidden email]
Subject: Re: [vwnc] Simplest way to truncate a file

I suspect Joerg and Colin were suggesting that these kinds of APIs deserve to be better exposed via the filename ;)

-Boris (via BlackBerry)

----- Original Message -----
From: [hidden email] <[hidden email]>
To: [hidden email] <[hidden email]>
Sent: Wed Mar 03 13:34:35 2010
Subject: Re: [vwnc] Simplest way to truncate a file

True, if you want to truncate it to a specific size - then the original
example is probably the fastest way to do it.

On 3/3/10 1:12 PM, Colin Putney, DeepCove Labs (YVR) wrote:


> That'll only truncate to size 0, though. Say a file is 10 bytes long and
> you want it to be 7 bytes long. #truncateTo: seems to be the only way to
> do that.
>
> -----Original Message-----
> From: [hidden email] [[hidden email]] On
> Behalf Of Michael Lucas-Smith
> Sent: Wednesday, March 03, 2010 10:57 AM
> To: [hidden email]
> Subject: Re: [vwnc] Simplest way to truncate a file
>
> On 3/3/10 10:49 AM, Joerg Beekmann, DeepCove Labs (YVR) wrote:
>   
>> This is the simplest way I could see to truncate an external file. Is
>> there a more obvious approach?
>>
>>     
> You don't need to do the truncateTo:, simply opening an existing file
> for write only will truncate it. You just need to close it after that.
>
> (IOAccessor
>       openFileNamed: aFilename
>       direction: IOAccessor writeOnly
>       creation: IOAccessor truncateNoCreate)
>           close
>
> Cheers,
> Michael
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>   

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc