OCILIB (C Driver for Oracle) 3.9.1
D:/Perso/dev/ocilib/ocilib/src/file.c
00001 /*
00002     +-----------------------------------------------------------------------------------------+
00003     |                                                                                         |
00004     |                               OCILIB - C Driver for Oracle                              |
00005     |                                                                                         |
00006     |                                (C Wrapper for Oracle OCI)                               |
00007     |                                                                                         |
00008     |                              Website : http://www.ocilib.net                            |
00009     |                                                                                         |
00010     |             Copyright (c) 2007-2011 Vincent ROGIER <vince.rogier@ocilib.net>            |
00011     |                                                                                         |
00012     +-----------------------------------------------------------------------------------------+
00013     |                                                                                         |
00014     |             This library is free software; you can redistribute it and/or               |
00015     |             modify it under the terms of the GNU Lesser General Public                  |
00016     |             License as published by the Free Software Foundation; either                |
00017     |             version 2 of the License, or (at your option) any later version.            |
00018     |                                                                                         |
00019     |             This library is distributed in the hope that it will be useful,             |
00020     |             but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00021     |             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           |
00022     |             Lesser General Public License for more details.                             |
00023     |                                                                                         |
00024     |             You should have received a copy of the GNU Lesser General Public            |
00025     |             License along with this library; if not, write to the Free                  |
00026     |             Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.          |
00027     |                                                                                         |
00028     +-----------------------------------------------------------------------------------------+
00029 */
00030 
00031 /* --------------------------------------------------------------------------------------------- *
00032  * $Id: file.c, v 3.9.1 2011-07-08 00:00 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_FileInit
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 OCI_File * OCI_FileInit
00046 (
00047     OCI_Connection *con,
00048     OCI_File      **pfile,
00049     OCILobLocator  *handle,
00050     ub4             type
00051 )
00052 {
00053     OCI_File *file = NULL;
00054     boolean res    = TRUE;
00055 
00056     OCI_CHECK(pfile == NULL, NULL);
00057 
00058     if (*pfile == NULL)
00059     {
00060         *pfile = (OCI_File *) OCI_MemAlloc(OCI_IPC_FILE, sizeof(*file), (size_t) 1, TRUE);
00061     }
00062 
00063     if (*pfile != NULL)
00064     {
00065         file = *pfile;
00066 
00067         file->type   = type;
00068         file->con    = con;
00069         file->handle = handle;
00070         file->offset = 1;
00071 
00072         /* reset file info */
00073 
00074         if (file->dir != NULL)
00075         {
00076             file->dir[0] = 0;
00077         }
00078 
00079         if (file->name != NULL)
00080         {
00081             file->name[0] = 0;
00082         }
00083 
00084         if (file->handle == NULL)
00085         {
00086             /* allocate handle for non fetched file (local file object) */
00087 
00088             file->hstate = OCI_OBJECT_ALLOCATED;
00089 
00090             res = (OCI_SUCCESS == OCI_DescriptorAlloc((dvoid *) file->con->env,
00091                                                       (dvoid **) (void *) &file->handle,
00092                                                       (ub4) OCI_DTYPE_LOB,
00093                                                       (size_t) 0, (dvoid **) NULL));
00094         }
00095         else if (file->hstate != OCI_OBJECT_ALLOCATED_ARRAY)
00096         {
00097             file->hstate = OCI_OBJECT_FETCHED_CLEAN;
00098         }
00099     }
00100     else
00101     {
00102         res = FALSE;
00103     }
00104 
00105     /* check for failure */
00106 
00107     if (res == FALSE)
00108     {
00109         OCI_FileFree(file);
00110         file = NULL;
00111     }
00112 
00113     return file;
00114 }
00115 
00116 /* --------------------------------------------------------------------------------------------- *
00117  * OCI_FileGetInfo
00118  * --------------------------------------------------------------------------------------------- */
00119 
00120 boolean OCI_FileGetInfo
00121 (
00122     OCI_File *file
00123 )
00124 {
00125     boolean res = TRUE;
00126     void *ostr1 = NULL;
00127     void *ostr2 = NULL;
00128     int osize1  = 0;
00129     int osize2  = 0;
00130     ub2 usize1  = 0;
00131     ub2 usize2  = 0;
00132 
00133     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00134 
00135     /* directory name */
00136 
00137     if (file->dir == NULL)
00138     {
00139         if (res == TRUE)
00140         {
00141             file->dir = (mtext *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
00142                                                (size_t) (OCI_SIZE_DIRECTORY + 1), TRUE);
00143 
00144             res = (file->dir != NULL);
00145         }
00146     }
00147     else
00148     {
00149         file->dir[0] = 0;
00150     }
00151 
00152     /* file name */
00153 
00154     if (file->name == NULL)
00155     {
00156         if (res == TRUE)
00157         {
00158             file->name = (mtext *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
00159                                                 (size_t)( OCI_SIZE_FILENAME + 1),
00160                                                 TRUE);
00161 
00162             res = (file->name != NULL);
00163         }
00164     }
00165     else
00166     {
00167         file->name[0] = 0;
00168     }
00169 
00170     /* retrieve name */
00171 
00172     if (res == TRUE)
00173     {
00174         osize1 = (int   ) OCI_SIZE_DIRECTORY  * (int) sizeof(mtext);
00175         ostr1  = (void *) OCI_GetInputMetaString(file->dir, &osize1);
00176 
00177         osize2 = (int   ) OCI_SIZE_FILENAME  * (int) sizeof(mtext);
00178         ostr2  = (void *) OCI_GetInputMetaString(file->name, &osize1);
00179 
00180         usize1 = (ub2) osize1;
00181         usize2 = (ub2) osize2;
00182 
00183         OCI_CALL2
00184         (
00185             res, file->con,
00186 
00187             OCILobFileGetName(file->con->env, file->con->err, file->handle,
00188                               (OraText *) ostr1, (ub2*) &usize1,
00189                               (OraText *) ostr2, (ub2*) &usize2)
00190         )
00191 
00192         osize1 = (int) usize1;
00193         osize2 = (int) usize2;
00194 
00195         OCI_GetOutputMetaString(ostr1, file->dir,  &osize1);
00196         OCI_GetOutputMetaString(ostr2, file->name, &osize2);
00197 
00198         OCI_ReleaseMetaString(ostr1);
00199         OCI_ReleaseMetaString(ostr2);
00200     }
00201 
00202     return res;
00203 }
00204 
00205 /* ********************************************************************************************* *
00206  *                            PUBLIC FUNCTIONS
00207  * ********************************************************************************************* */
00208 
00209 /* --------------------------------------------------------------------------------------------- *
00210  * OCI_FileCreate
00211  * --------------------------------------------------------------------------------------------- */
00212 
00213 OCI_File * OCI_API OCI_FileCreate
00214 (
00215     OCI_Connection *con,
00216     unsigned int    type
00217 )
00218 {
00219     OCI_File *file = NULL;
00220 
00221     OCI_CHECK_INITIALIZED(NULL);
00222 
00223     OCI_CHECK_PTR(OCI_IPC_CONNECTION, con, NULL);
00224 
00225     file = OCI_FileInit(con, &file, NULL, type);
00226 
00227     OCI_RESULT(file != NULL);
00228 
00229     return file;
00230 }
00231 
00232 /* --------------------------------------------------------------------------------------------- *
00233  * OCI_FileFree
00234  * --------------------------------------------------------------------------------------------- */
00235 
00236 boolean OCI_API OCI_FileFree
00237 (
00238     OCI_File *file
00239 )
00240 {
00241     boolean res = TRUE;
00242 
00243     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00244     OCI_CHECK_OBJECT_FETCHED(file, FALSE);
00245 
00246     OCI_FREE(file->dir);
00247     OCI_FREE(file->name);
00248 
00249     if (file->hstate == OCI_OBJECT_ALLOCATED)
00250     {
00251         OCI_DescriptorFree((dvoid *) file->handle, (ub4) OCI_DTYPE_LOB);
00252     }
00253 
00254     if (file->hstate != OCI_OBJECT_ALLOCATED_ARRAY)
00255     {
00256         OCI_FREE(file);
00257     }
00258 
00259     OCI_RESULT(res);
00260 
00261     return res;
00262 }
00263 
00264 /* --------------------------------------------------------------------------------------------- *
00265  * OCI_FileArrayCreate
00266  * --------------------------------------------------------------------------------------------- */
00267 
00268 OCI_File ** OCI_API OCI_FileArrayCreate
00269 (
00270     OCI_Connection *con,
00271     unsigned int    type,
00272     unsigned int    nbelem
00273 )
00274 {
00275     OCI_Array *arr   = NULL;
00276     OCI_File **files = NULL;
00277 
00278     arr = OCI_ArrayCreate(con, nbelem, OCI_CDT_FILE, type,
00279                           sizeof(OCILobLocator *), sizeof(OCI_File),
00280                           OCI_DTYPE_LOB, NULL);
00281 
00282     if (arr != NULL)
00283     {
00284         files = (OCI_File **) arr->tab_obj;
00285     }
00286 
00287     return files;
00288 }
00289 
00290 /* --------------------------------------------------------------------------------------------- *
00291  * OCI_FileArrayFree
00292  * --------------------------------------------------------------------------------------------- */
00293 
00294 boolean OCI_API OCI_FileArrayFree
00295 (
00296     OCI_File **files
00297 )
00298 {
00299     return OCI_ArrayFreeFromHandles((void **) files);
00300 }
00301 
00302 /* --------------------------------------------------------------------------------------------- *
00303  * OCI_FileSeek
00304  * --------------------------------------------------------------------------------------------- */
00305 
00306 boolean OCI_API OCI_FileSeek
00307 (
00308     OCI_File    *file,
00309     big_uint     offset,
00310     unsigned int mode
00311 )
00312 {
00313     boolean res   = TRUE;
00314     big_uint size = 0;
00315 
00316     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00317 
00318     size = OCI_FileGetSize(file);
00319 
00320     if ((mode == OCI_SEEK_CUR && (offset + file->offset-1) > size))
00321     {
00322         res = FALSE;
00323     }
00324     else if (mode == OCI_SEEK_SET)
00325     {
00326         file->offset = offset + 1;
00327     }
00328     else if (mode == OCI_SEEK_END)
00329     {
00330         file->offset = size-offset + 1;
00331     }
00332     else if (mode == OCI_SEEK_CUR)
00333     {
00334         file->offset += offset;
00335     }
00336     else
00337     {
00338         res = FALSE;
00339     }
00340 
00341     OCI_RESULT(res);
00342 
00343     return res;
00344 }
00345 
00346 /* --------------------------------------------------------------------------------------------- *
00347  * OCI_FileGetOffset
00348  * --------------------------------------------------------------------------------------------- */
00349 
00350 big_uint OCI_API OCI_FileGetOffset
00351 (
00352     OCI_File *file
00353 )
00354 {
00355     OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);
00356 
00357     OCI_RESULT(TRUE);
00358 
00359     return file->offset - 1;
00360 }
00361 
00362 /* --------------------------------------------------------------------------------------------- *
00363  * OCI_FileRead
00364  * --------------------------------------------------------------------------------------------- */
00365 
00366 unsigned int OCI_API OCI_FileRead
00367 (
00368     OCI_File    *file,
00369     void        *buffer,
00370     unsigned int len
00371 )
00372 {
00373     boolean res  = TRUE;
00374     ub4 size_in  = 0;
00375     ub4 size_out = 0;
00376 
00377     OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);
00378     OCI_CHECK_MIN(file->con, NULL, len, 1, 0);
00379 
00380     size_out = size_in = len;
00381 
00382 #ifdef OCI_LOB2_API_ENABLED
00383 
00384     if (OCILib.use_lob_ub8)
00385     {
00386         ub8 size_char = (ub8) len;
00387         ub8 size_byte = (ub8) size_in;
00388 
00389         OCI_CALL2
00390         (
00391             res, file->con,
00392 
00393             OCILobRead2(file->con->cxt, file->con->err,
00394                         file->handle, &size_byte,
00395                         &size_char, (ub8) file->offset,
00396                         buffer, (ub8) size_in,
00397                         (ub1) OCI_ONE_PIECE, (dvoid *) NULL,
00398                         NULL, (ub2) 0, (ub1) SQLCS_IMPLICIT)
00399         )
00400     }
00401 
00402     else
00403 
00404  #endif
00405 
00406     {
00407         ub4 offset = (ub4) file->offset;
00408 
00409         OCI_CALL2
00410         (
00411             res, file->con,
00412 
00413             OCILobRead(file->con->cxt, file->con->err,
00414                        file->handle,  &size_out, offset,
00415                        buffer, size_in, (dvoid *) NULL,
00416                        NULL, (ub2) 0, (ub1) SQLCS_IMPLICIT)
00417         )
00418     }
00419 
00420     if (res == TRUE)
00421     {
00422         file->offset += (big_uint) size_out;
00423     }
00424 
00425     OCI_RESULT(res);
00426 
00427     return size_out;
00428 }
00429 
00430 /* --------------------------------------------------------------------------------------------- *
00431  * OCI_FileGetType
00432  * --------------------------------------------------------------------------------------------- */
00433 
00434 unsigned int OCI_API OCI_FileGetType
00435 (
00436     OCI_File *file
00437 )
00438 {
00439     OCI_CHECK_PTR(OCI_IPC_FILE, file, OCI_UNKNOWN);
00440 
00441     OCI_RESULT(TRUE);
00442 
00443     return file->type;
00444 }
00445 
00446 /* --------------------------------------------------------------------------------------------- *
00447  * OCI_FileGetSize
00448  * --------------------------------------------------------------------------------------------- */
00449 
00450 big_uint OCI_API OCI_FileGetSize
00451 (
00452     OCI_File *file
00453 )
00454 {
00455     boolean res   = TRUE;
00456     big_uint size = 0;
00457 
00458     OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);
00459 
00460 #ifdef OCI_LOB2_API_ENABLED
00461 
00462     if (OCILib.use_lob_ub8)
00463     {
00464         OCI_CALL2
00465         (
00466             res, file->con,
00467 
00468             OCILobGetLength2(file->con->cxt, file->con->err, file->handle, (ub8 *) &size)
00469         )
00470 
00471     }
00472     else
00473 
00474 #endif
00475 
00476     {
00477         ub4 size32 = (ub4) size;
00478 
00479         OCI_CALL2
00480         (
00481             res, file->con,
00482 
00483             OCILobGetLength(file->con->cxt, file->con->err, file->handle, &size32)
00484         )
00485 
00486         size = (big_uint) size32;
00487     }
00488 
00489     OCI_RESULT(res);
00490 
00491     return size;
00492 }
00493 
00494 /* --------------------------------------------------------------------------------------------- *
00495  * OCI_LobFileExists
00496  * --------------------------------------------------------------------------------------------- */
00497 
00498 boolean OCI_API OCI_FileExists
00499 (
00500     OCI_File *file
00501 )
00502 {
00503     boolean res   = TRUE;
00504     boolean value = FALSE;
00505 
00506     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00507 
00508     OCI_CALL2
00509     (
00510         res, file->con,
00511 
00512         OCILobFileExists(file->con->cxt, file->con->err, file->handle, &value)
00513     )
00514 
00515     OCI_RESULT(res);
00516 
00517     return value;
00518 }
00519 
00520 /* --------------------------------------------------------------------------------------------- *
00521  * OCI_FileSetName
00522  * --------------------------------------------------------------------------------------------- */
00523 
00524 boolean OCI_API OCI_FileSetName
00525 (
00526     OCI_File    *file,
00527     const mtext *dir,
00528     const mtext *name
00529 )
00530 {
00531     void *ostr1 = NULL;
00532     void *ostr2 = NULL;
00533     int osize1  = -1;
00534     int osize2  = -1;
00535     boolean res = TRUE;
00536 
00537     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00538 
00539     ostr1 = OCI_GetInputMetaString(dir,  &osize1);
00540     ostr2 = OCI_GetInputMetaString(name, &osize2);
00541 
00542     OCI_CALL2
00543     (
00544         res, file->con,
00545 
00546         OCILobFileSetName(file->con->env, file->con->err,
00547                           &file->handle,
00548                           (OraText *) ostr1, (ub2) osize1,
00549                           (OraText *) ostr2, (ub2) osize2)
00550     )
00551 
00552     OCI_ReleaseMetaString(ostr1);
00553     OCI_ReleaseMetaString(ostr2);
00554 
00555     if (res == TRUE)
00556     {
00557         res = OCI_FileGetInfo(file);
00558     }
00559 
00560     OCI_RESULT(res);
00561 
00562     return res;
00563 }
00564 
00565 /* --------------------------------------------------------------------------------------------- *
00566  * OCI_FileGetDirectory
00567  * --------------------------------------------------------------------------------------------- */
00568 
00569 const mtext * OCI_API OCI_FileGetDirectory
00570 (
00571     OCI_File *file
00572 )
00573 {
00574     boolean res = TRUE;
00575 
00576     OCI_CHECK_PTR(OCI_IPC_FILE, file, NULL);
00577 
00578     if ((file->dir == NULL) || (file->dir[0] == 0))
00579     {
00580         res = OCI_FileGetInfo(file);
00581     }
00582 
00583     return file->dir;
00584 }
00585 
00586 /* --------------------------------------------------------------------------------------------- *
00587  * OCI_FileGetName
00588  * --------------------------------------------------------------------------------------------- */
00589 
00590 const mtext * OCI_API OCI_FileGetName
00591 (
00592     OCI_File *file
00593 )
00594 {
00595     boolean res = TRUE;
00596 
00597     OCI_CHECK_PTR(OCI_IPC_FILE, file, NULL);
00598 
00599     if ((file->name == NULL) || (file->name[0] == 0))
00600     {
00601         res = OCI_FileGetInfo(file);
00602     }
00603 
00604     OCI_RESULT(res);
00605 
00606     return file->name;
00607 }
00608 
00609 /* --------------------------------------------------------------------------------------------- *
00610  * OCI_FileOpen
00611  * --------------------------------------------------------------------------------------------- */
00612 
00613 boolean OCI_API OCI_FileOpen
00614 (
00615     OCI_File *file
00616 )
00617 {
00618     boolean res = TRUE;
00619 
00620     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00621 
00622     OCI_CALL2
00623     (
00624         res, file->con,
00625 
00626         OCILobFileOpen(file->con->cxt, file->con->err, file->handle, (ub1) OCI_LOB_READONLY)
00627     )
00628 
00629     if (res == TRUE)
00630     {
00631         file->con->nb_files++;
00632     }
00633 
00634     OCI_RESULT(res);
00635 
00636     return res;
00637 }
00638 
00639 /* --------------------------------------------------------------------------------------------- *
00640  * OCI_LobFileIsOpen
00641  * --------------------------------------------------------------------------------------------- */
00642 
00643 boolean OCI_API OCI_FileIsOpen
00644 (
00645     OCI_File *file
00646 )
00647 {
00648     boolean res   = TRUE;
00649     boolean value = FALSE;
00650 
00651     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00652 
00653     OCI_CALL2
00654     (
00655         res, file->con,
00656 
00657         OCILobFileIsOpen(file->con->cxt, file->con->err, file->handle, &value)
00658     )
00659 
00660     OCI_RESULT(res);
00661 
00662     return value;
00663 }
00664 
00665 /* --------------------------------------------------------------------------------------------- *
00666  * OCI_FileClose
00667  * --------------------------------------------------------------------------------------------- */
00668 
00669 boolean OCI_API OCI_FileClose
00670 (
00671     OCI_File *file
00672 )
00673 {
00674     boolean res = TRUE;
00675 
00676     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00677 
00678     OCI_CALL2
00679     (
00680         res, file->con,
00681 
00682         OCILobFileClose(file->con->cxt, file->con->err, file->handle)
00683     )
00684 
00685     if (res == TRUE)
00686     {
00687         file->con->nb_files--;
00688     }
00689 
00690     OCI_RESULT(res);
00691 
00692     return res;
00693 }
00694 
00695 /* --------------------------------------------------------------------------------------------- *
00696  * OCI_FileIsEqual
00697  * --------------------------------------------------------------------------------------------- */
00698 
00699 boolean OCI_API OCI_FileIsEqual
00700 (
00701     OCI_File *file,
00702     OCI_File *file2
00703 )
00704 {
00705     boolean res   = TRUE;
00706     boolean value = FALSE;
00707 
00708     OCI_CHECK_PTR(OCI_IPC_FILE, file,  FALSE);
00709     OCI_CHECK_PTR(OCI_IPC_FILE, file2, FALSE);
00710 
00711     OCI_CALL2
00712     (
00713         res, file->con,
00714 
00715         OCILobIsEqual(file->con->env, file->handle, file2->handle, &value)
00716     )
00717 
00718     OCI_RESULT(res);
00719 
00720     return value;
00721 }
00722 
00723 /* --------------------------------------------------------------------------------------------- *
00724  * OCI_FileAssign
00725  * --------------------------------------------------------------------------------------------- */
00726 
00727 boolean OCI_API OCI_FileAssign
00728 (
00729     OCI_File *file,
00730     OCI_File *file_src
00731 )
00732 {
00733     boolean res = TRUE;
00734 
00735     OCI_CHECK_PTR(OCI_IPC_FILE, file,     FALSE);
00736     OCI_CHECK_PTR(OCI_IPC_FILE, file_src, FALSE);
00737 
00738     if ((file->hstate == OCI_OBJECT_ALLOCATED) || (file->hstate == OCI_OBJECT_ALLOCATED_ARRAY))
00739     {
00740         OCI_CALL2
00741         (
00742             res, file->con,
00743 
00744             OCILobLocatorAssign(file->con->cxt, file->con->err, file_src->handle, &file->handle)
00745         )
00746     }
00747     else
00748     {
00749         OCI_CALL2
00750         (
00751             res, file->con,
00752 
00753             OCILobAssign(file->con->env, file->con->err, file_src->handle, &file->handle)
00754         )
00755     }
00756 
00757     if (res == TRUE)
00758     {
00759         OCI_FileGetInfo(file);
00760     }
00761 
00762     OCI_RESULT(res);
00763 
00764     return res;
00765 }