示例#1
0
int EDirectory::FmChDir(const char *Name) {
    char Dir[256];
    char CName[256] = "";

    if (strcmp(Name, SSLASH) == 0) {
        JustRoot(Path, Dir, sizeof(Dir));
    } else if (strcmp(Name, SDOT SDOT) == 0) {
        Slash(Path, 0);
        JustFileName(Path, CName, sizeof(CName));
        JustDirectory(Path, Dir, sizeof(Dir));
    } else {
        JustDirectory(Path, Dir, sizeof(Dir));
        Slash(Dir, 1);
        strlcat(Dir, Name, sizeof(Dir));
    }
    Slash(Dir, 1);
    free(Path);
    Path = strdup(Dir);
    Row = 0;
    RescanList();
    if (CName[0] != 0) {
        for (int i = 0; i < FCount; i++) {
            if (filecmp(Files[i]->Name(), CName) == 0)
            {
                Row = i;
                break;
            }
        }
    }
    UpdateTitle();
    return 1;
}
示例#2
0
文件: view.cpp 项目: mongrelx/efte
int EView::OpenDir(char *Path) {
    char XPath[MAXPATH];
    EDirectory *dir = 0;

    if (ExpandPath(Path, XPath, sizeof(XPath)) == -1)
        return 0;
    {
        EModel *x = Model;
        while (x) {
            if (x->GetContext() == CONTEXT_DIRECTORY) {
                if (filecmp(((EDirectory *)x)->Path, XPath) == 0) {
                    dir = (EDirectory *)x;
                    break;
                }
            }
            x = x->Next;
            if (x == Model)
                break;
        }
    }
    if (dir == 0)
        dir = new EDirectory(0, &ActiveModel, XPath);
    SelectModel(dir);
    return 1;
}
示例#3
0
int main(int argc, char *argv[]) {
	FILE *fp1, *fp2;
	void filecmp(FILE *, FILE *);

	if (argc == 1)
	{
		/* no args; copy standard input */
		printf("Please provide two files");
		return 1;
	}
	else
		while (--argc > 0) {
			if ((fp1 = fopen(argv[1], "r")) == NULL) {
				printf("cat: can't open %s\n", *argv);
				return 1;
			}

			if ((fp2 = fopen(argv[2], "r")) == NULL) {
				printf("cat: can't open %s\n", *argv);
				return 1;
			}
			filecmp(fp1, fp2);
			fclose(fp1);
			fclose(fp2);
		}

	return 0;
}
示例#4
0
文件: e_mark.cpp 项目: mongrelx/efte
int EMarkIndex::retrieveForBuffer(EBuffer *aBuffer) {
    for (int n = 0; n < markCount; n++)
        if (marks[n]->getBuffer() == 0 &&
                filecmp(aBuffer->FileName, marks[n]->getFileName()) == 0) {
            if (marks[n]->setBuffer(aBuffer) == 0)
                return 0;
        }
    return 1;
}
示例#5
0
void ESvnBase::FindFileLines (EBuffer *B) {
    char path[MAXPATH];
    char *pos;
    strcpy (path,Directory);Slash (path,1);pos=path+strlen (path);
    for (int i=0;i<LineCount;i++)
        if (Lines[i]->Buf==0&&Lines[i]->File!=0) {
            strcpy (pos,Lines[i]->File);
            if (filecmp (B->FileName,path)==0) {
                AssignBuffer (B,i);
            }
        }
}
示例#6
0
int _LNK_CONV FileNameCmp(const void *a, const void *b) {
    const FileInfo *A = *(const FileInfo **)a;
    const FileInfo *B = *(const FileInfo **)b;

    if (!(A->Type() == fiDIRECTORY) && (B->Type() == fiDIRECTORY))
        return 1;

    if ((A->Type() == fiDIRECTORY) && !(B->Type() == fiDIRECTORY))
        return -1;

    return filecmp(A->Name(), B->Name());
}
示例#7
0
int EMark::removeBuffer(EBuffer *aBuffer) {
    assert(aBuffer != 0);
    if (Buffer == 0 || Buffer != aBuffer)
        return 0;
    assert(filecmp(aBuffer->FileName, FileName) == 0);

    if (Buffer->GetBookmark(Name, Point) == 0)
        return 0;
    if (Buffer->RemoveBookmark(Name) == 0)
        return 0;
    
    Buffer = 0;
    return 1;
}
示例#8
0
int EMark::setBuffer(EBuffer *aBuffer) {
    assert(aBuffer != 0);
    assert(filecmp(aBuffer->FileName, FileName) == 0);

    if (Point.Row >= aBuffer->RCount)
        Point.Row = aBuffer->RCount - 1;
    if (Point.Row < 0)
        Point.Row = 0;

    if (aBuffer->PlaceBookmark(Name, Point) == 1) {
        Buffer = aBuffer;
        return 1;
    }
    return 0;
}
示例#9
0
EBuffer *FindFile(char *FileName) {
    EModel *M;
    EBuffer *B;
    
    M = ActiveModel;
    while (M) {
        if (M->GetContext() == CONTEXT_FILE) {
            B = (EBuffer *)M;
            if (filecmp(B->FileName, FileName) == 0) { return B; }
        }
        M = M->Next;
        if (M == ActiveModel) break;
    }
    return 0;
}
示例#10
0
int EDirectory::RescanDir() {
    char CName[256] = "";

    if (Row >= 0 && Row < FCount)
        strcpy(CName, Files[Row]->Name());
    Row = 0;
    RescanList();
    if (CName[0] != 0) {
        for (int i = 0; i < FCount; i++) {
            if (filecmp(Files[i]->Name(), CName) == 0) {
                Row = i;
                break;
            }
        }
    }
    return 1;
}
示例#11
0
文件: writer.c 项目: deglingo/los
/* writer_write:
 */
void writer_write ( Writer *writer )
{
  gchar *tmpname;
  FILE *f;
  /* CL_DEBUG("WRITE: '%s'", writer->fname); */
  tmpname = g_strdup_printf("%s.tmp", writer->fname);
  if (!(f = fopen(tmpname, "w")))
    CL_ERROR("could not open '%s' : %s", tmpname, strerror(errno));
  wnode_dump(writer->root, f);
  fclose(f);
  if (!g_file_test(writer->fname, G_FILE_TEST_EXISTS)) {
    CL_DEBUG(" C %s", writer->fname);
    rename(tmpname, writer->fname);
  } else if (filecmp(tmpname, writer->fname)) {
    CL_DEBUG(" U %s", writer->fname);
    rename(tmpname, writer->fname);
  } else {
    /* CL_DEBUG(" - %s", writer->fname); */
    unlink(tmpname);
  }
}
示例#12
0
文件: input.c 项目: JWasm/JWasm
/* check if a file is in the array of known files.
 * if no, store the file at the array's end.
 * returns array index.
 * used for the main source and all INCLUDEd files.
 * the array is stored in the standard C heap!
 * the filenames are stored in the "local" heap.
 */
static unsigned AddFile( char const *fname )
/******************************************/
{
    unsigned    index;

    DebugMsg1(("AddFile(%s) enter, curr index=%u\n", fname, ModuleInfo.g.cnt_fnames ));
    for( index = 0; index < ModuleInfo.g.cnt_fnames; index++ ) {
        if( filecmp( fname, ModuleInfo.g.FNames[index].fname ) == 0 ) {
#ifdef DEBUG_OUT
            if ( Parse_Pass == PASS_1 )
                ModuleInfo.g.FNames[index].included++;
#endif
            return( index );
        }
    }

    if ( ( index % 64 ) == 0 ) {
        struct fname_item *newfn;
        newfn = (struct fname_item *)MemAlloc( ( index + 64 ) * sizeof( struct fname_item ) );
        if ( ModuleInfo.g.FNames ) {
            memcpy( newfn, ModuleInfo.g.FNames, index * sizeof( struct fname_item ) );
            MemFree( ModuleInfo.g.FNames );
        }
        ModuleInfo.g.FNames = newfn;
    }
    ModuleInfo.g.cnt_fnames++;

    /* v2.11: use name directly - allows COFF .file entries with relative paths */
    //_splitpath( fname, NULL, NULL, name, ext );

    ModuleInfo.g.FNames[index].fname = (char *)LclAlloc( strlen( fname ) + 1 );
    strcpy( ModuleInfo.g.FNames[index].fname, fname );
    /* v2.11: field fullname removed */
    //ModuleInfo.g.FNames[index].fullname = (char *)LclAlloc( strlen( fullname ) + 1 );
    //strcpy( ModuleInfo.g.FNames[index].fullname, fullname );
    DebugCmd( ModuleInfo.g.FNames[index].included = 1 );
    return( index );
}
示例#13
0
文件: o_svn.cpp 项目: mongrelx/efte
char ESvn::GetFileStatus(char *file) {
    // Search backward, file can be present several times (old messages)
    for (int i = LineCount - 1;i >= 0;i--)
        if (Lines[i]->File && filecmp(Lines[i]->File, file) == 0) return Lines[i]->Msg[0];
    return 0;
}
示例#14
0
void
cs_restore(
	boolean strip_slashes,	/* should leading slash be stripped? */
	int fcount,		/* number of filename patterns in flist */
	char **flist)		/* filename patterns to select for restore  */
{
	int		file_data = 0;
	int		file_data_read;
	csd_fhdr_t file_hdr;
	char	name[MAXPATHLEN + 1];
	/* name to compare for selective rstr */
	char	*name_compare = &name[0];
	char	slink[MAXPATHLEN + 1];
	char	*save_path = (char *)NULL; /* Prev path opened for SAM_fd */
	char	*last_path = NULL;	/* Last compared path */
	char	*slash_loc;		/* Last loc of a '/' in filename */
	char	unused;			/* A safe value for slash_loc */
	int		namelen;
	int		skipping;
	struct sam_perm_inode perm_inode;
	struct sam_stat sb;
	struct sam_vsn_section *vsnp;
	void	*data;
	int		n_acls;
	aclent_t *aclp;

	/*
	 *	Read the dump file
	 */
	while (csd_read_header(&file_hdr) > 0) {
		namelen = file_hdr.namelen;
		csd_read(name, namelen, &perm_inode);
		data = NULL;
		vsnp = NULL;
		aclp = NULL;
		n_acls = 0;
		if (file_hdr.flags & CSD_FH_DATA) {
			BUMP_STAT(data_files);
			file_data_read = 0;
			file_data++;
		} else {
			file_data_read = file_data = 0;
		}

		if (S_ISREQ(perm_inode.di.mode)) {
			SamMalloc(data, perm_inode.di.psize.rmfile);
		}

		csd_read_next(&perm_inode, &vsnp, slink, data, &n_acls, &aclp);

		if (dont_process_this_entry) {   /* if problem reading inode */
			BUMP_STAT(errors);
			goto skip_file;
		}

		/* Skip privileged files except root */

		if (SAM_PRIVILEGE_INO(perm_inode.di.version,
		    perm_inode.di.id.ino)) {
			if (perm_inode.di.id.ino != SAM_ROOT_INO) {
				goto skip_file;
			}
		}

		/* select subset of file names to restore. */

		name_compare = &name[0];
		if (fcount != 0) {	/* Search the file name list */
			int i;

			for (i = 0; i < fcount; i++) {
				/*
				 * If stripping first slash to make relative
				 * path and dump file name begins with '/' and
				 * select file name does not being with '/',
				 * then move name compare ahead past first '/'
				 */
				if (strip_slashes && ('/' == *name) &&
				    ('/' != *flist[i])) {
					name_compare = &name[1];
				}

				/*
				 * filecmp returns:
				 *	0 for no match
				 *	1 exact match,
				 *	2 name prefix of flist[i],
				 *	3 flist[i] prefix of name.
				 */
				if (filecmp(name_compare, flist[i]) != 0) {
					break;
				}
			}

			if (i >= fcount) {
				if (S_ISSEGI(&perm_inode.di)) {
					skipping = 1;
					goto skip_seg_file;
				}
				goto skip_file;
			}
		}

		/*
		 * If stripping first slash to make relative path and
		 *	dump file name begins with '/'
		 * then move location ahead past first '/'
		 */
		if (strip_slashes && ('/' == *name)) {
			name_compare = &name[1];
		}

		/*
		 * Make sure that we don't restore into a non-SAM-FS
		 * filesystem.
		 * If relative path to be restored, check current directory for
		 * being in a SAM-FS filesystem.  If absolute path to be
		 * restored, search backwards through path for a viable
		 * SAM-FS path by calling sam_stat() and checking the
		 * directory attributes.
		 */
		if ((strip_slashes && ('/' == *name)) || ('/' != *name)) {
			char *check_name = ".";
			char *next_name;

			if ((save_path != (char *)NULL) &&
			    (strcmp(check_name, save_path) != 0)) {
				check_samfs_fs();
				free(save_path);
				(void) close(SAM_fd);
				save_path = strdup(check_name);
				SAM_fd = open_samfs(save_path);
			} else if (save_path == (char *)NULL) {
				check_samfs_fs();
				save_path = strdup(check_name);
				SAM_fd = open_samfs(save_path);
			}

			/*
			 * Check if last path matches this path. Otherwise,
			 * If subpath does not yet exist, make directories
			 * as needed.
			 */
			if (last_path != NULL) {
				char *name_cmp;
				int path_len;

				path_len = strlen(last_path);
				/* Copy filename */
				name_cmp = strdup(name_compare);
				if ((slash_loc = strrchr(name_cmp, '/')) !=
				    NULL) {
					*slash_loc = '\0';
					if ((path_len == strlen(name_cmp)) &&
					    (last_path[path_len-1] ==
					    name_cmp[path_len-1]) &&
					    (bcmp(last_path, name_cmp,
					    path_len) == 0)) {

						free(name_cmp);
						goto restore_file;
					}
				}
				free(name_cmp);
			}
			if (last_path != NULL) {
				free(last_path);
				last_path = NULL;
			}
			/* Save path for check above */
			last_path = strdup(name_compare);
			if ((slash_loc = strrchr(last_path, '/')) != NULL) {
				*slash_loc = '\0';
			} else {
				free(last_path);
				last_path = NULL;
			}
			/* Copy filename */
			next_name = check_name = strdup(name_compare);
			while ((slash_loc = strchr(next_name, '/')) !=
			    (char *)NULL) {
				*slash_loc = '\0';

				if (mkdir(check_name,
				    S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
					if (EEXIST == errno) {
						*slash_loc = '/';
						next_name = slash_loc + 1;
						continue;
					}

					BUMP_STAT(errors);
					error(1, errno,
					    catgets(catfd, SET, 222,
					    "%s: Cannot mkdir()"),
					    check_name);
					break;
				}

				*slash_loc = '/';
				next_name = slash_loc + 1;
			}

			free(check_name);
		} else {
			/*
			 * Search through absolute path from end for
			 * SAM-FS file system
			 */
			/* Copy filename */
			char *check_name = strdup(name_compare);

			slash_loc = &unused;

			do {
				*slash_loc = '\0';
				/*
				 * Make sure we don't restore into a
				 * non-SAM-FS filesystem
				 * by calling sam_stat() and checking the
				 * directory attributes.
				 */
				if (sam_stat(check_name, &sb,
				    sizeof (sb)) == 0 &&
				    SS_ISSAMFS(sb.attr)) {
					if ((save_path != (char *)NULL) &&
					    (strcmp(check_name,
					    save_path) != 0)) {
						free(save_path);
						(void) close(SAM_fd);
						save_path = strdup(check_name);
						SAM_fd = open_samfs(save_path);
					} else if (save_path == (char *)NULL) {
						save_path = strdup(check_name);
						SAM_fd = open_samfs(save_path);
					}

					break;
				}
				slash_loc = strrchr(check_name, '/');
			} while (slash_loc != (char *)NULL);


			/*
			 * If no SAM-FS file system found in path, issue error
			 */
			if ((char *)NULL == slash_loc) {
				BUMP_STAT(errors);
				BUMP_STAT(errors_dir);
				error(1, 0,
				    catgets(catfd, SET, 259,
				    "%s: Not a SAM-FS file."),
				    name_compare);
			}

			free(check_name);
		}

restore_file:
		if (verbose) {
			sam_ls(name_compare, &perm_inode, NULL);
		}
		/* If not already offline */
		if (!perm_inode.di.status.b.offline) {
			/* If archived */
			if (perm_inode.di.arch_status) {
				print_reslog(log_st, name_compare,
				    &perm_inode, "online");
			}
		} else if (perm_inode.di.status.b.pextents) {
			print_reslog(log_st, name_compare, &perm_inode,
			    "partial");
		}

		/*
		 * if segment index, we have to skip the data segment inodes
		 * before we can get to the dumped data.
		 */
		if (!(S_ISSEGI(&perm_inode.di) && file_data)) {
			sam_restore_a_file(name_compare, &perm_inode,
			    vsnp, slink, data,
			    n_acls, aclp, file_data, &file_data_read);
		}
		skipping = 0;

skip_seg_file:
		if (S_ISSEGI(&perm_inode.di)) {
			struct sam_perm_inode seg_inode;
			int i;
			offset_t seg_size;
			int no_seg;
			sam_id_t seg_parent_id = perm_inode.di.id;

			/*
			 * If we are restoring the data, don't restore the
			 * data segment inodes.  This means we will lose the
			 * archive copies, if any.
			 */
			if (file_data) skipping++;

			/*
			 * Read each segment inode. If archive copies
			 * overflowed,
			 * read vsn sections directly after each segment inode.
			 */

			seg_size =
			    (offset_t)perm_inode.di.rm.info.dk.seg_size *
			    SAM_MIN_SEGMENT_SIZE;
			no_seg = (perm_inode.di.rm.size + seg_size - 1) /
			    seg_size;
			for (i = 0; i < no_seg; i++) {
				struct sam_vsn_section *seg_vsnp;

				readcheck(&seg_inode, sizeof (seg_inode),
				    5002);
				if (swapped) {
					if (sam_byte_swap(
					    sam_perm_inode_swap_descriptor,
					    &seg_inode, sizeof (seg_inode))) {
						error(0, 0,
						    catgets(catfd, SET, 13531,
						"%s: segment inode byte "
						"swap error - skipping."),
						    name);
						dont_process_this_entry = 1;
					}
				}
				if (!(SAM_CHECK_INODE_VERSION(
				    seg_inode.di.version))) {
					if (debugging) {
						fprintf(stderr,
						    "cs_restore: seg %d "
						    "inode version %d\n",
						    i, seg_inode.di.version);
					}
					error(0, 0, catgets(catfd, SET, 739,
					    "%s: inode version incorrect - "
					    "skipping"),
					    name);
					dont_process_this_entry = 1;
				}
				if (skipping || dont_process_this_entry) {
					continue;
				}
				seg_inode.di.parent_id = seg_parent_id;
				seg_vsnp = NULL;
				csd_read_mve(&seg_inode, &seg_vsnp);
				if (verbose) {
					sam_ls(name_compare, &seg_inode, NULL);
				}
				sam_restore_a_file(name_compare, &seg_inode,
				    seg_vsnp,
				    NULL, NULL, 0, NULL, file_data, NULL);
				if (seg_vsnp) {
					SamFree(seg_vsnp);
				}
			}
			/*
			 * Now that we have skipped the data segment inodes
			 * we can restore the dumped data if any.
			 */
			if (file_data) {
				sam_restore_a_file(name_compare, &perm_inode,
				    vsnp, slink, data,
				    n_acls, aclp, file_data, &file_data_read);
			}
		}

skip_file:
		if (data) {
			SamFree(data);
			data = NULL;
		}
		if (vsnp) {
			SamFree(vsnp);
			vsnp = NULL;
		}
		if (aclp) {
			SamFree(aclp);
			aclp = NULL;
		}
		if (file_data && file_data_read == 0) {
			skip_embedded_file_data();
		}
	}

	if (last_path != NULL) {
		free(last_path);
	}
	if (save_path != (char *)NULL) {
		free(save_path);
	}
	if (open_dir_fd > 0) {
		(void) close(open_dir_fd);
	}
	pop_permissions_stack();
	pop_times_stack();
}
示例#15
0
END_TEST

START_TEST(test_dn_write_file)
{
	const char *tf = "/HADOOFUS_TEST_WRITE_FILE",
	      *client = "HADOOFUS_CLIENT", *err;
	bool s;

	struct hdfs_datanode *dn;
	struct hdfs_object *e = NULL, *bl, *fs, *bls;
	uint64_t begin, end;

	s = hdfs_delete(h, tf, false/*recurse*/, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));

	hdfs_create(h, tf, 0644, client, true/*overwrite*/,
	    false/*createparent*/, 1/*replication*/, blocksz, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));

	begin = _now();

	// write first block (full)
	bl = hdfs_addBlock(h, tf, client, NULL, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));

	dn = hdfs_datanode_new(bl, client, HDFS_DATANODE_AP_1_0, &err);
	ck_assert_msg((intptr_t)dn, "error connecting to datanode: %s", err);

	hdfs_object_free(bl);

	err = hdfs_datanode_write_file(dn, fd, blocksz, 0, _i/*crcs*/);
	fail_if(err, "error writing block: %s", err);

	hdfs_datanode_delete(dn);

	// write second block (partial)
	bl = hdfs_addBlock(h, tf, client, NULL, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));

	dn = hdfs_datanode_new(bl, client, HDFS_DATANODE_AP_1_0, &err);
	ck_assert_msg((intptr_t)dn, "error connecting to datanode: %s", err);

	hdfs_object_free(bl);

	err = hdfs_datanode_write_file(dn, fd, towrite-blocksz, blocksz, _i/*crcs*/);
	fail_if(err, "error writing block: %s", err);

	hdfs_datanode_delete(dn);

	end = _now();
	fprintf(stderr, "Wrote %d MB from file in %ld ms%s, %02g MB/s\n",
	    towrite/1024/1024, end - begin, _i? " (with crcs)":"",
	    (double)towrite/(end-begin)/1024*1000/1024);

	fs = hdfs_getFileInfo(h, tf, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));
	ck_assert(fs->ob_val._file_status._size == towrite);
	hdfs_object_free(fs);

	s = hdfs_complete(h, tf, client, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));
	ck_assert_msg(s, "did not complete");

	bls = hdfs_getBlockLocations(h, tf, 0, towrite, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));

	begin = _now();
	for (int i = 0; i < bls->ob_val._located_blocks._num_blocks; i++) {
		struct hdfs_object *bl =
		    bls->ob_val._located_blocks._blocks[i];
		dn = hdfs_datanode_new(bl, client, HDFS_DATANODE_AP_1_0, &err);
		ck_assert_msg((intptr_t)dn, "error connecting to datanode: %s", err);

		err = hdfs_datanode_read_file(dn, 0/*offset-in-block*/,
		    bl->ob_val._located_block._len,
		    ofd,
		    i*blocksz/*fd offset*/,
		    _i/*crcs*/);

		hdfs_datanode_delete(dn);

		if (err == HDFS_DATANODE_ERR_NO_CRCS) {
			fprintf(stderr, "Warning: test server doesn't support "
			    "CRCs, skipping validation.\n");
			_i = 0;

			// reconnect, try again without validating CRCs (for
			// isi_hdfs_d)
			dn = hdfs_datanode_new(bl, client, HDFS_DATANODE_AP_1_0, &err);
			ck_assert_msg((intptr_t)dn, "error connecting to datanode: %s", err);

			err = hdfs_datanode_read_file(dn, 0/*offset-in-block*/,
			    bl->ob_val._located_block._len,
			    ofd,
			    i*blocksz,
			    false/*crcs*/);

			hdfs_datanode_delete(dn);
		}

		fail_if(err, "error reading block: %s", err);
	}
	end = _now();
	fprintf(stderr, "Read %d MB to file in %ld ms%s, %02g MB/s\n",
	    towrite/1024/1024, end - begin, _i? " (with crcs)":"",
	    (double)towrite/(end-begin)/1024*1000/1024);

	hdfs_object_free(bls);
	fail_if(filecmp(fd, ofd, towrite), "read differed from write");

	s = hdfs_delete(h, tf, false/*recurse*/, &e);
	if (e)
		fail("exception: %s", hdfs_exception_get_message(e));
	ck_assert_msg(s, "delete returned false");
}
示例#16
0
文件: diotest1.c 项目: laitianli/ltp
/* 校验读出的与写入的是否致 */
int main(int argc, char *argv[])
{
	int bufsize = BUFSIZE;	/* Buffer size. Default 8k */
	int numblks = NBLKS;	/* Number of blocks. Default 20 */
	char infile[LEN];	/* Input file. Default "infile" */
	char outfile[LEN];	/* Output file. Default "outfile" */
	int fd, fd1, fd2;
	int i, n, offset;
	char *buf;

	/* Options */
	strcpy(infile, "infile");	/* Default input file */
	strcpy(outfile, "outfile");	/* Default outfile file */
	while ((i = getopt(argc, argv, "b:n:i:o:")) != -1) {
		switch (i) {
		case 'b':
			if ((bufsize = atoi(optarg)) <= 0) {
				fprintf(stderr, "bufsize must be > 0\n");
				prg_usage();
			}
			if (bufsize % 4096 != 0) {
				fprintf(stderr,
					"bufsize must be multiple of 4k\n");
				prg_usage();
			}
			break;
		case 'n':
			if ((numblks = atoi(optarg)) <= 0) {
				fprintf(stderr, "numblks must be > 0\n");
				prg_usage();
			}
			break;
		case 'i':
			strcpy(infile, optarg);
			break;
		case 'o':
			strcpy(outfile, optarg);
			break;
		default:
			prg_usage();
		}
	}
	/* 检测系统是否支持O_DIRECT标志 */
	/* Test for filesystem support of O_DIRECT */
	if ((fd = open(infile, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) {
		tst_brkm(TCONF,
			 NULL,
			 "O_DIRECT is not supported by this filesystem.");
	} else {
		close(fd);
	}
	/* 创建文件1 */
	/* Open files */
	if ((fd1 = open(infile, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) {
		tst_brkm(TFAIL, NULL, "open infile failed: %s",
			 strerror(errno));
	}
	/* 创建文件2 */
	if ((fd2 = open(outfile, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) {
		close(fd1);
		unlink(infile);
		tst_brkm(TFAIL, NULL, "open outfile failed: %s",
			 strerror(errno));
	}

	/* Allocate for buf, Create input file */
	if ((buf = valloc(bufsize)) == 0) {
		tst_resm(TFAIL, "valloc() failed: %s", strerror(errno));
		fail_clean(fd1, fd2, infile, outfile);
	}
	/* 将数据写入到文件1 */
	for (i = 0; i < numblks; i++) {
		fillbuf(buf, bufsize, (char)(i % 256));
		if (write(fd1, buf, bufsize) < 0) {
			tst_resm(TFAIL, "write infile failed: %s",
				 strerror(errno));
			fail_clean(fd1, fd2, infile, outfile);
		}
	}

	/* Copy infile to outfile using direct read and direct write */
	offset = 0;
	if (lseek(fd1, offset, SEEK_SET) < 0) {
		tst_resm(TFAIL, "lseek(infd) failed: %s", strerror(errno));
		fail_clean(fd1, fd2, infile, outfile);
	}
	/* 从文件1读取数据,并写入文件2 */
	while ((n = read(fd1, buf, bufsize)) > 0) {
		if (lseek(fd2, offset, SEEK_SET) < 0) {
			tst_resm(TFAIL, "lseek(outfd) failed: %s",
				 strerror(errno));
			fail_clean(fd1, fd2, infile, outfile);
		}
		if (write(fd2, buf, n) < n) {
			tst_resm(TFAIL, "write(outfd) failed: %s",
				 strerror(errno));
			fail_clean(fd1, fd2, infile, outfile);
		}
		offset += n;
		if (lseek(fd1, offset, SEEK_SET) < 0) {
			tst_resm(TFAIL, "lseek(infd) failed: %s",
				 strerror(errno));
			fail_clean(fd1, fd2, infile, outfile);
		}
	}
	/* 检测两文件的内容是否一致 */
	/* Verify */
	if (filecmp(infile, outfile) != 0) {
		tst_resm(TFAIL, "file compare failed for %s and %s",
			 infile, outfile);
		fail_clean(fd1, fd2, infile, outfile);
	}

	/* Cleanup */
	close(fd1);
	close(fd2);
	unlink(infile);
	unlink(outfile);
	tst_resm(TPASS, "Test passed");
	tst_exit();
}
示例#17
0
文件: test.c 项目: kapop/lincore
void test_filecmp()
{
    char * f1 = "/home/liheyuan/.xsession-errors";
    char * f2 = "/tmp/.xsession-errors";
    printf("filecmp():%d\n", filecmp(f1, f2));
}