tpoechtrager / osxcross

Mac OS X cross toolchain for Linux, FreeBSD, OpenBSD and Android (Termux)
GNU General Public License v2.0
2.91k stars 329 forks source link

ranlib can't rename file => fixed #365

Open philippe44 opened 2 years ago

philippe44 commented 2 years ago

I've seen that issue since a started to use osxcross: on non-local file systems, libtool (ranlib) fails by not being able to rename the temporary library into the original name (see libtool.c, #3173). It forced me to always build on local fs. I decided to give it a better look and I think I've found the issue:

In cctools/libstuff/ofile.c, the ofile_unmap function does not do a munmap to free the memory-mapping, so the file is still considered busy on the fs and cannot be renamed. It works fine on local fs as the system can honor the rename (later) but not for other type of fs.

The fix is simply to add the munmap as follows:

truct ofile *ofile)
{
    kern_return_t r;

        if(ofile->file_addr != NULL){
            if((r = vm_deallocate(mach_task_self(),
                                 (vm_address_t)ofile->file_addr,
                                 (vm_size_t)ofile->file_size)) != KERN_SUCCESS){
                my_mach_error(r, "Can't vm_deallocate mapped memory for file: "
                              "%s", ofile->file_name);
            }
        }
       // release mmap as well
        munmap(ofile->file_addr, ofile->file_size);
        if(ofile->file_name != NULL)
            free(ofile->file_name);
        if(ofile->arch_flag.name != NULL)
            free(ofile->arch_flag.name);
        memset(ofile, '\0', sizeof(struct ofile));
}

I'm not 100% sure how to do a PR as I don't understand for now where the file comes from exactly

philippe44 commented 2 years ago

Oops .. found it https://github.com/tpoechtrager/cctools-port/pull/125