/* {FILE}.exists */ EIF_BOOLEAN F785_7705 (EIF_REFERENCE Current) { GTCX EIF_POINTER tp1; EIF_BOOLEAN Result = ((EIF_BOOLEAN) 0); RTLD; RTLI(1); RTLR(0,Current); RTGC; if (F785_7729(Current)) { tp1 = *(EIF_POINTER *)(RTCV(F785_7825(Current))+ _PTROFF_0_1_0_1_0_0_); Result = (EIF_BOOLEAN) EIF_TEST(eif_file_exists((EIF_FILENAME) tp1)); } else { RTLE; return (EIF_BOOLEAN) (EIF_BOOLEAN) 1; } RTLE; return Result; }
/* * Rename file `from' into `to'. */ void eif_file_rename(EIF_FILENAME from, EIF_FILENAME to) { for (;;) { #ifdef EIF_WINDOWS if (eif_file_exists(to)) { /* To have the same behavior as Unix, we need to remove the destination file if it exists. * Of course we can do this only if `from' and `to' do not represent the same file. * To check this, we use `CreateFile' to open both file, and then using the information * returned by `GetFileInformationByHandle' we can check whether or not they are indeed * the same. */ BY_HANDLE_FILE_INFORMATION l_to_info, l_from_info; HANDLE l_from_file = CreateFileW (from, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE l_to_file = CreateFileW (to, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if ((l_from_file == INVALID_HANDLE_VALUE) || (l_to_file == INVALID_HANDLE_VALUE)) { /* We do not need the handles anymore, simply close them. Since Microsoft * API accepts INVALID_HANDLE_VALUE we don't check the validity of arguments. */ CloseHandle(l_from_file); CloseHandle(l_to_file); /* For some reasons we cannot open the file. This should not happen, maybe the OS has * removed `from' or `to'. In that case, we simply try to remove destination as we were * doing in former revision of `file_rename'. */ _wremove (to); } else { BOOL success = GetFileInformationByHandle (l_from_file, &l_from_info); if (success) { success = GetFileInformationByHandle (l_to_file, &l_to_info); /* We do not need the handles anymore, simply close them. */ CloseHandle(l_from_file); CloseHandle(l_to_file); if (success) { /* Check that `from' and `to' do not represent the same file. */ if ((l_from_info.dwVolumeSerialNumber != l_to_info.dwVolumeSerialNumber) || (l_from_info.nFileIndexLow != l_to_info.nFileIndexLow) || (l_from_info.nFileIndexHigh != l_to_info.nFileIndexHigh)) { _wremove (to); } else { /* Files are identical, nothing to be done apart from */ break; } } else { /* An error occurred while retrieving the information about `from' and `to'. Like * for the case where `l_from_file' and `l_to_file' are invalid, we try to remove * the file. */ _wremove (to); } } else { /* We do not need the handles anymore, simply close them. */ CloseHandle(l_from_file); CloseHandle(l_to_file); /* An error occurred while retrieving the information about `from' and `to'. Like * for the case where `l_from_file' and `l_to_file' are invalid, we try to remove * the file. */ _wremove (to); } } } #endif errno = 0; if (rt_rename(from, to) == -1) { esys(); /* Raise exception */ } break; } }
/* * Check whether the file `path' may be created: we need write permissions * in the parent directory and there must not be any file bearing that name * with no write permissions... */ EIF_BOOLEAN eif_file_creatable(EIF_FILENAME path, EIF_INTEGER nbytes) { rt_stat_buf buf; /* Buffer to get parent directory statistics */ EIF_FILENAME temp = NULL; EIF_FILENAME ptr; size_t l_extra_bytes; /* Allocate one extra character, just to be sure since we might be appending characters. */ #ifdef EIF_WINDOWS l_extra_bytes = sizeof(wchar_t); #else l_extra_bytes = sizeof(char); #endif temp = (EIF_FILENAME) malloc (nbytes + l_extra_bytes); if (!temp) { enomem(); } else { /* Search the directory separator. */ #ifdef EIF_WINDOWS memcpy (temp, path, nbytes); ptr = wcsrchr (temp, '\\'); if (!ptr) { /* On Windows we can have a forward slash as separator. */ ptr = wcsrchr (temp, '/'); } #else strcpy (temp, path); ptr = strrchr (temp, '/'); #endif if (ptr) { *ptr = '\0'; #ifdef EIF_WINDOWS if ((ptr == temp) || (*(ptr -1) == ':')) { /* path is of the form a:\bbb or \bbb, parent is a:\ or \ */ wcscat (ptr, L"\\"); } #endif } else { #ifdef EIF_WINDOWS wcsncpy (temp, L".", 2); #else strcpy (temp, "."); #endif } /* Does the parent exist? */ if (!eif_file_exists(temp)) { free (temp); return (EIF_BOOLEAN) '\0'; } rt_file_stat(temp, &buf); free (temp); if (S_ISDIR(buf.st_mode)) { /* Is parent a directory? */ if (eif_file_eaccess(&buf, 1)) { /* Check for write permissions */ /* Check if a non writable file `path' exists */ if (eif_file_exists(path)) { rt_file_stat(path, &buf); if (S_ISDIR(buf.st_mode)) { /* File exists and it is already a directory, thus we cannot create a file. */ return (EIF_BOOLEAN) '\0'; } else { return (eif_file_eaccess(&buf, 1)); /* Check for write permissions to re create it */ } } return (EIF_BOOLEAN) '\01'; } } } return (EIF_BOOLEAN) '\0'; }