Example #1
0
int copy_file_to_file(const char *src, const char *dst)
{
    char buf[BUFFER_SIZE];
    int fdr, fdw, len;

    if ((fdr = open(src, O_RDONLY)) < 0)
	return -1;

    if ((fdw = OpenCreate(dst, O_WRONLY | O_TRUNC)) < 0) {
	close(fdr);
	return -1;
    }

    while (1) {
	len = read(fdr, buf, sizeof(buf));
	if (len <= 0)
	    break;
	write(fdw, buf, len);
	if (len < BUFFER_SIZE)
	    break;
    }

    close(fdr);
    close(fdw);
    return 0;
}
Example #2
0
/* append data from tail of src (starting point=off) to dst */
int
AppendTail(const char *src, const char *dst, int off)
{
    int fi, fo, bytes;
    char buf[8192];

    fi=open(src, O_RDONLY);
    if(fi<0) return -1;

    fo=OpenCreate(dst, O_WRONLY | O_APPEND);
    if(fo<0) {close(fi); return -1;}
    // flock(dst, LOCK_SH);
    
    if(off > 0)
	lseek(fi, (off_t)off, SEEK_SET);

    while((bytes=read(fi, buf, sizeof(buf)))>0)
    {
         write(fo, buf, bytes);
    }
    // flock(dst, LOCK_UN);
    close(fo);
    close(fi);
    return 0;  
}
Example #3
0
bool UniStdioFile::OpenCreateUtf8(LPCTSTR filename)
{
	if (!OpenCreate(filename))
		return false;
	SetUnicoding(ucr::UTF8);
	return true;

}
Example #4
0
/* @param[in,out] fpath input as dirname, output as filename */
static inline int
fhdr_stamp(char *fpath, fileheader_t *fh, int type)
{
    char       *ip = fpath;
    time4_t     dtime = time(0);
    struct tm   ptime;
    int         res = 0;

    if (access(fpath, X_OK | R_OK | W_OK))
	Mkdir(fpath);

    while (*(++ip));
    *ip++ = '/';

    switch (type) {
	case STAMP_FILE:
	    do {
		sprintf(ip, "M.%d.A.%3.3X", (int)(++dtime),
                        (unsigned int)(random() & 0xFFF));
	    } while ((res = OpenCreate(fpath, O_EXCL | O_WRONLY)) == -1 && 
                     errno == EEXIST);
	    break;
	case STAMP_DIR:
	    do {
		sprintf(ip, "D%X", (int)++dtime & 07777);
	    } while ((res = Mkdir(fpath)) == -1 && errno == EEXIST);
	    break;
	case STAMP_LINK:
	    do {
		sprintf(ip, "S%X", (int)++dtime);
	    } while ((res = symlink("temp", fpath)) == -1 && errno == EEXIST);
	    break;
	default:
	    // unknown
	    return -1;
	    break;
    }

    if (res == -1)
	return -1;

    if (type == STAMP_FILE)
	close(res);

    strlcpy(fh->filename, ip, sizeof(fh->filename));
    localtime4_r(&dtime, &ptime);
    snprintf(fh->date, sizeof(fh->date), "%2d/%02d", ptime.tm_mon + 1, ptime.tm_mday);

    return 0;
}
Example #5
0
int tune(int num) {
    int i, j, fin, fout;
    userec_t u;
    
    if((fin = open(FN_PASSWD, O_RDONLY)) == -1) {
	perror(FN_PASSWD);
	return 1;
    }
    if(flock(fin, LOCK_EX)) {
	printf("Lock failed!\n");
	return 1;
    }
    if((fout = OpenCreate(FN_PASSWD ".tune" , O_WRONLY)) == -1) {
	perror(FN_PASSWD ".tune");
	flock(fin, LOCK_UN);
	close(fin);
	return 1;
    }
    
    for(i = j = 0; i < num; i++) {
	read(fin, &u, sizeof(u));
	if(u.userid[0]) {
	    if(j == MAX_USERS) {
		printf("MAX_USERS is too small!\n");
		close(fout);
		unlink(FN_PASSWD ".tune");
		flock(fin, LOCK_UN);
		close(fin);
		return 1;
	    }
	    write(fout, &u, sizeof(u));
	    j++;
	}
    }
    for(memset(&u, 0, sizeof(u)); j < MAX_USERS; j++) {
	write(fout, &u, sizeof(u));
    }
    close(fout);
    
    /* backup */
    unlink(FN_PASSWD "~");
    link(FN_PASSWD, FN_PASSWD "~");
    unlink(FN_PASSWD);
    link(FN_PASSWD ".tune", FN_PASSWD);
    unlink(FN_PASSWD ".tune");
    
    flock(fin, LOCK_UN);
    close(fin);
    return 0;
}
Example #6
0
/* rocker.011022: 避免lock檔開啟時不正常斷線,造成永久lock */
int
force_open(const char *fname)
{
    int             fd;
    time4_t          expire;

    expire = now - 3600;	/* lock 存在超過一個小時就是有問題! */

    if (dasht(fname) > expire)
	return -1;
    unlink(fname);
    fd = OpenCreate(fname, O_WRONLY | O_TRUNC);

    return fd;
}
Example #7
0
int
Copy(const char *src, const char *dst)
{
    int fi, fo, bytes;
    char buf[8192];
    fi=open(src, O_RDONLY);
    if(fi<0) return -1;
    fo=OpenCreate(dst, O_WRONLY | O_TRUNC);
    if(fo<0) {close(fi); return -1;}
    while((bytes=read(fi, buf, sizeof(buf)))>0)
         write(fo, buf, bytes);
    close(fo);
    close(fi);
    return 0;  
}
Example #8
0
BLAPI_PROTO
bls_save(lua_State *L)
{
    int n = lua_gettop(L), fd = -1;
    int limit = 0, slen = 0;
    const char *s = NULL, *cat = NULL;
    char fn[PATHLEN] = "", ret = 0;

    if (blrt.iocounter >= BLSCONF_MAXIO)
    {
        lua_pushboolean(L, 0);
        return 1;
    }

    if (n != 2) {
        lua_pushboolean(L, 0);
        return 1;
    }

    cat = lua_tostring(L, 1); // category
    s   = lua_tostring(L, 2); // data
    limit = bls_getlimit(cat);

    if (!cat || !s || limit < 1)
    {
        lua_pushboolean(L, 0);
        return 1;
    }

    slen = lua_objlen(L, 2);
    if (slen >= limit) slen = limit;

    blrt.iocounter++;
    // write file!
    if (bls_setfn(fn, sizeof(fn), cat))
    {
        fd = OpenCreate(fn, O_WRONLY);
        if (fd >= 0)
        {
            write(fd, s, slen);
            close(fd);
            ret = 1;
        }
    }

    lua_pushboolean(L, ret);
    return 1;
}
Example #9
0
int
substitute_record(const char *fpath, const void *rptr, size_t size, int id)
{
    int fd;
    off_t offset = size * (id - 1);

    if (id < 1 || (fd = OpenCreate(fpath, O_WRONLY)) == -1)
       return -1;

    lseek(fd, offset, SEEK_SET);
    PttLock(fd, offset, size, F_WRLCK);
    write(fd, rptr, size);
    PttLock(fd, offset, size, F_UNLCK);
    close(fd);

    return 0;
}
Example #10
0
int
substitute_record2(const char *fpath, const void *srcptr, const void *destptr,
                   size_t size, int id, record_callback_t cb_can_substitue)
{
    int fd;
    int err = 0;
    off_t offset = size * (id - 1);
    void *p = NULL;

    if (id < 1 || (fd = OpenCreate(fpath, O_RDWR)) == -1)
	return -1;
    if (lseek(fd, offset, SEEK_SET) != offset ||
        (cb_can_substitue && !(p = malloc(size)))) {
        close(fd);
        return -1;
    }
    PttLock(fd, offset, size, F_WRLCK);
    while(cb_can_substitue) {
        err = -1;
        if (read(fd, p, size) != (ssize_t)size)
            break;
        if (!cb_can_substitue(p, srcptr))
            break;
        if (lseek(fd, offset, SEEK_SET) != offset)
            break;
        err = 0;
        break;
    }
    if (err == 0) {
        if (write(fd, destptr, size) != (ssize_t)size)
            err = -1;
    }
    PttLock(fd, offset, size, F_UNLCK);
    close(fd);
    if (p) free(p);

    return err;
}
Example #11
0
int
append_record(const char *fpath, const void *record, size_t size)
{
    int fd, index = -2, fsize = 0;

    if ((fd = OpenCreate(fpath, O_WRONLY)) == -1)
	return -1;

    flock(fd, LOCK_EX);

    if ((fsize = lseek(fd, 0, SEEK_END)) < 0)
	goto out;

    index = fsize / size;
    lseek(fd, index * size, SEEK_SET);  // avoid offset

    write(fd, record, size);

out:
    flock(fd, LOCK_UN);
    close(fd);

    return index + 1;
}
Example #12
0
int
delete_range(const char *fpath, int id1, int id2)
{
    fileheader_t    fhdr;
    nol_t           my;
    char            fullpath[STRLEN], *t;
    int             fdr, fdw, fd;
    int             count, dcount=0;

    nolfilename(&my, fpath);

    if ((fd = OpenCreate(my.lockfn, O_RDWR | O_APPEND)) == -1)
	return -1;

    flock(fd, LOCK_EX);

    if ((fdr = open(fpath, O_RDONLY)) == -1) {
	flock(fd, LOCK_UN);
	close(fd);
	return -1;
    }
    if (
	((fdw = OpenCreate(my.newfn, O_WRONLY | O_EXCL)) == -1) &&
	((fdw = force_open(my.newfn)) == -1)) {
	close(fdr);
	flock(fd, LOCK_UN);
	close(fd);
	return -1;
    }
    count = 1;
    strlcpy(fullpath, fpath, sizeof(fullpath));
    t = strrchr(fullpath, '/');
    assert(t);
    t++;

    while (read(fdr, &fhdr, sizeof(fileheader_t)) == sizeof(fileheader_t)) {
	strcpy(t, fhdr.filename);

	/* rocker.011018: add new tag delete */
	if (
	    (fhdr.filemode & FILE_MARKED) ||	/* 標記 */
	    ((fhdr.filemode & FILE_DIGEST) && (currstat != RMAIL) )||	
	    /* 文摘 , FILE_DIGEST is used as REPLIED in mail menu.*/
	    (id1 && (count < id1 || count > id2)) ||	/* range */
	    (!id1 && !FindTaggedItem(&fhdr))) {	/* TagList */
	    if ((safewrite(fdw, &fhdr, sizeof(fileheader_t)) == -1)) {
		close(fdr);
		close(fdw);
		unlink(my.newfn);
		flock(fd, LOCK_UN);
		close(fd);
		return -1;
	    } 
	} else {
            unlink(fullpath);
            dcount++;
            RemoveTagItem(&fhdr);
	}
	++count;
    }
    close(fdr);
    close(fdw);
    if (Rename(fpath, my.oldfn) == -1 || Rename(my.newfn, fpath) == -1) {
	flock(fd, LOCK_UN);
	close(fd);
	return -1;
    }
    flock(fd, LOCK_UN);
    close(fd);
    return dcount;
}
Example #13
0
int main(int argc, char **argv)
{
    int k;
    
    if(argc < 2) {
	fprintf(stderr, "Usage: %s <path1> [<path2> ...]\n", argv[0]);
	return 1;
    }
    
    for(k = 1; k < argc; k++) {
	int fdir, count, total;
	char *ptr, path[MAXPATHLEN];
	struct dirent **dirlist;
	
	sprintf(path, "%s/.DIR", argv[k]);
	if((fdir = OpenCreate(path, O_WRONLY | O_TRUNC)) == -1) {
	    perror(path);
	    continue;
	}
	
	if((total = scandir(argv[k], &dirlist, dirselect, mysort)) == -1) {
	    fprintf(stderr, "scandir failed!\n");
	    close(fdir);
	    continue;
	}
	
	ptr = strrchr(path, '.');
	for(count = 0; count < total; count++) {
	    FILE *fp;
	    struct stat st;
	    
	    strcpy(ptr, dirlist[count]->d_name);
	    if(stat(path, &st) == 0 && st.st_size > 0 &&
	       (fp = fopen(path, "r")) != NULL) {
		char buf[512];
		time4_t filetime;
		fileheader_t fhdr;
		
		memset(&fhdr, 0, sizeof(fhdr));
		/* set file name */
		strcpy(fhdr.filename, dirlist[count]->d_name);
		
		/* set file time */
		filetime = atoi(dirlist[count]->d_name + 2);
		if(filetime > 740000000) {
		    struct tm *ptime = localtime4(&filetime);
		    sprintf(fhdr.date, "%2d/%02d", ptime->tm_mon + 1,
			    ptime->tm_mday);
		} else
		    strcpy(fhdr.date, "     ");
		
		/* set file mode */
		fhdr.filemode = FILE_READ;
		
		/* set article owner */
		fgets(buf, sizeof(buf), fp);
		if(strncmp(buf, "作者: ", 6) == 0 ||
		   strncmp(buf, "發信人: ", 8) == 0) {
		    int i, j;
		    
		    for(i = 5; buf[i] != ' '; i++);
		    for(; buf[i] == ' '; i++);
		    for(j = i + 1; buf[j] != ' '; j++);
		    j -= i;
		    if(j > IDLEN + 1)
			j = IDLEN + 1;
		    strncpy(fhdr.owner, buf + i, j);
		    fhdr.owner[IDLEN + 1] = '\0';
		    strtok(fhdr.owner, " .@\t\n\r");
		    if(strtok(NULL, " .@\t\n\r"))
			strcat(fhdr.owner, ".");
		    
		    /* set article title */
		    while(fgets(buf, sizeof(buf), fp))
			if(strncmp(buf, "標題: ", 6) == 0 ||
			   strncmp(buf, "標  題: ", 8) == 0) {
			    for( i = 5 ; buf[i] != ' ' ; ++i )
				;
			    for( ; buf[i] == ' ' ; ++i )
				;
			    strtok(buf + i - 1, "\n");
			    strncpy(fhdr.title, buf + i, TTLEN);
			    fhdr.title[TTLEN] = '\0';
			    for( i = 0 ; fhdr.title[i] != 0 ; ++i )
				if( fhdr.title[i] == '\e' ||
				    fhdr.title[i] == '\b' )
				    fhdr.title[i] = ' ';
			    printf("%s, owner: %s, title: %s\n", path, fhdr.owner, fhdr.title);
			    break;
			}
		} else if(strncmp(buf, "☉ 歡迎光臨", 11) == 0) {
		    strcpy(fhdr.title, "會議記錄");
		} else if(strncmp(buf, "\33[1;33;46m★", 12) == 0||
			  strncmp(buf, "To", 2) == 0) {
		    strcpy(fhdr.title, "熱線記錄");
		}
//		if(!fhdr.title[0])
//		    strcpy(fhdr.title, dirlist[count]->d_name);
		fclose(fp);
		write(fdir, &fhdr, sizeof(fhdr));
	    }
	}
	close(fdir);
	for(total--; total >= 0; total--)
	    free(dirlist[total]);
	free(dirlist);
    }
    return 0;
}