Esempio n. 1
0
void NamedLogWriter::append(const boost::filesystem::path &path)
{
    try {
        open_(path, true);
    } catch (std::exception &) {
        BOOST_THROW_EXCEPTION(NamedLogWriterAppendError() <<
                              enable_nested_current());
    }
}
Esempio n. 2
0
void use_device(struct reader *reader, struct writer *writer, struct device *d)
    /*@
    requires
        reader(reader) &*& writer(writer) &*& lockset(currentThread, nil) &*&
        [?f1]d->ops |-> ?ops &*& [f1]file_ops(ops, ?device, ?file_) &*&
        [?f2]device();
    @*/
    /*@
    ensures
        reader(reader) &*& writer(writer) &*& lockset(currentThread, nil) &*&
        [f1]d->ops |-> ops &*& [f1]file_ops(ops, device, file_) &*&
        [f2]device();
    @*/
{
    //@ open file_ops(ops, _, _);
    device_open *open_ = d->ops->open_;
    device_close *close_ = d->ops->close_;
    void *file = open_();
    bool exitMenu = false;
    while (!exitMenu)
        /*@
        invariant
            reader(reader) &*& writer(writer) &*& file_(f2, file) &*& lockset(currentThread, nil) &*&
            [f1]d->ops |-> ops &*&
            [f1]ops->read |-> ?read &*& [f1]ops->write |-> ?write &*&
            [_]is_device_read(read, file_) &*&
            [_]is_device_write(write, file_);
        @*/
    {
        int choice = 0;
        writer_write_string(writer,
            "Device Menu:\r\n"
            "1. Read Value\r\n"
            "2. Write Value\r\n"
            "0. Exit\r\n");
        choice = reader_read_nonnegative_integer(reader);
        if (choice == 1) {
            device_read *read_ = d->ops->read;
            int value = read_(file);
            writer_write_string(writer, "Value read: ");
            writer_write_integer_as_decimal(writer, value);
            writer_write_string(writer, "\r\n");
        } else if (choice == 2) {
            int value = 0;
            device_write *write_ = d->ops->write;
            writer_write_string(writer, "Enter value:\r\n");
            value = reader_read_nonnegative_integer(reader);
            write_(file, value);
            writer_write_string(writer, "The value has been written\r\n");
        } else {
            exitMenu = true;
        }
    }
    close_(file);
    //@ close [f1]file_ops(ops, device, file_);
}
Esempio n. 3
0
File: unrar.cpp Progetto: kode54/Cog
	static unrar_err_t reopen( unrar_t* p )
	{
		// Save and restore archive reader
		unrar_read_func read = p->Arc.user_read;
		void* user_data      = p->Arc.user_read_data;
		
		void (*close_file)( void* ) = p->close_file;
		p->close_file = NULL;
		
		p->~unrar_t();
		new (p) unrar_t;
		
		p->close_file = close_file;
		
		return open_( p, read, user_data );
	}
Esempio n. 4
0
/**
 * qdb->ping(): Checks whether the connection to the server is working.
 *
 * @param db        a pointer of qdb_t object
 *
 * @return true if connection is alive, false if connection is not available
 *         and failed to reconnect
 *
 * @note
 * If the connection has gone down, an attempt to reconnect.
 */
static bool ping(qdb_t *db)
{
    if (db == NULL) return false;

#ifdef _Q_ENABLE_MYSQL
    if (db->connected == true && mysql_ping(db->mysql) == 0) {
        return true;
    } else { // ping test failed
        if (open_(db) == true) { // try re-connect
            DEBUG("Connection recovered.");
            return true;
        }
    }

    return false;
#else
    return false;
#endif
}
Esempio n. 5
0
File: unrar.cpp Progetto: kode54/Cog
unrar_err_t unrar_open_custom( unrar_t** impl_out, unrar_read_func read, void* user_data )
{
	*impl_out = NULL;
	
	unrar_ptr ptr;
	ptr.p = new unrar_t;
	if ( !ptr.p )
		return unrar_err_memory;
	
	RETURN_ERR( NONLOCAL_ERROR( ptr.p ) );
	RETURN_ERR( open_( ptr.p, read, user_data ) );
	RETURN_ERR( next_( ptr.p, false ) );
	
	*impl_out = ptr.p;
	ptr.p = NULL;
		
	//delete ptr.p; // done automatically at end of function
	
	return unrar_ok;
}
Esempio n. 6
0
/// Write a ldf data buffer to disk.
bool DATA_buffer::Write(std::ofstream *file_, char *data_, int nWords_, int &buffs_written){
	if(!file_ || !file_->is_open() || !file_->good() || !data_ || nWords_ == 0){ 
		if(debug_mode){ std::cout << "debug: !file_ || !file_->is_open() || !data_ || nWords_ == 0\n"; }	
		return false; 
	}

		// Write a DATA header if needed
		buffs_written = 0;
		if(current_buff_pos < 2){ open_(file_); }
		else if(current_buff_pos > ACTUAL_BUFF_SIZE){
			if(debug_mode){ std::cout << "debug: previous buffer overfilled by " << current_buff_pos - ACTUAL_BUFF_SIZE << " words!!!\n"; }
			Close(file_);
			open_(file_);
			buffs_written++;
		}
		else if(buff_words_remaining < SMALLEST_CHUNK_SIZE){ // Can't fit enough words in this buffer. Start a fresh one
			Close(file_);
			open_(file_);
			buffs_written++;
		}
	
		// The entire spill needs to be chopped up to fit into buffers
		// Calculate the number of data chunks we will need
		int words_written = 0;
		int this_chunk_sizeW, this_chunk_sizeB;
		int total_num_chunks, current_chunk_num;
		if((nWords_ + 10) >= good_words_remaining){ // Spill needs at least one more buffer	
			total_num_chunks = 2 + (nWords_ - good_words_remaining + 3) / OPTIMAL_CHUNK_SIZE;
			if((nWords_ - good_words_remaining + 3) % OPTIMAL_CHUNK_SIZE != 0){ 
				total_num_chunks++; // Account for the buffer fragment
			}
		}
		else{ // Entire spill (plus footer) will fit in the current buffer
			if(debug_mode){ std::cout << "debug: writing spill of nWords_=" << nWords_ << " + 10 words\n"; }
			
			// Write the spill chunk header
			this_chunk_sizeW = nWords_ + 3;
			this_chunk_sizeB = 4 * this_chunk_sizeW;
			total_num_chunks = 2; 
			current_chunk_num = 0;
			file_->write((char*)&this_chunk_sizeB, 4);
			file_->write((char*)&total_num_chunks, 4);
			file_->write((char*)&current_chunk_num, 4);
		
			// Write the spill
			file_->write((char*)data_, (this_chunk_sizeB - 12));
		
			// Write the end of spill buffer (5 words + 2 end of buffer words)
			current_chunk_num = 1;
			file_->write((char*)&buffend, 4);
			file_->write((char*)&end_spill_size, 4);
			file_->write((char*)&total_num_chunks, 4);
			file_->write((char*)&current_chunk_num, 4);
			file_->write((char*)&pacman_word1, 4);
			file_->write((char*)&pacman_word2, 4);
			file_->write((char*)&buffend, 4); // write 0xFFFFFFFF (signal end of spill footer)
		
			current_buff_pos += this_chunk_sizeW + 7;
			buff_words_remaining = ACTUAL_BUFF_SIZE - current_buff_pos;
				
			return true;
		} 

		if(debug_mode){
			std::cout << "debug: nWords_=" << nWords_ << ", total_num_chunks=" << total_num_chunks << ", current_buff_pos=" << current_buff_pos << std::endl;
			std::cout << "debug: buff_words_remaining=" << buff_words_remaining << ", good_words_remaining=" << good_words_remaining << std::endl;
		}
	
		current_chunk_num = 0;
		while(words_written < nWords_){
			// Calculate the size of this chunk
			if((nWords_ - words_written + 10) >= good_words_remaining){ // Spill chunk will require more than this buffer
				this_chunk_sizeW = good_words_remaining;
			
				// Write the chunk header
				this_chunk_sizeB = 4 * this_chunk_sizeW;
				file_->write((char*)&this_chunk_sizeB, 4);
				file_->write((char*)&total_num_chunks, 4);
				file_->write((char*)&current_chunk_num, 4);
		
				// Actually write the data
				if(debug_mode){ std::cout << "debug: writing spill chunk " << current_chunk_num << " of " << total_num_chunks << " with " << this_chunk_sizeW << " words\n"; }
				file_->write((char*)&data_[4*words_written], (this_chunk_sizeB - 12));
				file_->write((char*)&buffend, 4); // Mark the end of this chunk
				current_chunk_num++;
		
				current_buff_pos += this_chunk_sizeW + 1;
				buff_words_remaining = ACTUAL_BUFF_SIZE - current_buff_pos;		
				good_words_remaining = 0;
				words_written += this_chunk_sizeW - 3;
			
				Close(file_);
				open_(file_);
				buffs_written++;
			}
			else{ // Spill chunk (plus spill footer) will fit in this buffer. This is the final chunk
				this_chunk_sizeW = (nWords_ - words_written + 3);
			
				// Write the chunk header
				this_chunk_sizeB = 4 * this_chunk_sizeW;
				file_->write((char*)&this_chunk_sizeB, 4);
				file_->write((char*)&total_num_chunks, 4);
				file_->write((char*)&current_chunk_num, 4);
		
				// Actually write the data
				if(debug_mode){ std::cout << "debug: writing final spill chunk " << current_chunk_num << " with " << this_chunk_sizeW << " words\n"; }
				file_->write((char*)&data_[4*words_written], (this_chunk_sizeB - 12));
				file_->write((char*)&buffend, 4); // Mark the end of this chunk
				current_chunk_num++;

				current_buff_pos += this_chunk_sizeW + 1;
				buff_words_remaining = ACTUAL_BUFF_SIZE - current_buff_pos;		
				good_words_remaining = good_words_remaining - this_chunk_sizeW;
				words_written += this_chunk_sizeW - 3;
			}
		}

		// Can't fit spill footer. Fill with 0xFFFFFFFF and start new buffer instead
		if(good_words_remaining < 7){ 
			Close(file_);
			open_(file_);
			buffs_written++;
		}
	
		if(debug_mode){ std::cout << "debug: writing 24 bytes (6 words) for spill footer (chunk " << current_chunk_num << ")\n"; }
	
		// Write the end of spill buffer (5 words + 1 end of buffer words)
		file_->write((char*)&end_spill_size, 4);
		file_->write((char*)&total_num_chunks, 4);
		file_->write((char*)&current_chunk_num, 4);
		file_->write((char*)&pacman_word1, 4);
		file_->write((char*)&pacman_word2, 4);
		file_->write((char*)&buffend, 4); // write 0xFFFFFFFF (signal end of spill footer)
	
		current_buff_pos += 6;
		buff_words_remaining = ACTUAL_BUFF_SIZE - current_buff_pos;
		good_words_remaining = good_words_remaining - 6;
	
		if(debug_mode){ 
			std::cout << "debug: finished writing spill into " << buffs_written << " new buffers\n"; 
			if(total_num_chunks != current_chunk_num + 1){ 
				std::cout << "debug: total number of chunks does not equal number of chunks written (" << total_num_chunks << " != " << current_chunk_num+1 << ")!!!\n"; 
			}
			std::cout << std::endl;
		}

		return true;
}
Esempio n. 7
0
/*
 * Execute command f, arg list t.
 * Record error message if not found.
 * Also do shell scripts here.
 */
void
texec(struct command *cmd, tchar *f, tchar **t)
{
	int	pfstatus = 0;
	struct	varent *v;
	tchar	**vp;
	tchar		*lastsh[2];

#ifdef TRACE
	tprintf("TRACE- texec()\n");
#endif
	/* convert cfname and cargs from tchar to char */
	tconvert(cmd, f, t);

	if (pfcshflag == 1) {
		pfstatus = secpolicy_pfexec((const char *)(cmd->cfname),
		    cmd->cargs, (const char **)NULL);
		if (pfstatus != NOATTRS) {
			errno = pfstatus;
		}
	}
	if ((pfcshflag == 0) || (pfstatus == NOATTRS)) {
		execv(cmd->cfname, cmd->cargs);
	}

	/*
	 * exec returned, free up allocations from above
	 * tconvert(), zero cfname and cargs to prevent
	 * duplicate free() in freesyn()
	 */
	xfree(cmd->cfname);
	chr_blkfree(cmd->cargs);
	cmd->cfname = (char *)0;
	cmd->cargs = (char **)0;

	switch (errno) {
	case ENOEXEC:
		/* check that this is not a binary file */
		{
			int ff = open_(f, 0);
			tchar ch[MB_LEN_MAX];

			if (ff != -1 && read_(ff, ch, 1) == 1 &&
			    !isprint(ch[0]) && !isspace(ch[0])) {
				printf("Cannot execute binary file.\n");
				Perror(f);
				(void) close(ff);
				unsetfd(ff);
				return;
			}
			(void) close(ff);
			unsetfd(ff);
		}
		/*
		 * If there is an alias for shell, then
		 * put the words of the alias in front of the
		 * argument list replacing the command name.
		 * Note no interpretation of the words at this point.
		 */
		v = adrof1(S_shell /* "shell" */, &aliases);
		if (v == 0) {
#ifdef OTHERSH
			int ff = open_(f, 0);
			tchar ch[MB_LEN_MAX];
#endif

			vp = lastsh;
			vp[0] = adrof(S_shell /* "shell" */) ? value(S_shell /* "shell" */) : S_SHELLPATH /* SHELLPATH */;
			vp[1] =  (tchar *) NULL;
#ifdef OTHERSH
			if (ff != -1 && read_(ff, ch, 1) == 1 && ch[0] != '#')
				vp[0] = S_OTHERSH /* OTHERSH */;
			(void) close(ff);
			unsetfd(ff);
#endif
		} else
			vp = v->vec;
		t[0] = f;
		t = blkspl(vp, t);		/* Splice up the new arglst */
		f = *t;

		tconvert(cmd, f, t);		/* convert tchar to char */

		/*
		 * now done with tchar arg list t,
		 * free the space calloc'd by above blkspl()
		 */
		xfree((char *)t);

		execv(cmd->cfname, cmd->cargs);	/* exec the command */

		/* exec returned, same free'ing as above */
		xfree(cmd->cfname);
		chr_blkfree(cmd->cargs);
		cmd->cfname = (char *)0;
		cmd->cargs = (char **)0;

		/* The sky is falling, the sky is falling! */

	case ENOMEM:
		Perror(f);

	case ENOENT:
		break;

	default:
		if (exerr == 0) {
			exerr = strerror(errno);
			setname(f);
		}
	}
}