apr_file_open函式用法心得

Mr_John_Liang發表於2013-07-26
apr_status_t apr_file_open ( apr_file_t **  newf,
    const char *  fname,
    apr_int32_t  flag,
    apr_fileperms_t  perm,
    apr_pool_t pool 
  )    

Open the specified file.

Parameters:
newf The opened file descriptor.
fname The full path to the file (using / on all systems)
flag Or'ed value of:
         APR_READ              open for reading
         APR_WRITE             open for writing
         APR_CREATE            create the file if not there
         APR_APPEND            file ptr is set to end prior to all writes
         APR_TRUNCATE          set length to zero if file exists
         APR_BINARY            not a text file (This flag is ignored on 
                               UNIX because it has no meaning)
         APR_BUFFERED          buffer the data.  Default is non-buffered
         APR_EXCL              return error if APR_CREATE and file exists
         APR_DELONCLOSE        delete the file after closing.
         APR_XTHREAD           Platform dependent tag to open the file
                               for use across multiple threads
         APR_SHARELOCK         Platform dependent support for higher
                               level locked read/write access to support
                               writes across process/machines
         APR_FILE_NOCLEANUP    Do not register a cleanup with the pool 
                               passed in on the pool argument (see below).
                               The apr_os_file_t handle in apr_file_t will not
                               be closed when the pool is destroyed.
         APR_SENDFILE_ENABLED  Open with appropriate platform semantics
                               for sendfile operations.  Advisory only,
                               apr_socket_sendfile does not check this flag.
 
perm Access permissions for file.
pool The pool to use.
Remarks:
If perm is APR_OS_DEFAULT and the file is being created, appropriate default permissions will be used.
By default, the returned file descriptor will not be inherited by child processes created byapr_proc_create(). This can be changed using apr_file_inherit_set().
如果你是服務端,客戶端請求傳送檔案,你寫在指定路徑下,此時在開啟檔案時需要注意,apr_file_open函式,第三個引數應該寫為
APR_WRITE|APR_CREATE| APR_TRUNCATE,其中APR_TRUNCATE表示,如果檔案已經存在,則先將檔案清空,這樣就不會發生以下這種情況:
先發個大的檔案,再傳送個小檔案,此時新生成的檔案不會全部覆蓋以前的檔案,這就是BUG,故將引數設定成像上述所說的一樣!

當頻繁呼叫該函式,及需要在Pool上開闢空間的函式,應該使用區域性的 pool  !!!

相關文章