1. -2
    Fast Function for Copying Files c gist.github.com
  1. 6
    char buf[src_size];

    wont this fail if source file is larger than RAM? I am guess more robust solutions (cp, rsync) dont have this issue.

    1. 3

      It should fail if it’s larger than the available stack size, I think.

      1. 1

        How about char* buf?

        1. 3

          I dont see that as helping the problem. Youd need a sliding window of a set size, say 1 GB that is emptied after that portion is copied - or you could just ignore the problem if its for personal use

          1. 1

            What do you mean a sliding window?

            1. 3
              1. 3

                Its a buffer implementation - you would need to use something like this for a robust copy solution. if you dont care about supporting larger files you can ignore this

                if you do care about supporting larger files - create buffer of say 1GB - load first 1GB of source file and copy to destination - rinse and repeat until file is copied - you might need to seek as well but I think not as I believe C read moves the cursor as well.

            2. 2

              You’ve changed the code now to just do:

                char* buf;
                fread(buf, 1, src_size, src);

              Won’t that just fail since buf is uninitialized?

              1. 0

                I tested it, it didn’t

                1. 4

                  You’re relying on undefined behavior then, which is inadvisable.

                  1. 1

                    Are you joking? Even the most cursory of checking triggers the warning:

                    $ x86_64-w64-mingw32-gcc  -Wall   copy.c
                    copy.c: In function ‘copy’:
                    copy.c:14:3: warning: ‘buf’ is used uninitialized in this function
                    1. 1


                      I think OP is learning C.

            3. 5

              It’s the first time I see a #include inside a function body!? Any reason you did that? Are you looking for feedback on the code?

              1. 2

                I made it a stand-alone function, kind of. I’m stupid. Just forget it

              2. 3

                Also the test file is only 7MB? I think most programs that can copy a file could do this under 1 second. Better test might be to get a larger file:


                1. 2

                  If this is for Linux, then I think sendfile() would be fastest, as it happens entirely within the kernel. If you can’t use sendfile() (old Linux kernel, non-Linux Unix system), then I think calling mmap() on the file being copied, then madvise(MADV_SEQUENTIAL) followed by write() would be a good thing.

                  1. 1

                    no need to write(), just mmap() the output file too and even use MADV_DONTNEED.

                    1. 0

                      Not Linux-specific

                    2. 1

                      When writing C you need to check the man page for every function that you use, it will tell you about the values returned when there is an error and you need to check for those and handle them.