Пример #1
0
int handleFileFunctionRequest(int acceptHandle, char *inBuffer, char *outBuffer, int type, int msgSize) {
    int32_t ret = -1;
    int32_t index = -1;
    int32_t mode = -1;
    int32_t flags = -1;
    int32_t fd = -1;
    int32_t maxBytes = -1;

    int sendRet = 0;
    int namelen = 0;
    short extensionId = -1;

    switch (type) {

        case FILE_CTRL_OPEN:
            flags = getInt32(inBuffer, 12);
            mode = getInt32(inBuffer, 16);
            ret = mpOpen(inBuffer + 20, flags, mode);
            setInt32(outBuffer, 0, 4);
            setInt32(outBuffer, 4, ret);
            sendRet = sendN(acceptHandle, outBuffer, 8, 0);
            if (sendRet != 8) {
                fprintf(stderr, "tcpSvr: sendRet = %d != 8\n", sendRet);
                return -1;
            }
            break;

        case FILE_CTRL_CREATE:
            flags = getInt32(inBuffer, 12);
            ret = mpCreate(inBuffer + 16, flags);
            setInt32(outBuffer, 0, 4);
            setInt32(outBuffer, 4, ret);
            sendRet = sendN(acceptHandle, outBuffer, 8, 0);
            if (sendRet != 8) {
                fprintf(stderr, "tcpSvr: sendRet = %d != 8\n", sendRet);
                return -1;
            }
            break;

        case FILE_CTRL_CLOSE:
            if (msgSize != 12) {
                fprintf(stderr, "tcpSvr: invalid msgSize for mpClose = %d != 12\n", msgSize);
                return -1;
            }
            fd = getInt32(inBuffer, 12);
            if (fd < 1) {
                fprintf(stderr, "tcpSvr: invalid fd for mpRead = %d\n", fd);
                return -1;
            }
            ret = mpClose(fd);
            setInt32(outBuffer, 0, 4);
            setInt32(outBuffer, 4, ret);
            sendRet = sendN(acceptHandle, outBuffer, 8, 0);
            if (sendRet != 8) {
                fprintf(stderr, "tcpSvr: sendRet = %d != 8\n", sendRet);
                return -1;
            }
            break;

        case FILE_CTRL_READ:
            fd = getInt32(inBuffer, 12);
            if (fd < 1) {
                fprintf(stderr, "tcpSvr: invalid fd for mpRead = %d\n", fd);
                return -1;
            }
            maxBytes = getInt32(inBuffer, 16);
            if (maxBytes < 1 || maxBytes >= (BUFF_MAX - 8)) {
                fprintf(stderr, "tcpSvr: invalid maxBytes for mpRead = %d max = %d\n", maxBytes,(BUFF_MAX-8));
                return -1;
            }
            ret = mpRead(fd, outBuffer + 8, maxBytes);
            setInt32(outBuffer, 0, 4 + (ret > 0 ? ret : 0));
            setInt32(outBuffer, 4, ret);
            sendRet = sendN(acceptHandle, outBuffer, 8 + (ret > 0 ? ret : 0), 0);
            if (sendRet != 8 + (ret > 0 ? ret : 0)) {
                fprintf(stderr, "tcpSvr: sendRet = %d != 8 + (ret > 0?ret:0)\n", sendRet);
                return -1;
            }
            break;


        case FILE_CTRL_WRITE:
            fd = getInt32(inBuffer, 12);
            if (fd < 1) {
                fprintf(stderr, "tcpSvr: invalid fd for mpRead = %d\n", fd);
                return -1;
            }
            maxBytes = getInt32(inBuffer, 16);
            if (maxBytes < 1 || maxBytes >= (BUFF_MAX - 8)) {
                fprintf(stderr, "tcpSvr: invalid maxBytes for mpRead = %d max = %d\n", maxBytes,(BUFF_MAX-8));
                return -1;
            }
            ret = mpWrite(fd, inBuffer+20, maxBytes);
            setInt32(outBuffer, 0, 4 + (ret > 0 ? ret : 0));
            setInt32(outBuffer, 4, ret);
            sendRet = sendN(acceptHandle, outBuffer, 8 + (ret > 0 ? ret : 0), 0);
            if (sendRet != 8 + (ret > 0 ? ret : 0)) {
                fprintf(stderr, "tcpSvr: sendRet = %d != 8 + (ret > 0?ret:0)\n", sendRet);
                return -1;
            }
            break;

        default:
            fprintf(stderr, "tcpSvr: invalid file function type = %d\n", type);
            return -1;
    }
    return 0;
}
Пример #2
0
bool Controller::writeJob(char* path, char* job)
{
    bool rtn = false;
    int fd = this->MP_ERROR;
    int status = this->MP_ERROR;
    char filename[FILE_NAM_BUFFER_SIZE];  //should be big enough to hold a file name and DRAM drive name
    
    memset(filename, '\0', FILE_NAM_BUFFER_SIZE);  //not sure this is needed, strcpy below also does this.
    strcpy(filename, "MPRAM1:\\");
    strcat(filename, path);
    
    LOG_DEBUG("writeJob: %s", filename);
    
    // Remove the file, if it exists
    LOG_DEBUG("Trying to remove file, if it exists");
    status = mpRemove( filename );
    if (this->MP_ERROR == status)
    {
        LOG_WARN("Failed to remove job file: %s", filename);
    }
    
    
    // Create the file and write the job
    fd = mpCreate( filename, O_WRONLY );
    if (this->MP_ERROR != fd)
    {
        status = mpWrite( fd, job, strlen(job) );
        if ( this->MP_ERROR != status )
        {
            LOG_INFO("Successfully loaded file: %s, bytes written: %d", filename, status);
            rtn = true;
        }
        else
        {
            LOG_ERROR("Failed to write file: %s", filename);
            rtn = false;
        }
        
        
        // Checking file status
        /*
	    struct stat pStat;
	    status = mpFstat(fd, &pStat);
	    {
			LOG_DEBUG("mpFstat Complete");
			LOG_DEBUG("st_dev = %u", pStat.st_dev);
			LOG_DEBUG("st_ino = %u", pStat.st_ino);
			LOG_DEBUG("st_mode = %u", pStat.st_mode);
			LOG_DEBUG("st_nlink = %d", pStat.st_nlink);
			LOG_DEBUG("st_uid = %d", pStat.st_uid);
			LOG_DEBUG("st_gid = %d", pStat.st_gid);
			LOG_DEBUG("st_rdev = %u", pStat.st_rdev);
			LOG_DEBUG("st_size = %u", pStat.st_size);
			LOG_DEBUG("st_atime = %u", pStat.st_atime);
			LOG_DEBUG("st_mtime = %u", pStat.st_mtime);
			LOG_DEBUG("st_ctime = %u", pStat.st_ctime);
			LOG_DEBUG("st_blksize = %u", pStat.st_blksize);
			LOG_DEBUG("st_blocks = %u", pStat.st_blocks);
			LOG_DEBUG("st_attrib = %u", pStat.st_attrib);
		}
		*/
		
        // close file descriptor
	    status = mpClose(fd);
	    if (this->MP_ERROR == status)
	    {
	        LOG_WARN("Failed to close file: %s, ignoring failure", filename);
	    }
    }
    else
    {
        LOG_ERROR("Failed to create job file: %s", filename);
        rtn = false;
    }
    
    
    return rtn;
}