Internal_DirEnt* Internal_Dir_Read(_In_ Internal_Dir* self, _In_opt_z_ TChar *fileEndsWith)
{
    for(;;)
    {
#if defined(_MSC_VER)

        if (!self->firstTime)
        {
            if (_wfindnext(self->handle, &self->fileinfo) != 0)
                return NULL;
        }
        self->firstTime = 0;
        
        if( StringEndsWith(self->fileinfo.name, fileEndsWith) != 0 )
            continue;
        Wcslcpy(self->ent.name, self->fileinfo.name, PAL_MAX_PATH_SIZE);
        if( self->fileinfo.attrib & _A_SUBDIR )
            self->ent.isDir = 1;
        else
            self->ent.isDir = 0;
        
        return &self->ent;

#else
        struct dirent* p = readdir(self->dir);
        struct stat st;
        TChar filespec[PAL_MAX_PATH_SIZE];
        if (!p)
            return NULL;

        if( StringEndsWith(p->d_name, fileEndsWith) != 0 )
            continue;        

        Strlcpy(self->ent.name, p->d_name, PAL_MAX_PATH_SIZE);
        // Logic to detect Dir may not work on non-linux, non-windows platforms, in which case
        // We would need to use Isdir method.
        //self->ent.isDir = Isdir(self->ent.name);  
        if( Tcslcpy(filespec, self->dirName, PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE)
            return NULL;

        if( Tcslcat(filespec, "/", PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE)
            return NULL;

        if( Tcslcat(filespec, self->ent.name, PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE)
            return NULL;

        if (stat(filespec, &st) != 0)      
            return NULL;
        // self->ent.isDir = (p->d_type & DT_DIR) ? 1 : 0;
        self->ent.isDir = S_ISDIR(st.st_mode);
        return &self->ent;

#endif       
    }
}
Internal_Dir* Internal_Dir_Open(_In_z_ const TChar* path, NitsCallSite cs)
{
#if defined(_MSC_VER)

    Internal_Dir* dir;
    wchar_t filespec[PAL_MAX_PATH_SIZE];
    
    /* Allocate and zero-fill struct */
    dir = (Internal_Dir*)PAL_CallocCallsite(cs, 1, sizeof(Internal_Dir));
    if (!dir)
        return NULL;

    /* Build files spec */
    {
        if (Wcslcpy(filespec, path, PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE)
            return NULL;

        if (Wcslcat(filespec, L"/*", PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE)
            return NULL;
    }

    /* Find first file matching the file spec */
    dir->handle = _wfindfirst(filespec, &dir->fileinfo);
    if (dir->handle == -1)
    {
        PAL_Free(dir);
        return NULL;
    }

    /* Note that readdir() has not been called yet */
    dir->firstTime = 1;

    return dir;

#else

    Internal_Dir* self = (Internal_Dir*)PAL_CallocCallsite(cs, 1, sizeof(Internal_Dir));

    if (!self)
        return NULL;

    self->dir = opendir(path);
    if (!self->dir)
    {
        PAL_Free(self);
        return NULL;
    }
    memcpy(self->dirName, path, (Tcslen(path)+1) * sizeof(TChar));

    return self;

#endif          
}
예제 #3
0
파일: stub.c 프로젝트: HuaweiSwitch/OMI
// Nits Installation reads the nitsinj.dll name from registry key on windows
// On linux, we use the file based mechanism where we require that NitsInstalled file is present
// at specific location. The windows way of doing this was required so that we are fine with layermap tool
void LoadInjectorIfRequired()
{    
#ifdef _MSC_VER        
#define InjectorStrLength (sizeof(L"nitsinj.dll")/sizeof(wchar_t))

    HKEY key;
    wchar_t value[InjectorStrLength];    
    DWORD valueSize = sizeof(value);
    DWORD valueType = 0;
    DWORD queryError = 0;

    RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WSMAN", 0, KEY_QUERY_VALUE, &key);

    queryError = RegQueryValueEx(key, L"NitsInjector", NULL, &valueType, (BYTE *)&value, &valueSize);
    
    if((queryError == 0) && (InjectorStrLength*sizeof(wchar_t) == valueSize))
    {        
        PAL_Char injectorLibName[InjectorStrLength];
        value[InjectorStrLength - 1] = L'\0';
#ifdef CONFIG_ENABLE_WCHAR        
        Wcslcpy(injectorLibName, value, InjectorStrLength);
#else
        StrWcslcpy(injectorLibName, value, InjectorStrLength)
#endif        
        LoadInjector(injectorLibName);                
    }

    RegCloseKey(key);
#else
    FILE* fp = File_Open(CONFIG_TMPDIR "/NitsInstalled", "rb");
    if(fp)
    {
        PAL_Char injectorLibName[NAME_MAX];
        TcsStrlcpy(injectorLibName, CONFIG_LIBDIR "/libnitsinj.so", NAME_MAX);

        LoadInjector(injectorLibName);
        File_Close(fp);        
    }
#endif    
}