Exemple #1
0
//static
S32 LLAPRFile::seek(apr_file_t* file_handle, apr_seek_where_t where, S32 offset)
{
	if(!file_handle)
	{
		return -1 ;
	}

	apr_status_t s;
	apr_off_t apr_offset;
	if (offset >= 0)
	{
		apr_offset = (apr_off_t)offset;
		s = apr_file_seek(file_handle, where, &apr_offset);
	}
	else
	{
		apr_offset = 0;
		s = apr_file_seek(file_handle, APR_END, &apr_offset);
	}
	if (s != APR_SUCCESS)
	{
		ll_apr_warn_status(s);
		return -1;
	}
	else
	{
		llassert_always(apr_offset <= 0x7fffffff);
		return (S32)apr_offset;
	}
}
Exemple #2
0
// File I/O
apr_file_t* ll_apr_file_open(const std::string& filename, apr_int32_t flags, S32* sizep, apr_pool_t* pool)
{
	apr_file_t* apr_file;
	apr_status_t s;
	if (pool == NULL) pool = gAPRPoolp;
	s = apr_file_open(&apr_file, filename.c_str(), flags, APR_OS_DEFAULT, pool);
	if (s != APR_SUCCESS)
	{
		if (sizep)
		{
			*sizep = 0;
		}
		return NULL;
	}

	if (sizep)
	{
		S32 file_size = 0;
		apr_off_t offset = 0;
		if (apr_file_seek(apr_file, APR_END, &offset) == APR_SUCCESS)
		{
			llassert_always(offset <= 0x7fffffff);
			file_size = (S32)offset;
			offset = 0;
			apr_file_seek(apr_file, APR_SET, &offset);
		}
		*sizep = file_size;
	}

	return apr_file;
}
Exemple #3
0
const char *exterm_test_sample(navel_context_t *cont, apr_pool_t *pool)
{
	const char *file_name = "test/sample.x";
	apr_file_t *bin_file;
	apr_off_t off;
	size_t len;
	apr_byte_t *data;
	term_t bin, exploded;

	if (cont->solitaire == 0)
		return "No spawned process to use";

	apr_file_open(&bin_file, file_name, APR_READ, 0, pool);
	off = 0;
	apr_file_seek(bin_file, APR_END, &off);
	len = (int)off;
	off = 0;
	apr_file_seek(bin_file, APR_SET, &off);
	data = apr_palloc(pool, len);
	apr_file_read(bin_file, data, &len);
	apr_file_close(bin_file);

	bin = heap_binary(cont->heap, 8*len, data);
	exploded = bif_binary_to_term1(bin, cont->solitaire);

	return apr_psprintf(pool, "%s", term2html(exploded, cont->atoms, pool));
}
Exemple #4
0
static int file_seek(lua_State *L)
{
  /* TODO Seek the write buffer as well! */

  const char *const modenames[] = { "set", "cur", "end", NULL };
  const apr_seek_where_t modes[] = { APR_SET, APR_CUR, APR_END };

  apr_status_t status;
  lua_apr_file *file;
  lua_apr_buffer *B;
  apr_off_t offset;
  int mode;

  file = file_check(L, 1, 1);
  B = &file->input.buffer;
  mode = modes[luaL_checkoption(L, 2, "cur", modenames)];
  offset = luaL_optlong(L, 3, 0);

  /* XXX Flush write buffer before changing offset! */
  if (!(mode == APR_CUR && offset == 0)) {
    status = flush_buffer(L, &file->output, 1);
    if (status != APR_SUCCESS)
      return push_file_error(L, file, status);
  }

  /* Make relative offsets absolute, adjust for buffered input. */
  if (mode == APR_CUR && B->index < B->limit) {
    apr_off_t temp = 0;
    status = apr_file_seek(file->handle, APR_CUR, &temp);
    if (status != APR_SUCCESS)
      return push_file_error(L, file, status);
    mode = APR_SET, offset = temp - (B->limit - B->index);
  }

  /* Perform the requested seek() operation. */
  status = apr_file_seek(file->handle, mode, &offset);
  if (status != APR_SUCCESS)
    return push_file_error(L, file, status);

  /* Invalidate all buffered input (very inefficient but foolproof: parts of
   * the buffer may have been modified by the binary to text translation).
   * XXX The write buffer has already been reset by flush_buffer() above.
   * FIXME Don't invalidate the buffered input unnecessarily?!
   */
  file->input.buffer.index = 0;
  file->input.buffer.limit = 0;

  /* FIXME Bound to lose precision when APR_FOPEN_LARGEFILE is in effect? */
  lua_pushnumber(L, (lua_Number) offset);

  return 1;
}
apr_status_t LLAPRFile::open(std::string const& filename, apr_int32_t flags, access_t access_type, S32* sizep)
{
	llassert_always(!mFile);
	llassert_always(!mVolatileFilePoolp && !mRegularFilePoolp);

	apr_status_t status;
	{
		apr_pool_t* apr_file_open_pool;	// The use of apr_pool_t is OK here.
										// This is a temporary variable for a pool that is passed directly to apr_file_open below.
		if (access_type == short_lived)
		{
			// Use a "volatile" thread-local pool.
			mVolatileFilePoolp = &LLThreadLocalData::tldata().mVolatileAPRPool;
			// Access the pool and increment it's reference count.
			// The reference count of LLVolatileAPRPool objects will be decremented
			// again in LLAPRFile::close by calling mVolatileFilePoolp->clearVolatileAPRPool().
			apr_file_open_pool = mVolatileFilePoolp->getVolatileAPRPool();
		}
		else
		{
			mRegularFilePoolp = new LLAPRPool(LLThreadLocalData::tldata().mRootPool);
			apr_file_open_pool = (*mRegularFilePoolp)();
		}
		status = apr_file_open(&mFile, filename.c_str(), flags, APR_OS_DEFAULT, apr_file_open_pool);
	}
	if (status != APR_SUCCESS || !mFile)
	{
		mFile = NULL ;
		close() ;
		if (sizep)
		{
			*sizep = 0;
		}
		return status;
	}

	if (sizep)
	{
		S32 file_size = 0;
		apr_off_t offset = 0;
		if (apr_file_seek(mFile, APR_END, &offset) == APR_SUCCESS)
		{
			llassert_always(offset <= 0x7fffffff);
			file_size = (S32)offset;
			offset = 0;
			apr_file_seek(mFile, APR_SET, &offset);
		}
		*sizep = file_size;
	}

	return status;
}
Exemple #6
0
apr_status_t LLAPRFile::open(const std::string& filename, apr_int32_t flags, LLVolatileAPRPool* pool, S32* sizep)
{
	apr_status_t s ;

	//check if already open some file
	llassert_always(!mFile) ;
	llassert_always(!mCurrentFilePoolp) ;
	
	apr_pool_t* apr_pool = pool ? pool->getVolatileAPRPool() : NULL ;

	// <FS:ND> Convert filenames with UTF-8 charaters into a shortfilename (8.3) if running under windows

	// s = apr_file_open(&mFile, filename.c_str(), flags, APR_OS_DEFAULT, getAPRFilePool(apr_pool));
	s = apr_file_open(&mFile, ndConvertFilename(filename).c_str(), flags, APR_OS_DEFAULT, getAPRFilePool(apr_pool));

	// </FS:ND>

	if (s != APR_SUCCESS || !mFile)
	{
		mFile = NULL ;
		
		if (sizep)
		{
			*sizep = 0;
		}
	}
	else if (sizep)
	{
		S32 file_size = 0;
		apr_off_t offset = 0;
		if (apr_file_seek(mFile, APR_END, &offset) == APR_SUCCESS)
		{
			llassert_always(offset <= 0x7fffffff);
			file_size = (S32)offset;
			offset = 0;
			apr_file_seek(mFile, APR_SET, &offset);
		}
		*sizep = file_size;
	}

	if(!mCurrentFilePoolp)
	{
		mCurrentFilePoolp = pool ;

		if(!mFile)
		{
			close() ;
		}
	}

	return s ;
}
Exemple #7
0
static void test_xthread(abts_case *tc, void *data)
{
    apr_file_t *f;
    const char *fname = "data/testxthread.dat";
    apr_status_t rv;
    apr_int32_t flags = APR_FOPEN_CREATE|APR_FOPEN_READ|APR_FOPEN_WRITE|APR_FOPEN_APPEND|APR_FOPEN_XTHREAD;
    char buf[128] = { 0 };

    /* Test for bug 38438, opening file with append + xthread and seeking to
       the end of the file resulted in writes going to the beginning not the
       end. */

    apr_file_remove(fname, p);

    APR_ASSERT_SUCCESS(tc, "open test file",
                       apr_file_open(&f, fname, flags,
                                     APR_UREAD|APR_UWRITE, p));

    APR_ASSERT_SUCCESS(tc, "write should succeed",
                       apr_file_puts("hello", f));

    apr_file_close(f);

    APR_ASSERT_SUCCESS(tc, "open test file",
                       apr_file_open(&f, fname, flags,
                                     APR_UREAD|APR_UWRITE, p));

    /* Seek to the end. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_END, &offset);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    }

    APR_ASSERT_SUCCESS(tc, "more writes should succeed",
                       apr_file_puts("world", f));

    /* Back to the beginning. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_SET, &offset);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    }

    apr_file_read_full(f, buf, sizeof(buf), NULL);

    ABTS_STR_EQUAL(tc, "helloworld", buf);

    apr_file_close(f);
}
Exemple #8
0
static void test_seek(abts_case *tc, void *data)
{
    apr_status_t rv;
    apr_off_t offset = 5;
    apr_size_t nbytes = 256;
    char *str = apr_pcalloc(p, nbytes + 1);
    apr_file_t *filetest = NULL;

    rv = apr_file_open(&filetest, FILENAME, 
                       APR_READ, 
                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
    APR_ASSERT_SUCCESS(tc, "Open test file " FILENAME, rv);

    rv = apr_file_read(filetest, str, &nbytes);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_INT_EQUAL(tc, strlen(TESTSTR), nbytes);
    ABTS_STR_EQUAL(tc, TESTSTR, str);

    memset(str, 0, nbytes + 1);

    rv = apr_file_seek(filetest, SEEK_SET, &offset);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    
    rv = apr_file_read(filetest, str, &nbytes);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_INT_EQUAL(tc, strlen(TESTSTR) - 5, nbytes);
    ABTS_STR_EQUAL(tc, TESTSTR + 5, str);

    apr_file_close(filetest);

    /* Test for regression of sign error bug with SEEK_END and
       buffered files. */
    rv = apr_file_open(&filetest, FILENAME,
                       APR_READ | APR_BUFFERED,
                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
    APR_ASSERT_SUCCESS(tc, "Open test file " FILENAME, rv);

    offset = -5;
    rv = apr_file_seek(filetest, SEEK_END, &offset);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_INT_EQUAL(tc, strlen(TESTSTR) - 5, nbytes);

    memset(str, 0, nbytes + 1);
    nbytes = 256;
    rv = apr_file_read(filetest, str, &nbytes);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_INT_EQUAL(tc, 5, nbytes);
    ABTS_STR_EQUAL(tc, TESTSTR + strlen(TESTSTR) - 5, str);

    apr_file_close(filetest);
}                
Exemple #9
0
static void test_seek(CuTest *tc)
{
    apr_status_t rv;
    apr_off_t offset = 5;
    apr_size_t nbytes = 256;
    char *str = apr_pcalloc(p, nbytes + 1);
    apr_file_t *filetest = NULL;

    rv = apr_file_open(&filetest, FILENAME, 
                       APR_READ, 
                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
    apr_assert_success(tc, "Open test file " FILENAME, rv);

    rv = apr_file_read(filetest, str, &nbytes);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    CuAssertIntEquals(tc, strlen(TESTSTR), nbytes);
    CuAssertStrEquals(tc, TESTSTR, str);

    memset(str, 0, nbytes + 1);

    rv = apr_file_seek(filetest, SEEK_SET, &offset);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    
    rv = apr_file_read(filetest, str, &nbytes);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    CuAssertIntEquals(tc, strlen(TESTSTR) - 5, nbytes);
    CuAssertStrEquals(tc, TESTSTR + 5, str);

    apr_file_close(filetest);

    /* Test for regression of sign error bug with SEEK_END and
       buffered files. */
    rv = apr_file_open(&filetest, FILENAME,
                       APR_READ | APR_BUFFERED,
                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
    apr_assert_success(tc, "Open test file " FILENAME, rv);

    offset = -5;
    rv = apr_file_seek(filetest, SEEK_END, &offset);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    CuAssertIntEquals(tc, strlen(TESTSTR) - 5, offset);

    memset(str, 0, nbytes + 1);
    nbytes = 256;
    rv = apr_file_read(filetest, str, &nbytes);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    CuAssertIntEquals(tc, 5, nbytes);
    CuAssertStrEquals(tc, TESTSTR + strlen(TESTSTR) - 5, str);

    apr_file_close(filetest);
}                
Exemple #10
0
static void create_segments(geo_db *geo) {
    int i, j;
    unsigned char delim[3];
    unsigned char buf[GEO_SEGMENT_RECORD_LENGTH];
    apr_size_t nbytes;
    apr_off_t offset;

    geo->ctry_offset = 0;

    geo->dbtype = GEOIP_COUNTRY_EDITION;
    offset = -3l;
    apr_file_seek(geo->db, APR_END, &offset);

    for (i = 0; i < GEO_STRUCT_INFO_MAX_SIZE; i++) {

        apr_file_read_full(geo->db, &delim, 3, &nbytes);

        if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) {
            apr_file_read_full(geo->db, &geo->dbtype, 1, &nbytes);
            if (geo->dbtype >= 106) {
                geo->dbtype -= 105;
            }

            if (geo->dbtype == GEOIP_REGION_EDITION_REV0) {
                geo->ctry_offset = GEO_STATE_BEGIN_REV0;
            } else if (geo->dbtype == GEOIP_REGION_EDITION_REV1) {
                geo->ctry_offset = GEO_STATE_BEGIN_REV1;
            } else if (geo->dbtype == GEOIP_CITY_EDITION_REV0 ||
                                 geo->dbtype == GEOIP_CITY_EDITION_REV1 ||
                                 geo->dbtype == GEOIP_ORG_EDITION ||
                                 geo->dbtype == GEOIP_ISP_EDITION ||
                                 geo->dbtype == GEOIP_ASNUM_EDITION) {
                geo->ctry_offset = 0;
                apr_file_read_full(geo->db, &buf, GEO_SEGMENT_RECORD_LENGTH, &nbytes);
                for (j = 0; j < GEO_SEGMENT_RECORD_LENGTH; j++) {
                    geo->ctry_offset += (buf[j] << (j * 8));
                }
            }
            break;
        } else {
            offset = -4l;
            apr_file_seek(geo->db, APR_CUR, &offset);
        }
    }
    if (geo->dbtype == GEOIP_COUNTRY_EDITION ||
            geo->dbtype == GEOIP_PROXY_EDITION ||
            geo->dbtype == GEOIP_NETSPEED_EDITION) {
        geo->ctry_offset = GEO_COUNTRY_BEGIN;
    }
}
Exemple #11
0
/* reads a line from a filehandle. */
MVMString * MVM_file_readline_fh(MVMThreadContext *tc, MVMObject *oshandle) {
    MVMString *result;
    apr_status_t rv;
    MVMOSHandle *handle;
    char ch;
    char *buf;
    apr_off_t offset = 0;
    apr_off_t fetched = 0;
    apr_off_t bytes_read = 0;

    verify_filehandle_type(tc, oshandle, &handle, "readline from filehandle");

    if ((rv = apr_file_seek(handle->body.file_handle, APR_CUR, &offset)) != APR_SUCCESS) {
        MVM_exception_throw_apr_error(tc, rv, "Failed to tell position of filehandle in readline(1): ");
    }

    while (apr_file_getc(&ch, handle->body.file_handle) == APR_SUCCESS && ch != 10 && ch != 13) {
        bytes_read++;
    }

    /* have a look if it is a windows newline, and step back if not. */
    if (ch == 13 && apr_file_getc(&ch, handle->body.file_handle) == APR_SUCCESS && ch != 10) {
        fetched--;
    }

    if ((rv = apr_file_seek(handle->body.file_handle, APR_CUR, &fetched)) != APR_SUCCESS) {
        MVM_exception_throw_apr_error(tc, rv, "Failed to tell position of filehandle in readline(2): ");
    }

    if ((rv = apr_file_seek(handle->body.file_handle, APR_SET, &offset)) != APR_SUCCESS) {
        MVM_exception_throw_apr_error(tc, rv, "Failed to tell position of filehandle in readline(3)");
    }

    buf = malloc((int)(bytes_read + 1));

    if ((rv = apr_file_read(handle->body.file_handle, buf, &bytes_read)) != APR_SUCCESS) {
        free(buf);
        MVM_exception_throw_apr_error(tc, rv, "readline from filehandle failed: ");
    }

    if ((rv = apr_file_seek(handle->body.file_handle, APR_SET, &fetched)) != APR_SUCCESS) {
        MVM_exception_throw_apr_error(tc, rv, "Failed to tell position of filehandle in readline(4)");
    }
                                               /* XXX should this take a type object? */
    result = MVM_decode_C_buffer_to_string(tc, tc->instance->VMString, buf, bytes_read, handle->body.encoding_type);
    free(buf);

    return result;
}
Exemple #12
0
static void test_xthread(CuTest *tc)
{
    apr_file_t *f;
    const char *fname = "data/testxthread.dat";
    apr_status_t rv;
    apr_int32_t flags = APR_CREATE|APR_READ|APR_WRITE|APR_APPEND|APR_XTHREAD;
    char buf[128] = { 0 };

    /* Test for bug 38438, opening file with append + xthread and seeking to 
       the end of the file resulted in writes going to the beginning not the
       end. */

    apr_file_remove(fname, p);

    rv = apr_file_open(&f, fname, flags, APR_UREAD|APR_UWRITE, p);
    CuAssert(tc, "open test file", rv == APR_SUCCESS);

    rv = apr_file_puts("hello", f);
    CuAssert(tc, "write should succeed", rv == APR_SUCCESS);

    apr_file_close(f);
    
    rv = apr_file_open(&f, fname, flags, APR_UREAD|APR_UWRITE, p);
    CuAssert(tc, "open test file", rv == APR_SUCCESS);

    /* Seek to the end. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_END, &offset);
    }

    rv = apr_file_puts("world", f);
    CuAssert(tc, "more writes should succeed", rv == APR_SUCCESS);

    /* Back to the beginning. */
    {
        apr_off_t offset = 0;
        
        rv = apr_file_seek(f, APR_SET, &offset);
    }
    
    apr_file_read_full(f, buf, sizeof(buf), NULL);

    CuAssertStrEquals(tc, "helloworld", buf);

    apr_file_close(f);
}
Exemple #13
0
apr_status_t LLAPRFile::open(std::string const& filename, apr_int32_t flags, access_t access_type, S32* sizep)
{
	llassert_always(!mFile);
	llassert_always(!mCurrentFilePoolp);

	// Access the pool and increment it's reference count.
	// The reference count of LLVolatileAPRPool objects will be decremented
	// again in LLAPRFile::close by calling mCurrentFilePoolp->clearVolatileAPRPool().
	apr_pool_t* pool;
	if (access_type == local)
	{
	  	// Use a "volatile" thread-local pool.
		mCurrentFilePoolp = LLVolatileAPRPool::getLocalAPRFilePool();
		pool = mCurrentFilePoolp->getVolatileAPRPool();
	}
	else
	{
	  	llassert(is_main_thread());
		pool = gAPRPoolp;
	}
	apr_status_t s = apr_file_open(&mFile, filename.c_str(), flags, APR_OS_DEFAULT, pool);
	if (s != APR_SUCCESS || !mFile)
	{
		mFile = NULL ;
		close() ;
		if (sizep)
		{
			*sizep = 0;
		}
		return s;
	}

	if (sizep)
	{
		S32 file_size = 0;
		apr_off_t offset = 0;
		if (apr_file_seek(mFile, APR_END, &offset) == APR_SUCCESS)
		{
			llassert_always(offset <= 0x7fffffff);
			file_size = (S32)offset;
			offset = 0;
			apr_file_seek(mFile, APR_SET, &offset);
		}
		*sizep = file_size;
	}

	return s;
}
Exemple #14
0
static void _write_item(apr_file_t *file, nx_cc_item_t *item)
{
    nx_value_t key;
    nx_string_t str;
    nx_exception_t e;

    ASSERT(file != NULL);
    ASSERT(item != NULL);
    ASSERT(item->key != NULL);
    ASSERT(item->value != NULL);

    nx_value_init(&key);
    key.type = NX_VALUE_TYPE_STRING;
    nx_string_init_const(&str, item->key);
    key.string = &str;

    try
    {
	CHECKERR_MSG(nx_value_to_file(&key, file), "couldn't write key to cache file");
	if ( item->value->type == NX_VALUE_TYPE_INTEGER )
	{ // get offset for integer values so that updates can be done efficiently	    
	    item->offs = 0;
	    CHECKERR_MSG(apr_file_seek(file, APR_CUR, &(item->offs)), "failed to seek in cache file");
	}
	CHECKERR_MSG(nx_value_to_file(item->value, file), "couldn't write value to cache file");
    }
    catch(e)
    {
	nx_value_kill(&key);
	rethrow(e);
    }
}
Exemple #15
0
// this callback fucntion is used to write all the data from a CURL request to a file
static int write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
	int len = size * nmemb;
	int written = 	apr_file_write((apr_file_t*)stream, ptr, &len);
	apr_off_t offset = 0;
	apr_file_seek((apr_file_t*)stream, APR_SET, &offset);
	return size * nmemb;
}
Exemple #16
0
// This function takes the request and a buffer containing a name
// and uses that to retrieve information about the location and then
// store it in the buffer.
static int get_place_coordinates(request_rec *r, char *buf) {
	// initialization
	apr_file_t *name_data;
	apr_status_t rv;

	// opens a file/stream for manipulating data
	if(rv = open_file(r, &name_data, MOON_TEMP_FILE_LOC) != OK) {
		return rv;
	}

	// retrieves the data and stores it in the stream via libcurl
	rv = retrieve_name_data(r, name_data, buf);
	if(rv != OK) {	
		apr_file_close(name_data);
		return rv;
	}

	// resets stream to beginning
	apr_off_t offset = 0;
	apr_file_seek(name_data, APR_SET, &offset);

	// stores coordinates int the buffer and returns
	int size = MAX_BUF_LENGTH;
	rv = apr_file_read(name_data, buf, &size);
	apr_file_close(name_data);
	if(size>=MAX_BUF_LENGTH) {
		ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "coords too long");
		return HTTP_INTERNAL_SERVER_ERROR;
	}
	buf[size]='\0';
	return OK;
}
Exemple #17
0
apr_status_t apr_file_trunc(apr_file_t *fp, apr_off_t offset)
{
    if (fp->buffered) {
        int rc = 0;
        file_lock(fp);
        if (fp->direction == 1 && fp->bufpos != 0) {
            apr_off_t len = fp->filePtr + fp->bufpos;
            if (offset < len) {
                /* New file end fall below our write buffer limit.
                 * Figure out if and what needs to be flushed.
                 */
                apr_off_t off = len - offset;
                if (off >= 0 && off <= fp->bufpos)
                    fp->bufpos = fp->bufpos - (size_t)off;
                else
                    fp->bufpos = 0;
            }
            rc = apr_file_flush_locked(fp);
            /* Reset buffer positions for write mode */
            fp->bufpos = fp->direction = fp->dataRead = 0;
        }
        file_unlock(fp);
        if (rc) {
            return rc;
        }
    }
    if (ftruncate(fp->filedes, offset) == -1) {
        return errno;
    }
    return apr_file_seek(fp, APR_SET, &offset);
}
Exemple #18
0
SWITCH_DECLARE(switch_status_t) switch_file_seek(switch_file_t *thefile, switch_seek_where_t where, int64_t *offset)
{
	apr_status_t rv;
	apr_off_t off = (apr_off_t) (*offset);
	rv = apr_file_seek(thefile, where, &off);
	*offset = (int64_t) off;
	return rv;
}
Exemple #19
0
/* Rewind the file pointer */
static void rewind_file(apr_file_t *fp)
{
  apr_off_t offset = 0;
#ifndef NDEBUG
  apr_status_t apr_err =
#endif
    apr_file_seek(fp, APR_SET, &offset);
  assert(apr_err == 0);
  assert(offset == 0);
}
Exemple #20
0
/* seeks in a filehandle */
void MVM_file_seek(MVMThreadContext *tc, MVMObject *oshandle, MVMint64 offset, MVMint64 flag) {
    apr_status_t rv;
    MVMOSHandle *handle;

    verify_filehandle_type(tc, oshandle, &handle, "seek in filehandle");

    if ((rv = apr_file_seek(handle->body.file_handle, (apr_seek_where_t)flag, (apr_off_t *)&offset)) != APR_SUCCESS) {
        MVM_exception_throw_apr_error(tc, rv, "Failed to seek in filehandle: ");
    }
}
Exemple #21
0
/*
 * write JSON metadata to a file
 */
static apr_byte_t oidc_metadata_file_write(request_rec *r, const char *path,
		const char *data) {

	// TODO: completely erase the contents of the file if it already exists....

	apr_file_t *fd = NULL;
	apr_status_t rc = APR_SUCCESS;
	apr_size_t bytes_written = 0;
	char s_err[128];

	/* try to open the metadata file for writing, creating it if it does not exist */
	if ((rc = apr_file_open(&fd, path, (APR_FOPEN_WRITE | APR_FOPEN_CREATE),
			APR_OS_DEFAULT, r->pool)) != APR_SUCCESS) {
		oidc_error(r, "file \"%s\" could not be opened (%s)", path,
				apr_strerror(rc, s_err, sizeof(s_err)));
		return FALSE;
	}

	/* lock the file and move the write pointer to the start of it */
	apr_file_lock(fd, APR_FLOCK_EXCLUSIVE);
	apr_off_t begin = 0;
	apr_file_seek(fd, APR_SET, &begin);

	/* calculate the length of the data, which is a string length */
	apr_size_t len = strlen(data);

	/* (blocking) write the number of bytes in the buffer */
	rc = apr_file_write_full(fd, data, len, &bytes_written);

	/* check for a system error */
	if (rc != APR_SUCCESS) {
		oidc_error(r, "could not write to: \"%s\" (%s)", path,
				apr_strerror(rc, s_err, sizeof(s_err)));
		return FALSE;
	}

	/* check that all bytes from the header were written */
	if (bytes_written != len) {
		oidc_error(r,
				"could not write enough bytes to: \"%s\", bytes_written (%" APR_SIZE_T_FMT ") != len (%" APR_SIZE_T_FMT ")",
				path, bytes_written, len);
		return FALSE;
	}

	/* unlock and close the written file */
	apr_file_unlock(fd);
	apr_file_close(fd);

	oidc_debug(r, "file \"%s\" written; number of bytes (%" APR_SIZE_T_FMT ")",
			path, len);

	return TRUE;
}
Exemple #22
0
/**
 * Sets current position in this file, where the next write will occur.
 */
static apr_status_t
lcn_fs_index_output_seek ( lcn_index_output_t *io, apr_off_t pos )
{
    apr_status_t s;
    apr_off_t p = pos;

    LCNCR( lcn_index_output_flush( io ) );
    io->buffer_start = pos;
    LCNCR( apr_file_seek( ( ( lcn_fs_index_output_t*) io)->_apr_file, APR_SET, &p ) );

    return s;
}
Exemple #23
0
/* tells position within file */
MVMint64 MVM_file_tell_fh(MVMThreadContext *tc, MVMObject *oshandle) {
    apr_status_t rv;
    MVMOSHandle *handle;
    MVMint64 offset = 0;

    verify_filehandle_type(tc, oshandle, &handle, "tell in filehandle");

    if ((rv = apr_file_seek(handle->body.file_handle, APR_CUR, (apr_off_t *)&offset)) != APR_SUCCESS) {
        MVM_exception_throw_apr_error(tc, rv, "Failed to tell position of filehandle: ");
    }

    return offset;
}
Exemple #24
0
static void test_dup2_readwrite(abts_case *tc, void *data)
{
    apr_file_t *errfile = NULL;
    apr_file_t *testfile = NULL;
    apr_file_t *saveerr = NULL;
    apr_status_t rv;
    apr_size_t txtlen = sizeof(TEST);
    char buff[50];
    apr_off_t fpos;

    rv = apr_file_open(&testfile, FILEPATH "testdup2.readwrite.file",
                       APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE |
                       APR_FOPEN_DELONCLOSE, APR_OS_DEFAULT, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_PTR_NOTNULL(tc, testfile);

    rv = apr_file_open_stderr(&errfile, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);

    /* Set aside the real errfile */
    rv = apr_file_dup(&saveerr, errfile, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_PTR_NOTNULL(tc, saveerr);

    rv = apr_file_dup2(errfile, testfile, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_PTR_NOTNULL(tc, errfile);

    txtlen = sizeof(TEST2);
    rv = apr_file_write(errfile, TEST2, &txtlen);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_SIZE_EQUAL(tc, sizeof(TEST2), txtlen);

    fpos = 0;
    rv = apr_file_seek(testfile, APR_SET, &fpos);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);

    txtlen = 50;
    rv = apr_file_read(testfile, buff, &txtlen);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_STR_EQUAL(tc, TEST2, buff);

    apr_file_close(testfile);

    rv = apr_file_dup2(errfile, saveerr, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_PTR_NOTNULL(tc, errfile);

    apr_file_close(saveerr);
}
Exemple #25
0
int file_apache_read(file_handle_t* handler, void* output_buffer, int data_size, int offset_from_file_start, int flags){
#ifndef TEST_APP
	apache_file_handler_t* afh = (apache_file_handler_t*)handler;
	apr_off_t offset = offset_from_file_start;
	apr_size_t s = data_size;

	if (apr_file_seek(afh->f, APR_SET, &offset) == APR_SUCCESS && offset == offset_from_file_start){
		if (apr_file_read(afh->f, output_buffer, &s) == APR_SUCCESS ) {
			return s;
		}
	}
#endif
	return 0;
}
Exemple #26
0
apr_status_t LLAPRFile::open(const std::string& filename, apr_int32_t flags, apr_pool_t* pool, S32* sizep)
{
	apr_status_t s;

	//check if already open some file
	llassert_always(!mFile) ;
	llassert_always(!mCurrentFilePoolp) ;
	
	s = apr_file_open(&mFile, filename.c_str(), flags, APR_OS_DEFAULT, getAPRFilePool(pool));
	if (s != APR_SUCCESS || !mFile)
	{
		mFile = NULL ;
		close() ;
		if (sizep)
		{
			*sizep = 0;
		}
		return s;
	}

	if (sizep)
	{
		S32 file_size = 0;
		apr_off_t offset = 0;
		if (apr_file_seek(mFile, APR_END, &offset) == APR_SUCCESS)
		{
			llassert_always(offset <= 0x7fffffff);
			file_size = (S32)offset;
			offset = 0;
			apr_file_seek(mFile, APR_SET, &offset);
		}
		*sizep = file_size;
	}

	return s;
}
Exemple #27
0
S32 ll_apr_file_seek(apr_file_t* apr_file, apr_seek_where_t where, S32 offset)
{
	apr_status_t s;
	apr_off_t apr_offset;
	if (offset >= 0)
	{
		apr_offset = (apr_off_t)offset;
		s = apr_file_seek(apr_file, where, &apr_offset);
	}
	else
	{
		apr_offset = 0;
		s = apr_file_seek(apr_file, APR_END, &apr_offset);
	}
	if (s != APR_SUCCESS)
	{
		return -1;
	}
	else
	{
		llassert_always(apr_offset <= 0x7fffffff);
		return (S32)apr_offset;
	}
}
Exemple #28
0
static void test_fail_read_flush(abts_case *tc, void *data)
{
    apr_file_t *f;
    const char *fname = "data/testflush.dat";
    apr_status_t rv;
    char buf[2];

    apr_file_remove(fname, p);

    APR_ASSERT_SUCCESS(tc, "open test file",
                       apr_file_open(&f, fname,
                                     APR_CREATE|APR_READ|APR_BUFFERED,
                                     APR_UREAD|APR_UWRITE, p));

    /* this write should be buffered. */
    APR_ASSERT_SUCCESS(tc, "buffered write should succeed",
                       apr_file_puts("hello", f));

    /* Now, trying a read should fail since the write must be flushed,
     * and should fail with something other than EOF since the file is
     * opened read-only. */
    rv = apr_file_read_full(f, buf, 2, NULL);

    ABTS_ASSERT(tc, "read should flush buffered write and fail",
                rv != APR_SUCCESS && rv != APR_EOF);

    /* Likewise for gets */
    rv = apr_file_gets(buf, 2, f);

    ABTS_ASSERT(tc, "gets should flush buffered write and fail",
                rv != APR_SUCCESS && rv != APR_EOF);

    /* Likewise for seek. */
    {
        apr_off_t offset = 0;

        rv = apr_file_seek(f, APR_SET, &offset);
    }

    ABTS_ASSERT(tc, "seek should flush buffered write and fail",
                rv != APR_SUCCESS && rv != APR_EOF);

    apr_file_close(f);
}
Exemple #29
0
static void test_file_readwrite(abts_case *tc, void *data)
{
    apr_file_t *file1 = NULL;
    apr_file_t *file3 = NULL;
    apr_status_t rv;
    apr_finfo_t finfo;
    apr_size_t txtlen = sizeof(TEST);
    char buff[50];
    apr_off_t fpos;

    /* First, create a new file, empty... */
    rv = apr_file_open(&file1, FILEPATH "testdup.readwrite.file",
                       APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE |
                       APR_FOPEN_DELONCLOSE, APR_OS_DEFAULT, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_PTR_NOTNULL(tc, file1);

    rv = apr_file_dup(&file3, file1, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_PTR_NOTNULL(tc, file3);

    rv = apr_file_write(file3, TEST, &txtlen);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_SIZE_EQUAL(tc, sizeof(TEST), txtlen);

    fpos = 0;
    rv = apr_file_seek(file1, APR_SET, &fpos);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_ASSERT(tc, "File position mismatch, expected 0", fpos == 0);

    txtlen = 50;
    rv = apr_file_read(file1, buff, &txtlen);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    ABTS_STR_EQUAL(tc, TEST, buff);

    /* cleanup after ourselves */
    rv = apr_file_close(file1);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);

    rv = apr_file_close(file3);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    rv = apr_stat(&finfo, FILEPATH "testdup.readwrite.file", APR_FINFO_NORM, p);
    ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
}
Exemple #30
0
static void _update_item(nx_cc_item_t *item)
{
    nx_ctx_t *ctx;
    apr_off_t offs;
    nx_exception_t e;

    ASSERT(item->value != NULL);

    ctx = nx_ctx_get();

    CHECKERR(apr_thread_mutex_lock(ctx->config_cache_mutex));
    try
    {
	if ( ctx->ccfile == NULL )
	{
	    CHECKERR_MSG(apr_file_open(&(ctx->ccfile), ctx->ccfilename,
				       APR_READ | APR_WRITE | APR_CREATE | APR_TRUNCATE,
				       APR_OS_DEFAULT, ctx->pool),
			 "couldn't open config cache '%s' for writing", ctx->ccfilename);
	}
	// only integers are supported for now, string modifications trigger a whole rewrite
	if ( (item->offs > 0) && (item->value->type == NX_VALUE_TYPE_INTEGER) )

	{
	    offs = item->offs;
	    CHECKERR_MSG(apr_file_seek(ctx->ccfile, APR_SET, &offs),
			 "failed to seek in %s file", ctx->ccfilename);
	    CHECKERR_MSG(nx_value_to_file(item->value, ctx->ccfile), "failed to update config cache item");
	}
	else
	{ // write the whole file, not very efficient
	    nx_config_cache_write();
	}
	item->needflush = FALSE;
    }
    catch(e)
    {
	CHECKERR(apr_thread_mutex_unlock(ctx->config_cache_mutex));
	rethrow(e);
    }
    CHECKERR(apr_thread_mutex_unlock(ctx->config_cache_mutex));
}