/* * Append a copy of `other' to `f'. */ void eif_file_append(FILE *f, FILE *other, EIF_INTEGER l) { size_t amount; char buffer[512]; int bufsize = 512; errno = 0; if (fseek(other, 0, FS_START) != 0) { esys(); } if (fseek(f, 0, FS_END) != 0) { esys(); } while (l > 0) { amount = l; if (l < bufsize) amount = l; else amount = bufsize; if (amount != fread(buffer, sizeof(char), amount, other)) { eise_io("FILE: unable to read appended file."); break; } l -= (EIF_INTEGER) amount; if (amount != fwrite(buffer, sizeof(char), amount, f)) { eise_io("FILE: unable to write appended file."); break; } } }
/* * Change permission mode on file `path'. */ void eif_file_chmod(EIF_FILENAME path, int mode) { errno = 0; #ifdef EIF_WINDOWS if (_wchmod(path, mode) == -1) { #else if (chmod(path, mode) == -1) { #endif esys(); } } /* * Change the owner of the file to `uid'. */ void eif_file_chown(EIF_FILENAME name, int uid) { #ifdef HAS_CHOWN int gid; rt_stat_buf buf; rt_file_stat(name, &buf); gid = buf.st_gid; errno = 0; if (chown(name, uid, gid) == -1) { esys(); } #endif }
/* * Size of file `fp'. */ EIF_INTEGER eif_file_size(FILE *fp) { rt_stat_buf buf; errno = 0; if (fflush (fp) != 0) { esys(); } if (rt_fstat (fileno(fp), &buf) == -1) esys(); /* An error occurred: raise exception */ /* FIXME: This code should be upgraded to use 64-bit */ return (EIF_INTEGER) buf.st_size; }
/* * Flush data held in stdio buffer. */ void eif_file_flush(FILE *fp) { errno = 0; if (fflush(fp) != 0) { esys(); /* Flush failed, raise exception */ } #ifdef EIF_WINDOWS /* On Windows, it does not write directly to disk, so we have to force it. See KB66052: * http://support.microsoft.com/kb/66052 * We ignore bad file descriptor case, as it is most likely when calling it on one of the standard * input/outputs. */ if ((0 != _commit(fileno(fp))) && (errno != EBADF)) { esys(); } #endif }
void AddVariableAction::act() { if (_current_action == "add_variable") { // Get necessary data for creating a variable std::string var_name = getShortName(); std::set<SubdomainID> blocks = getSubdomainIDs(); Real scale_factor = isParamValid("scaling") ? getParam<Real>("scaling") : 1; // Scalar variable if (_scalar_var) _problem->addScalarVariable(var_name, _fe_type.order, scale_factor); // Block restricted variable else if (blocks.empty()) _problem->addVariable(var_name, _fe_type, scale_factor); // Non-block restricted variable else _problem->addVariable(var_name, _fe_type, scale_factor, &blocks); if (getParam<bool>("eigen")) { EigenSystem & esys(static_cast<EigenSystem &>(_problem->getNonlinearSystem())); esys.markEigenVariable(var_name); } } // Set the initial condition if (_current_action == "add_ic") setInitialCondition(); }
/* * Modify the modification and/or the access time stored in the file's * inode. The 'how' parameter tells which attributes should be set. */ void eif_file_utime(EIF_FILENAME name, time_t stamp, int how) { struct utimbuf tp; /* Time array */ rt_stat_buf buf; /* File statistics */ int status; /* System call status */ if (how < 2) { /* Need to fetch time from inode */ rt_file_stat(name, &buf); switch (how) { case 0: /* Change only access time */ tp.actime = stamp; tp.modtime = buf.st_mtime; break; case 1: /* Change only modification time */ tp.actime = buf.st_atime; tp.modtime = stamp; break; } } else tp.actime = tp.modtime = stamp; /* Change both access and modification times */ errno = 0; if (rt_utime(name, &tp) == -1) { /* Raise exception */ esys(); } }
static void rt_file_stat (EIF_FILENAME path, rt_stat_buf *buf) { /* To preserve compatibility we always follow symbolic links and raise an exception upon failure. */ if (eif_file_stat(path, buf, 1) == -1) { esys(); } }
/* * Go to absolute position `pos' counted from current position. */ void eif_file_move(FILE *f, EIF_INTEGER pos) { errno = 0; if (fseek(f, pos, FS_CUR) != 0) { esys(); } clearerr(f); }
/* * Go to absolute position `pos' counted from end. */ void eif_file_recede(FILE *f, EIF_INTEGER pos) { errno = 0; if (fseek(f, -pos, FS_END) != 0) { esys(); } clearerr(f); }
/* * Go to absolute position `pos' counted from start. */ void eif_file_go(FILE *f, EIF_INTEGER pos) { errno = 0; if (fseek(f, pos, FS_START) != 0) { esys(); } clearerr(f); }
rt_public char *partial_retrieve(EIF_INTEGER f_desc, long position, long nb_obj) /* Return `nb_obj' retrieved in file `file_ptr' read at `position'. */ { RT_GET_CONTEXT EIF_GET_CONTEXT char *result; file_descriptor = (int) f_desc; if (lseek (f_desc, position, SEEK_SET) == -1) esys (); current_position = 0; end_of_buffer = 0; rt_kind = BASIC_STORE; rt_kind_version = BASIC_STORE_6_6; rt_kind_properties = 0; result = rt_nmake(nb_obj); /* Retrieve `nb_obj' objects */ ht_free(rt_table); /* Free hash table descriptor */ #ifdef ISE_GC if (nb_recorded) { eif_ostack_npop(&hec_stack, nb_recorded); /* Pop hector records */ nb_recorded = 0; } #endif return result; }
rt_public char *retrieve_all(EIF_INTEGER f_desc, long position) { RT_GET_CONTEXT EIF_GET_CONTEXT /* Return object graph retrieved in file `file_ptr' read at * position. */ char *result; file_descriptor = (int)f_desc; if (lseek(file_descriptor, position, SEEK_SET) == -1) esys(); /* bail out */ current_position = 0; end_of_buffer = 0; rt_kind = BASIC_STORE; rt_kind_version = BASIC_STORE_6_6; rt_kind_properties = 0; result = rt_make(); ht_free(rt_table); /* Free hash table descriptor */ #ifdef ISE_GC if (nb_recorded) { eif_ostack_npop(&hec_stack, nb_recorded); /* Pop hector records */ nb_recorded = 0; } #endif return result; }
/* * Link file `from' into `to'. */ void eif_file_link(EIF_FILENAME from, EIF_FILENAME to) { #ifdef HAS_LINK errno = 0; if (link(from, to) == -1) { esys(); /* Raise exception */ } #endif }
/* * Open file `fd' with the corresponding type `how'. */ EIF_POINTER eif_file_dopen(int fd, int how) { FILE *fp; errno = 0; #ifdef EIF_WINDOWS fp = (FILE *)rt_file_fdopen(fd, rt_file_open_mode(how, (how < 10 ? 't' : 'b'))); #else fp = (FILE *)rt_file_fdopen(fd, rt_file_open_mode(how, '\0')); #endif if (fp == (FILE *)0) { esys(); /* Open failed, raise exception */ } return (EIF_POINTER)fp; }
/* * Reopen file `name' with the corresponding type `how' and substitute that * to the old stream described by `old'. This is useful to redirect 'stdout' * to another place, for instance. */ EIF_POINTER eif_file_reopen(EIF_FILENAME name, int how, FILE *old) { FILE *fp; errno = 0; #ifdef EIF_WINDOWS fp = (FILE *)rt_file_freopen(name, rt_file_open_mode(how, (how < 10 ? 't' : 'b')), old); #else fp = (FILE *)rt_file_freopen(name, rt_file_open_mode(how, '\0'), old); #endif if (fp == (FILE *)0) { esys(); /* Open failed, raise exception */ } return (EIF_POINTER)fp; }
MDNSPacket* mdns_alloc_packet(Error* err) { MDNSPacket* packet; packet = malloc(sizeof(*packet)); if(packet == NULL) { esys(err, "Failed to allocate MDNS packet"); return NULL; } return packet; }
/* * Change the group of the file to `gid'. */ void eif_file_chgrp(EIF_FILENAME name, int gid) { #ifdef HAS_CHOWN int uid; rt_stat_buf buf; rt_file_stat(name, &buf); uid = buf.st_uid; errno = 0; if (chown(name, uid, gid) == -1) { esys(); } #endif }
/* * Open file `name' with the corresponding type `how'. */ EIF_POINTER eif_file_binary_open(EIF_FILENAME name, int how) { FILE *fp; errno = 0; #ifdef EIF_WINDOWS fp = (FILE *)rt_file_fopen(name, rt_file_open_mode(how,'b')); #else fp = (FILE *)rt_file_fopen(name, rt_file_open_mode(how,'\0')); #endif if (fp == (FILE *)0) { esys(); /* Open failed, raise exception */ } return (EIF_POINTER)fp; }
/* * Delete file or directory `name'. */ void eif_file_unlink(EIF_FILENAME name) { rt_stat_buf buf; /* File statistics */ int status; /* Status from system call */ status = eif_file_stat(name, &buf, 0); /* Side effect: ensure file exists */ if (status == -1) { /* An error occurred */ esys(); } else { for (;;) { errno = 0; /* Reset error condition */ if (S_ISDIR(buf.st_mode)) /* Directory */ status = rt_rmdir(name); /* Remove only if empty */ else status = rt_unlink(name); /* Not a directory */ if (status == -1) { /* An error occurred */ if (errno == EINTR) /* Interrupted by signal */ continue; /* Re-issue system call */ else esys(); /* Raise exception */ } break; } } }
void AddVariableAction::addVariable(std::string & var_name) { std::set<SubdomainID> blocks = getSubdomainIDs(); Real scale_factor = isParamValid("scaling") ? getParam<Real>("scaling") : 1; // Scalar variable if (_scalar_var) _problem->addScalarVariable(var_name, _fe_type.order, scale_factor); // Block restricted variable else if (blocks.empty()) _problem->addVariable(var_name, _fe_type, scale_factor); // Non-block restricted variable else _problem->addVariable(var_name, _fe_type, scale_factor, &blocks); if (getParam<bool>("eigen")) { EigenSystem & esys(static_cast<EigenSystem &>(_problem->getNonlinearSystem())); esys.markEigenVariable(var_name); } }
/* * Change permissions of file `name', using an interface like chmod(1). * The flag is true if permissions are to be added, 0 to remove them. */ void eif_file_perm(EIF_FILENAME name, char *who, char *what, int flag) { int fmode; /* File mode to be altered */ rt_stat_buf buf; /* File statistics */ /* We need to follow links since `chmod' does follow them to change the permissions. */ if (eif_file_stat(name, &buf, 1)) { esys(); } else { fmode = (int) buf.st_mode; /* Fetch current file mode */ switch (*who) { case 'u': while (*what) switch (*what++) { #ifdef EIF_WINDOWS case 's': break; case 'r': break; case 'w': break; case 'x': break; #else case 's': if (flag) fmode |= S_ISUID; else fmode &= ~S_ISUID; break; case 'r': if (flag) fmode |= S_IRUSR; else fmode &= ~S_IRUSR; break; case 'w': if (flag) fmode |= S_IWUSR; else fmode &= ~S_IWUSR; break; case 'x': if (flag) fmode |= S_IXUSR; else fmode &= ~S_IXUSR; break; #endif default: eraise("invalid user permission", EN_EXT); } break; case 'g': while (*what) switch (*what++) { case 's': #ifdef S_ISGID if (flag) fmode |= S_ISGID; else fmode &= ~S_ISGID; #endif break; case 'r': #ifdef S_IRGRP if (flag) fmode |= S_IRGRP; else fmode &= ~S_IRGRP; #endif break; case 'w': #ifdef S_IWGRP if (flag) fmode |= S_IWGRP; else fmode &= ~S_IWGRP; #endif break; case 'x': #ifdef S_IXGRP if (flag) fmode |= S_IXGRP; else fmode &= ~S_IXGRP; #endif break; default: eraise("invalid group permission", EN_EXT); } break; case 'o': while (*what) switch (*what++) { case 't': #ifdef S_ISVTX if (flag) fmode |= S_ISVTX; else fmode &= ~S_ISVTX; #endif break; case 'r': #ifdef S_IROTH if (flag) fmode |= S_IROTH; else fmode &= ~S_IROTH; #endif break; case 'w': #ifdef S_IWOTH if (flag) fmode |= S_IWOTH; else fmode &= ~S_IWOTH; #endif break; case 'x': #ifdef S_IXOTH if (flag) fmode |= S_IXOTH; else fmode &= ~S_IXOTH; #endif break; default: eraise("invalid other permission", EN_EXT); } break; default: eraise("invalid permission target", EN_EXT); } eif_file_chmod(name, fmode); } }
/* * Create directory `dirname'. */ void eif_file_mkdir(EIF_FILENAME dirname) { errno = 0; if (rt_mkdir(dirname, 0777) == -1) { esys(); /* Raise exception */ } }
/* * 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; } }
/* * Close the file. */ void eif_file_close(FILE *fp) { errno = 0; if (fclose(fp) != 0) { esys(); /* Close failed, raise exception */ } }