Search code examples
carchive

Can't remove file in a folder with remove() path is ok


I have the following code, and I'm having a error while removing the file, but the file is closed and should be not problem, idk what I'm doing wrong, the path I also print and there is no character that should making a problem and persmissions are in order the plot twist is that the file is remove successfully but still the function delivers -1

while ((stdir = readdir(dirIn)) != NULL && iRet == R_OK) 
{
    if (strstr(stdir->d_name, tParametros.szArchEntrada) != NULL) 
    {
        
        memset (szRuta, 0x00,sizeof(szRuta));
        sprintf(szRuta,"%s/%s", tParametros.szPathEntrada, stdir->d_name);
        sprintf(szArchEntrada,"%s", stdir->d_name);
        strcpy(szArchProc, stdir->d_name);
    
        if((pfdEntrada = fopen(szRuta, "r")) == NULL)
        {
            fprintf(gpfdLogFile, "%s%sNo se pudo abrir el archivo de entrada[%s]\n", 
                LINEA, ctime(&tTimeNow), stdir->d_name);
            iRet = R_ARCH_ENTR;
        }
        else
        {           
            fprintf(gpfdLogFile, "%s%sSe abrio archivo de entrada: [%s]\n", 
                LINEA, ctime(&tTimeNow), stdir->d_name);
                
            bFlagOpenArchIn = B_TRUE;
            
            fprintf(gpfdLogFile, "%s%sSe abrio archivo tmp: [%s.tmp]\n", 
                LINEA, ctime(&tTimeNow), stdir->d_name);
                
            memset (szBuffer,0x00,sizeof(szBuffer));
            memset (szBufferAux,0x00,sizeof(szBufferAux));
            while((iRet == R_OK) && 
                (fgets(szBuffer, MAX_VAR_PARAM, pfdEntrada) != NULL))
            {       
                ldCount++;
                
                if(ldCount == 1)
                    continue;

                if(ldCount == 2)
                {
                    strcpy(szArchProc, cfnObtengoSecuencia(stdir->d_name));
        
                    /* Abrimos el archivo tmp */    
                    memset (szRuta, 0x00,sizeof(szRuta));
                    sprintf(szRuta,"%s/%s.tmp", tParametros.szPathTmp, szArchProc);
                    
                    if((pfdTmp = fopen(szRuta, "w")) == NULL)
                    {
                        fprintf(gpfdLogFile, "%s%sNo se pudo abrir el archivo tmp[%s]\n", 
                            LINEA, ctime(&tTimeNow), szArchProc);
                        iRet = R_ARCH_TMP;
                        bFlagOpenArchTmp = B_FALSE;
                    }
                    else    
                        bFlagOpenArchTmp = B_TRUE;
                }
                else if(ldCount2 == tParametros.iMaxRegistros)
                {
                    fclose(pfdTmp);
                    ldCount2 = 0;
                    strcpy(szArchProc, cfnObtengoSecuencia1(szArchProc));
                    
                    memset (szRuta, 0x00,sizeof(szRuta));
                    sprintf(szRuta,"%s/%s.tmp", tParametros.szPathTmp, szArchProc);
                    
                    if((pfdTmp = fopen(szRuta, "w")) == NULL)
                    {
                        fprintf(gpfdLogFile, "%s%sNo se pudo abrir el archivo tmp[%s]\n", 
                            LINEA, ctime(&tTimeNow), szArchProc);
                        iRet = R_ARCH_TMP;
                        bFlagOpenArchTmp = B_FALSE;
                    }
                    else    
                        bFlagOpenArchTmp = B_TRUE;
                }
                    
                if (bFlagOpenArchTmp == B_TRUE)
                {
                    strcpy(szBufferAux, ifnFormateo(szBuffer));
                    fputs(szBufferAux, pfdTmp);
                    ldCount2++;
                }
                
                memset (szBuffer,0x00,sizeof(szBuffer));
                memset (szBufferAux,0x00,sizeof(szBufferAux));
            }/*WHILE 2*/                
        }/* IF 2 */
    }/* IF 1 */
    fclose(pfdTmp);
}/*WHILE 1*/
    
/************TMP*********/
if ((dirTmp = opendir(tParametros.szPathTmp)) == NULL && iRet != R_OK)
{
    fprintf(gpfdLogFile, "%s%sERROR no se puede abrir el directorio de entrada[%s]\n", 
                LINEA, ctime(&tTimeNow), tParametros.szPathTmp);
    return R_DIR_ENTR;
}
else 
{
    bFlagOpenDirTmp = B_TRUE;
    fprintf(gpfdLogFile, "%s%sDirectorio [%s] abierto\n", 
                LINEA, ctime(&tTimeNow),  tParametros.szPathTmp);
}   


/* Abro el directorio de entrada */
while ((stdir = readdir(dirTmp)) != NULL && iRet == R_OK) 
{
    if (!strcmp(stdir->d_name,".") == 0 && !strcmp(stdir->d_name,"..") == 0
            && strstr(stdir->d_name, "tmp") != NULL) 
    {
        ldCant = 0;
        strcpy(szArchAux, stdir->d_name);
        memset (szRutaAux,  0x00,sizeof(szRutaAux));
        sprintf(szRutaAux,"%s%s", tParametros.szPathTmp, szArchAux);
        iRet = ifnObtenerCant(szArchAux, &ldCant);
        
        memset (szArchProc, 0x00,sizeof(szArchProc));
        strcpy(szArchProc, cfnObtenerNombre(szArchAux));
        
        memset (szRuta, 0x00,sizeof(szRuta));
        sprintf(szRuta,"%s%s", tParametros.szPathSalida, szArchProc);
        
        
        if (rename(szRutaAux, szRuta) != R_ERROR)
            fprintf(gpfdLogFile, "%s%sSe genera el archivo de salida [%s]\n", 
                LINEA, ctime(&tTimeNow), szRuta);
        else
        {
            fprintf(gpfdLogFile, "%s%sERROR al generar el archivo de salida [%s]\n", 
                LINEA, ctime(&tTimeNow), szRuta);
            iRet = R_ERROR;
        }
        
        if (remove(szRutaAux) == R_ERROR)
        {
            fprintf(gpfdLogFile, "%s%sERROR al eliminar el archivo tmp [%s]\n", 
                LINEA, ctime(&tTimeNow), szRutaAux);
            iRet = R_ERROR;
        }   
    
    }
}

#undef  R_OK
#define R_OK                    0
#undef  R_ERROR                 
#define R_ERROR                 -1

Solution

  • The file at szRutaAux will no longer exist after renaming it to szRuta with rename(szRutaAux, szRuta).