bool QFile::rename(const QString &newName) { Q_D(QFile); if (d->fileName.isEmpty()) { qWarning("QFile::rename: Empty or null file name"); return false; } if (QFile(newName).exists()) { // ### Race condition. If a file is moved in after this, it /will/ be // overwritten. On Unix, the proper solution is to use hardlinks: // return ::link(old, new) && ::remove(old); d->setError(QFile::RenameError, tr("Destination file exists")); return false; } unsetError(); close(); if(error() == QFile::NoError) { if (d->engine()->rename(newName)) { unsetError(); // engine was able to handle the new name so we just reset it d->fileEngine->setFileName(newName); d->fileName = newName; return true; } if (isSequential()) { d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); return false; } QFile out(newName); if (open(QIODevice::ReadOnly)) { if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { bool error = false; char block[4096]; qint64 bytes; while ((bytes = read(block, sizeof(block))) > 0) { if (bytes != out.write(block, bytes)) { d->setError(QFile::RenameError, out.errorString()); error = true; break; } } if (bytes == -1) { d->setError(QFile::RenameError, errorString()); error = true; } if(!error) { if (!remove()) { d->setError(QFile::RenameError, tr("Cannot remove source file")); error = true; } } if (error) { out.remove(); } else { d->fileEngine->setFileName(newName); setPermissions(permissions()); unsetError(); setFileName(newName); } close(); return !error; } close(); } d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); } return false; }
bool QFile::copy(const QString &newName) { Q_D(QFile); if (d->fileName.isEmpty()) { qWarning("QFile::copy: Empty or null file name"); return false; } if (QFile(newName).exists()) { // ### Race condition. If a file is moved in after this, it /will/ be // overwritten. On Unix, the proper solution is to use hardlinks: // return ::link(old, new) && ::remove(old); See also rename(). d->setError(QFile::CopyError, tr("Destination file exists")); return false; } unsetError(); close(); if(error() == QFile::NoError) { if (d->engine()->copy(newName)) { unsetError(); return true; } else { bool error = false; if(!open(QFile::ReadOnly)) { error = true; d->setError(QFile::CopyError, tr("Cannot open %1 for input").arg(d->fileName)); } else { QString fileTemplate = QLatin1String("%1/qt_temp.XXXXXX"); #ifdef QT_NO_TEMPORARYFILE QFile out(fileTemplate.arg(QFileInfo(newName).path())); if (!out.open(QIODevice::ReadWrite)) error = true; #else QTemporaryFile out(fileTemplate.arg(QFileInfo(newName).path())); if (!out.open()) { out.setFileTemplate(fileTemplate.arg(QDir::tempPath())); if (!out.open()) error = true; } #endif if (error) { out.close(); close(); d->setError(QFile::CopyError, tr("Cannot open for output")); } else { char block[4096]; qint64 totalRead = 0; while(!atEnd()) { qint64 in = read(block, sizeof(block)); if (in <= 0) break; totalRead += in; if(in != out.write(block, in)) { close(); d->setError(QFile::CopyError, tr("Failure to write block")); error = true; break; } } if (totalRead != size()) { // Unable to read from the source. The error string is // already set from read(). error = true; } if (!error && !out.rename(newName)) { error = true; close(); d->setError(QFile::CopyError, tr("Cannot create %1 for output").arg(newName)); } #ifdef QT_NO_TEMPORARYFILE if (error) out.remove(); #else if (!error) out.setAutoRemove(false); #endif } } if(!error) { QFile::setPermissions(newName, permissions()); close(); unsetError(); return true; } } } return false; }
bool operator==(const file_status& rhs) const { return type() == rhs.type() && permissions() == rhs.permissions(); }
int do_exec(task_t *t, char *path, char **argv, char **env) { unsigned int i=0; addr_t end, eip; unsigned int argc=0, envc=0; int desc; char **backup_argv=0, **backup_env=0; /* Sanity */ if(!t) panic(PANIC_NOSYNC, "Tried to execute with empty task"); if(t == kernel_task) panic(0, "Kernel is being executed at the gallows!"); if(t != current_task) panic(0, "I don't know, was really drunk at the time"); if(t->magic != TASK_MAGIC) panic(0, "Invalid task in exec (%d)", t->pid); if(!path || !*path) return -EINVAL; /* Load the file, and make sure that it is valid and accessable */ if(EXEC_LOG == 2) printk(0, "[%d]: Checking executable file (%s)\n", t->pid, path); struct file *efil; int err_open, num; efil=d_sys_open(path, O_RDONLY, 0, &err_open, &num); if(efil) desc = num; else desc = err_open; if(desc < 0 || !efil) return -ENOENT; if(!permissions(efil->inode, MAY_EXEC)) { sys_close(desc); return -EACCES; } /* Detirmine if the file is a valid ELF */ int header_size = 0; #if CONFIG_ARCH == TYPE_ARCH_X86_64 header_size = sizeof(elf64_header_t); #elif CONFIG_ARCH == TYPE_ARCH_X86 header_size = sizeof(elf32_header_t); #endif char mem[header_size]; read_data(desc, mem, 0, header_size); int other_bitsize=0; #if CONFIG_ARCH == TYPE_ARCH_X86_64 if(is_valid_elf32_otherarch(mem, 2)) other_bitsize = 1; #endif if(!is_valid_elf(mem, 2) && !other_bitsize) { sys_close(desc); return -ENOEXEC; } if(EXEC_LOG == 2) printk(0, "[%d]: Copy data\n", t->pid); /* okay, lets back up argv and env so that we can * clear out the address space and not lose data..*/ if(__is_valid_user_ptr(SYS_EXECVE, argv, 0)) { while(__is_valid_user_ptr(SYS_EXECVE, argv[argc], 0) && *argv[argc]) argc++; backup_argv = (char **)kmalloc(sizeof(addr_t) * argc); for(i=0;i<argc;i++) { backup_argv[i] = (char *)kmalloc(strlen(argv[i]) + 1); _strcpy(backup_argv[i], argv[i]); } } if(__is_valid_user_ptr(SYS_EXECVE, env, 0)) { while(__is_valid_user_ptr(SYS_EXECVE, env[envc], 0) && *env[envc]) envc++; backup_env = (char **)kmalloc(sizeof(addr_t) * envc); for(i=0;i<envc;i++) { backup_env[i] = (char *)kmalloc(strlen(env[i]) + 1); _strcpy(backup_env[i], env[i]); } } /* and the path too! */ char *path_backup = (char *)kmalloc(strlen(path) + 1); _strcpy((char *)path_backup, path); path = path_backup; if(pd_cur_data->count > 1) printk(0, "[exec]: Not sure what to do here...\n"); /* Preexec - This is the point of no return. Here we close out unneeded * file descs, free up the page directory and clear up the resources * of the task */ if(EXEC_LOG) printk(0, "Executing (task %d, cpu %d, tty %d, cwd=%s): %s\n", t->pid, ((cpu_t *)t->cpu)->apicid, t->tty, current_task->thread->pwd->name, path); preexec(t, desc); strncpy((char *)t->command, path, 128); if(other_bitsize) { #if CONFIG_ARCH == TYPE_ARCH_X86_64 if(!process_elf_other(mem, desc, &eip, &end)) eip=0; #endif } else if(!process_elf(mem, desc, &eip, &end)) eip=0; sys_close(desc); if(!eip) { printk(5, "[exec]: Tried to execute an invalid ELF file!\n"); free_dp(backup_argv, argc); free_dp(backup_env, envc); #if DEBUG panic(0, ""); #endif exit(0); } if(EXEC_LOG == 2) printk(0, "[%d]: Updating task values\n", t->pid); /* Setup the task with the proper values (libc malloc stack) */ addr_t end_l = end; end = (end&PAGE_MASK); user_map_if_not_mapped_noclear(end); /* now we need to copy back the args and env into userspace * writeable memory...yippie. */ addr_t args_start = end + PAGE_SIZE; addr_t env_start = args_start; addr_t alen = 0; if(backup_argv) { for(i=0;i<(sizeof(addr_t) * (argc+1))/PAGE_SIZE + 2;i++) user_map_if_not_mapped_noclear(args_start + i * PAGE_SIZE); memcpy((void *)args_start, backup_argv, sizeof(addr_t) * argc); alen += sizeof(addr_t) * argc; *(addr_t *)(args_start + alen) = 0; /* set last argument value to zero */ alen += sizeof(addr_t); argv = (char **)args_start; for(i=0;i<argc;i++) { char *old = argv[i]; char *new = (char *)(args_start+alen); user_map_if_not_mapped_noclear((addr_t)new); unsigned len = strlen(old) + 4; user_map_if_not_mapped_noclear((addr_t)new + len + 1); argv[i] = new; _strcpy(new, old); kfree(old); alen += len; } kfree(backup_argv); } env_start = args_start + alen; alen = 0; if(backup_env) { for(i=0;i<(((sizeof(addr_t) * (envc+1))/PAGE_SIZE) + 2);i++) user_map_if_not_mapped_noclear(env_start + i * PAGE_SIZE); memcpy((void *)env_start, backup_env, sizeof(addr_t) * envc); alen += sizeof(addr_t) * envc; *(addr_t *)(env_start + alen) = 0; /* set last argument value to zero */ alen += sizeof(addr_t); env = (char **)env_start; for(i=0;i<envc;i++) { char *old = env[i]; char *new = (char *)(env_start+alen); user_map_if_not_mapped_noclear((addr_t)new); unsigned len = strlen(old) + 1; user_map_if_not_mapped_noclear((addr_t)new + len + 1); env[i] = new; _strcpy(new, old); kfree(old); alen += len; } kfree(backup_env); } end = (env_start + alen) & PAGE_MASK; t->env = env; t->argv = argv; kfree(path); t->heap_start = t->heap_end = end + PAGE_SIZE; if(other_bitsize) raise_task_flag(t, TF_OTHERBS); user_map_if_not_mapped_noclear(t->heap_start); /* Zero the heap and stack */ memset((void *)end_l, 0, PAGE_SIZE-(end_l%PAGE_SIZE)); memset((void *)(end+PAGE_SIZE), 0, PAGE_SIZE); memset((void *)(STACK_LOCATION - STACK_SIZE), 0, STACK_SIZE); /* Release everything */ if(EXEC_LOG == 2) printk(0, "[%d]: Performing call\n", t->pid); set_int(0); lower_task_flag(t, TF_SCHED); if(!(kernel_state_flags & KSF_HAVEEXECED)) set_ksf(KSF_HAVEEXECED); arch_specific_exec_initializer(t, argc, eip); return 0; }
void Post::Serialize(Json::Value& root) { // General Post if(!id_.empty()) root["id"] = id_; if(!entity_.empty()) root["entity"] = entity_; /* if(!published_at_.empty()) root["published_at"] = published_at_; */ if(licenses_.size() > 0) { Json::Value licenses; jsn::SerializeVector(licenses_, licenses); root["licenses"] = licenses; } if(!type_.empty()) root["type"] = type_; if(content_.size() > 0) { Json::Value content(Json::objectValue); jsn::SerializeMapIntoObject(content, content_); root["content"] = content; } if(attachments_.size() > 0) { Json::Value attachment_arr(Json::arrayValue); AttachmentMap::iterator itr = attachments_.begin(); for(;itr!= attachments_.end(); itr++) { Json::Value attachment(Json::objectValue); itr->second.Serialize(attachment); attachment_arr.append(attachment); } root["attachments"] = attachment_arr; } if(mentions_.size() > 0) { Json::Value mentions_array(Json::arrayValue); MentionsList::iterator itr = mentions_.begin(); for(;itr!= mentions_.end(); itr++) { Json::Value mention(Json::objectValue); jsn::SerializeObject(&(*itr), mention); mentions_array.append(mention); } root["mentions"] = mentions_array; } Json::Value app(Json::nullValue); tent_app_.Serialize(app); if(!app.isNull()) root["app"] = app; if(views_.size() > 0) { Json::Value views(Json::objectValue); jsn::SerializeMapIntoObject(views, views_); root["views"] = views; } Json::Value permissions(Json::objectValue); jsn::SerializeObject(&permissions_, permissions); root["permissions"] = permissions; Json::Value version(Json::objectValue); jsn::SerializeObject(&version_, version); root["version"] = version; }
//!Creates shared memory and creates and places the segment manager. //!This can throw. basic_managed_shared_memory(create_only_t create_only, const char *name, size_type size, const void *addr = 0, const permissions& perm = permissions()) : base_t() , base2_t(create_only, name, size, read_write, addr, create_open_func_t(get_this_pointer(), ipcdetail::DoCreate), perm) {}
posix_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions()) { semaphore_open(mp_sem, DoOpenOrCreate, name, initialCount, perm); }
inline named_mutex::named_mutex(open_only_t, const char *name) : m_sem(ipcdetail::DoOpen, name, 1, permissions()) {}
void prim_addprop(PRIM_PROTOTYPE) { CHECKOP(4); oper1 = POP(); oper2 = POP(); oper3 = POP(); oper4 = POP(); if (oper1->type != PROG_INTEGER) abort_interp("Non-integer argument (4)"); if (oper2->type != PROG_STRING) abort_interp("Non-string argument (3)"); if (oper3->type != PROG_STRING) abort_interp("Non-string argument (2)"); if (!oper3->data.string) abort_interp("Empty string argument (2)"); if (!valid_object(oper4)) abort_interp("Non-object argument (1)"); CHECKREMOTE(oper4->data.objref); if ((mlev < 2) && (!permissions(ProgUID, oper4->data.objref))) abort_interp("Permission denied."); if (!prop_write_perms(ProgUID, oper4->data.objref, oper3->data.string->data, mlev)) abort_interp("Permission denied."); { const char *temp; char *tmpe; char tname[BUFFER_LEN]; int len = oper3->data.string->length; temp = (oper2->data.string ? oper2->data.string->data : 0); tmpe = oper3->data.string->data; while (*tmpe && *tmpe != '\r') tmpe++; if (*tmpe) abort_interp("CRs not allowed in propname"); strcpyn(tname, sizeof(tname), oper3->data.string->data); while (len-- > 0 && tname[len] == PROPDIR_DELIMITER) { tname[len] = '\0'; } /* if ((temp) || (oper1->data.number)) */ { add_property(oper4->data.objref, tname, temp, oper1->data.number); #ifdef LOG_PROPS log2file("props.log", "#%d (%d) ADDPROP: o=%d n=\"%s\" s=\"%s\" v=%d", program, pc->line, oper4->data.objref, tname, temp, oper1->data.number); #endif ts_modifyobject(oper4->data.objref); } } CLEAR(oper1); CLEAR(oper2); CLEAR(oper3); CLEAR(oper4); }
void prim_parseprop(PRIM_PROTOTYPE) { const char *temp; char *ptr; struct inst *oper1 = NULL; /* prevents re-entrancy issues! */ struct inst *oper2 = NULL; /* prevents re-entrancy issues! */ struct inst *oper3 = NULL; /* prevents re-entrancy issues! */ struct inst *oper4 = NULL; /* prevents re-entrancy issues! */ char buf[BUFFER_LEN]; char type[BUFFER_LEN]; CHECKOP(4); oper4 = POP(); /* int */ oper2 = POP(); /* arg str */ oper1 = POP(); /* propname str */ oper3 = POP(); /* object dbref */ if (mlev < 3) abort_interp("Mucker level 3 or greater required."); if (oper3->type != PROG_OBJECT) abort_interp("Non-object argument. (1)"); if (!valid_object(oper3)) abort_interp("Invalid object. (1)"); if (oper2->type != PROG_STRING) abort_interp("String expected. (3)"); if (oper1->type != PROG_STRING) abort_interp("String expected. (2)"); if (!oper1->data.string) abort_interp("Empty string argument (2)"); if (oper4->type != PROG_INTEGER) abort_interp("Integer expected. (4)"); if (oper4->data.number < 0 || oper4->data.number > 1) abort_interp("Integer of 0 or 1 expected. (4)"); CHECKREMOTE(oper3->data.objref); { int len = oper1->data.string->length; if (!prop_read_perms(ProgUID, oper3->data.objref, oper1->data.string->data, mlev)) abort_interp("Permission denied."); if (mlev < 3 && !permissions(player, oper3->data.objref) && prop_write_perms(ProgUID, oper3->data.objref, oper1->data.string->data, mlev)) abort_interp("Permission denied."); strcpyn(type, sizeof(type), oper1->data.string->data); while (len-- > 0 && type[len] == PROPDIR_DELIMITER) { type[len] = '\0'; } temp = get_property_class(oper3->data.objref, type); #ifdef LOG_PROPS log2file("props.log", "#%d (%d) GETPROPSTR: o=%d n=\"%s\" s=\"%s\"", program, pc->line, oper3->data.objref, type, temp); #endif } ptr = (oper2->data.string) ? oper2->data.string->data : ""; if (temp) { result = 0; if (oper4->data.number) result |= MPI_ISPRIVATE; if (Prop_Blessed(oper3->data.objref, type)) result |= MPI_ISBLESSED; ptr = do_parse_mesg(fr->descr, player, oper3->data.objref, temp, ptr, buf, sizeof(buf), result); CLEAR(oper1); CLEAR(oper2); CLEAR(oper3); CLEAR(oper4); PushString(ptr); } else { CLEAR(oper1); CLEAR(oper2); CLEAR(oper3); CLEAR(oper4); PushNullStr; } }
void prim_setprop(PRIM_PROTOTYPE) { CHECKOP(3); oper1 = POP(); oper2 = POP(); oper3 = POP(); if ((oper1->type != PROG_STRING) && (oper1->type != PROG_INTEGER) && (oper1->type != PROG_LOCK) && (oper1->type != PROG_OBJECT) && (oper1->type != PROG_FLOAT)) abort_interp("Invalid argument type (3)"); if (oper2->type != PROG_STRING) abort_interp("Non-string argument (2)"); if (!oper2->data.string) abort_interp("Empty string argument (2)"); if (!valid_object(oper3)) abort_interp("Non-object argument (1)"); CHECKREMOTE(oper3->data.objref); if ((mlev < 2) && (!permissions(ProgUID, oper3->data.objref))) abort_interp("Permission denied."); if (!prop_write_perms(ProgUID, oper3->data.objref, oper2->data.string->data, mlev)) abort_interp("Permission denied."); { char *tmpe; char tname[BUFFER_LEN]; PData propdat; int len = oper2->data.string->length; tmpe = oper2->data.string->data; while (*tmpe && *tmpe != '\r' && *tmpe != ':') tmpe++; if (*tmpe) abort_interp("Illegal propname"); strcpyn(tname, sizeof(tname), oper2->data.string->data); while (len-- > 0 && tname[len] == PROPDIR_DELIMITER) { tname[len] = '\0'; } switch (oper1->type) { case PROG_STRING: propdat.flags = PROP_STRTYP; propdat.data.str = oper1->data.string ? oper1->data.string->data : 0; break; case PROG_INTEGER: propdat.flags = PROP_INTTYP; propdat.data.val = oper1->data.number; break; case PROG_FLOAT: propdat.flags = PROP_FLTTYP; propdat.data.fval = oper1->data.fnumber; break; case PROG_OBJECT: propdat.flags = PROP_REFTYP; propdat.data.ref = oper1->data.objref; break; case PROG_LOCK: propdat.flags = PROP_LOKTYP; propdat.data.lok = copy_bool(oper1->data.lock); break; } set_property(oper3->data.objref, tname, &propdat); #ifdef LOG_PROPS log2file("props.log", "#%d (%d) SETPROP: o=%d n=\"%s\"", program, pc->line, oper3->data.objref, tname); #endif ts_modifyobject(oper3->data.objref); } CLEAR(oper1); CLEAR(oper2); CLEAR(oper3); }
//!Tries to open a shared memory object with name "name", with the access mode "mode". //!If the file does not previously exist, it throws an error. shared_memory_object(open_only_t, const char *name, mode_t mode) { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); }
bool QFile::rename(const QString &newName) { Q_D(QFile); if (d->fileName.isEmpty()) { qWarning("QFile::rename: Empty or null file name"); return false; } if (d->fileName == newName) { d->setError(QFile::RenameError, tr("Destination file is the same file.")); return false; } if (!exists()) { d->setError(QFile::RenameError, tr("Source file does not exist.")); return false; } // If the file exists and it is a case-changing rename ("foo" -> "Foo"), // compare Ids to make sure it really is a different file. if (QFile::exists(newName)) { if (d->fileName.compare(newName, Qt::CaseInsensitive) || QFileSystemEngine::id(QFileSystemEntry(d->fileName)) != QFileSystemEngine::id(QFileSystemEntry(newName))) { // ### Race condition. If a file is moved in after this, it /will/ be // overwritten. On Unix, the proper solution is to use hardlinks: // return ::link(old, new) && ::remove(old); d->setError(QFile::RenameError, tr("Destination file exists")); return false; } #ifndef QT_NO_TEMPORARYFILE // This #ifndef disables the workaround it encloses. Therefore, this configuration is not recommended. #ifdef Q_OS_LINUX // rename() on Linux simply does nothing when renaming "foo" to "Foo" on a case-insensitive // FS, such as FAT32. Move the file away and rename in 2 steps to work around. QTemporaryFile tempFile(d->fileName + QStringLiteral(".XXXXXX")); tempFile.setAutoRemove(false); if (!tempFile.open(QIODevice::ReadWrite)) { d->setError(QFile::RenameError, tempFile.errorString()); return false; } tempFile.close(); if (!d->engine()->rename(tempFile.fileName())) { d->setError(QFile::RenameError, tr("Error while renaming.")); return false; } if (tempFile.rename(newName)) { d->fileEngine->setFileName(newName); d->fileName = newName; return true; } d->setError(QFile::RenameError, tempFile.errorString()); // We need to restore the original file. if (!tempFile.rename(d->fileName)) { d->setError(QFile::RenameError, errorString() + QLatin1Char('\n') + tr("Unable to restore from %1: %2"). arg(QDir::toNativeSeparators(tempFile.fileName()), tempFile.errorString())); } return false; #endif // Q_OS_LINUX #endif // QT_NO_TEMPORARYFILE } unsetError(); close(); if(error() == QFile::NoError) { if (d->engine()->rename(newName)) { unsetError(); // engine was able to handle the new name so we just reset it d->fileEngine->setFileName(newName); d->fileName = newName; return true; } if (isSequential()) { d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); return false; } QFile out(newName); if (open(QIODevice::ReadOnly)) { if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { bool error = false; char block[4096]; qint64 bytes; while ((bytes = read(block, sizeof(block))) > 0) { if (bytes != out.write(block, bytes)) { d->setError(QFile::RenameError, out.errorString()); error = true; break; } } if (bytes == -1) { d->setError(QFile::RenameError, errorString()); error = true; } if(!error) { if (!remove()) { d->setError(QFile::RenameError, tr("Cannot remove source file")); error = true; } } if (error) { out.remove(); } else { d->fileEngine->setFileName(newName); setPermissions(permissions()); unsetError(); setFileName(newName); } close(); return !error; } close(); } d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); } return false; }
//!Creates a global condition with a name. //!If the condition can't be created throws interprocess_exception named_condition_any(create_only_t, const char *name, const permissions &perm = permissions()) : m_cond(create_only_t(), name, perm) {}
windows_named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm = permissions()) : windows_named_mutex(open_or_create_t(), name, perm) {}
//!Opens or creates a global condition with a name. //!If the condition is created, this call is equivalent to //!named_condition_any(create_only_t, ... ) //!If the condition is already created, this call is equivalent //!named_condition_any(open_only_t, ... ) //!Does not throw named_condition_any(open_or_create_t, const char *name, const permissions &perm = permissions()) : m_cond(open_or_create_t(), name, perm) {}
inline windows_named_semaphore::windows_named_semaphore(open_only_t, const char *name) : m_sem_wrapper() { named_sem_callbacks callbacks(m_sem_wrapper, 0); m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks); }