views:

482

answers:

3

Is there a O(1) way in windows API to concatenate 2 files?

O(1) with respect to not having to read in the entire second file and write it out to the file you want to append to. So as opposed to O(n) bytes processed.

I think this should be possible at the file system driver level, and I don't think there is a user mode API available for this, but I thought I'd ask.

+3  A: 

No, there isn't.

The best you could hope for is O(n), where n is the length of the shorter of the two files.

Max
What would the n being the shorter of the two files having to do with anything in terms of Big-Oh analysis?
BobbyShaftoe
It doesn't. The time to concatenate files in windows is linear corresponding to the size of the second file, the one you need to tuck onto the end of the first.
Lasse V. Karlsen
It's a community wiki. Just fix it :P
Artelius
I think you can do it with a user mode file system filter (a so-called "mini filter"), that will take 2 files and treat them as one
Scott Wisniewski
for sure you can at the driver level ya.
Brian R. Bondy
BobbyShaftoe: There's nothing intrinsically wrong with analysing something in terms of more than one parameter -- it's simply a matter of how much detail you want to give. Substitute "O(min(n, m)), where n and m are the lengths of the files" if it makes you feel better.
j_random_hacker
Considering the minimum of the two file sizes means that the order of concatenation isn't important. Surely that can't be right, though.
Rob Kennedy
I see what you mean Rob. :)
j_random_hacker
+2  A: 

From a theoretical perspective, this is possible (on-disk) provided that:

  • the second file is destroyed
  • the concatenation honours the filesystem's fragment alignment (e.g. occurs on a cluster boundary)
Artelius
I know on disk it could be possible, just don't know if this is available without a low level driver.
Brian R. Bondy
+4  A: 

If the "new file" is only going to be read by your application, then you can get away without actually concatenating them on disk.

You can just implement a stream interface that behaves as if the two files have been concatenated, and then use that stream as opposed to what ever the default filestream implementation used by your app framework is.

If that won't work for you, and you are using windows, you could always create a re parse point and a file system filter. I believe if you create a "mini filter" that it will run in user mode, but I'm not sure.

You can probably find more information about it here:

http://www.microsoft.com/whdc/driver/filterdrv/default.mspx

Scott Wisniewski
Depending on your scenario, authoring your own mini-filter to solve this problem could be overkill, particuarly given that it would need to have at least one kernel-mode component. Scott's other suggestion (implementing a stream interface) would certainly be much less work.
Reuben