nfs-ganesha 1.4

fsal_truncate.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  * ------------- 
00024  */
00025 
00035 #ifdef HAVE_CONFIG_H
00036 #include "config.h"
00037 #endif
00038 
00039 #include "fsal.h"
00040 #include "fsal_internal.h"
00041 #include "fsal_convert.h"
00042 
00043 #include <unistd.h>
00044 #include <sys/types.h>
00045 
00069 fsal_status_t XFSFSAL_truncate(fsal_handle_t * p_filehandle, /* IN */
00070                                fsal_op_context_t * p_context,        /* IN */
00071                                fsal_size_t length,      /* IN */
00072                                fsal_file_t * file_descriptor,        /* Unused in this FSAL */
00073                                fsal_attrib_list_t * p_object_attributes /* [ IN/OUT ] */
00074     )
00075 {
00076 
00077   int rc, errsv;
00078   int fd;
00079   fsal_status_t st;
00080 
00081   /* sanity checks.
00082    * note : object_attributes is optional.
00083    */
00084   if(!p_filehandle || !p_context)
00085     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_truncate);
00086 
00087   /* get the path of the file and its handle */
00088   TakeTokenFSCall();
00089   st = fsal_internal_handle2fd(p_context, p_filehandle, &fd, O_RDWR);
00090   ReleaseTokenFSCall();
00091 
00092   if(FSAL_IS_ERROR(st))
00093     ReturnStatus(st, INDEX_FSAL_truncate);
00094 
00095   /* Executes the POSIX truncate operation */
00096 
00097   TakeTokenFSCall();
00098   rc = ftruncate(fd, length);
00099   errsv = errno;
00100   ReleaseTokenFSCall();
00101 
00102   close(fd);
00103 
00104   /* convert return code */
00105   if(rc)
00106     {
00107       if(errsv == ENOENT)
00108         Return(ERR_FSAL_STALE, errsv, INDEX_FSAL_truncate);
00109       else
00110         Return(posix2fsal_error(errsv), errsv, INDEX_FSAL_truncate);
00111     }
00112 
00113   /* Optionally retrieve attributes */
00114   if(p_object_attributes)
00115     {
00116 
00117       fsal_status_t st;
00118 
00119       st = XFSFSAL_getattrs(p_filehandle, p_context, p_object_attributes);
00120 
00121       if(FSAL_IS_ERROR(st))
00122         {
00123           FSAL_CLEAR_MASK(p_object_attributes->asked_attributes);
00124           FSAL_SET_MASK(p_object_attributes->asked_attributes, FSAL_ATTR_RDATTR_ERR);
00125         }
00126 
00127     }
00128 
00129   /* No error occurred */
00130   Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_truncate);
00131 
00132 }