Skip to content

Commit

Permalink
https://github.com/cloudstore/cloudstore/issues/54
Browse files Browse the repository at this point in the history
  • Loading branch information
nlmarco committed Aug 21, 2016
1 parent 9f83c9d commit 70d4613
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ public void localSync(final ProgressMonitor monitor) {
final LocalRepoTransactionImpl transaction = beginWriteTransaction();
try {
monitor.worked(1);
LocalRepoSync.create(transaction).sync(new SubProgressMonitor(monitor, 98));
LocalRepoSync.create(transaction).ignoreRulesEnabled(true).sync(new SubProgressMonitor(monitor, 98));
transaction.commit();
monitor.worked(1);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class LocalRepoSync {
protected final ModificationDao modificationDao;
protected final DeleteModificationDao deleteModificationDao;
private Collection<RemoteRepository> remoteRepositories;
private boolean ignoreRulesEnabled;

private final Map<String, Set<String>> sha1AndLength2Paths = new HashMap<String, Set<String>>();

Expand Down Expand Up @@ -137,7 +138,9 @@ protected RepoFile sync(final RepoFile parentRepoFile, final File file, final Pr
RepoFile repoFile = repoFileDao.getRepoFile(localRoot, file);

if (parentRepoFile != null) {
boolean ignored = IgnoreRuleManagerImpl.getInstanceForDirectory(file.getParentFile()).isIgnored(file);
boolean ignored = isIgnoreRulesEnabled()
? IgnoreRuleManagerImpl.getInstanceForDirectory(file.getParentFile()).isIgnored(file)
: false;
if (ignored) {
if (repoFile != null) {
deleteRepoFile(repoFile, false);
Expand Down Expand Up @@ -630,4 +633,15 @@ protected void sha(final NormalFile normalFile, final File file, final ProgressM
protected void onFinalizeFileChunk(FileChunk fileChunk) {
// can be extended by sub-classes to handle FileChunk-subclasses specifically.
}

public boolean isIgnoreRulesEnabled() {
return ignoreRulesEnabled;
}
public void setIgnoreRulesEnabled(boolean ignoreRulesEnabled) {
this.ignoreRulesEnabled = ignoreRulesEnabled;
}
public LocalRepoSync ignoreRulesEnabled(boolean ignoreRulesEnabled) {
setIgnoreRulesEnabled(ignoreRulesEnabled);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public void delete(String path) {
try ( final LocalRepoTransaction transaction = getLocalRepoManager().beginWriteTransaction(); ) {
ParentFileLastModifiedManager.getInstance().backupParentFileLastModified(parentFile);
try {
final LocalRepoSync localRepoSync = LocalRepoSync.create(transaction);
final LocalRepoSync localRepoSync = LocalRepoSync.create(transaction); // not sure about the ignoreRulesEnabled here.
localRepoSync.sync(file, new NullProgressMonitor(), true);

if (fileIsLocalRoot) {
Expand Down Expand Up @@ -622,7 +622,8 @@ protected void mkDir(final LocalRepoTransaction transaction, final UUID clientRe
protected RepoFile syncRepoFile(final LocalRepoTransaction transaction, final File file) {
assertNotNull("transaction", transaction);
assertNotNull("file", file);
return LocalRepoSync.create(transaction).sync(file, new NullProgressMonitor(), false); // recursiveChildren==false, because we only need this one single Directory object in the DB, and we MUST NOT consume time with its children.
return LocalRepoSync.create(transaction)
.sync(file, new NullProgressMonitor(), false); // recursiveChildren==false, because we only need this one single Directory object in the DB, and we MUST NOT consume time with its children.
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class IgnoreRulesRepoToRepoSyncIT extends AbstractRepoAwareIT
private static final Logger logger = LoggerFactory.getLogger(IgnoreRulesRepoToRepoSyncIT.class);

@Test
public void syncWithIgnoreRules() throws Exception {
public void ignoreRulesExistBeforeAffectedFiles() throws Exception {
createLocalAndRemoteRepo();
final LocalRepoManager localRepoManagerRemote = localRepoManagerFactory.createLocalRepoManagerForExistingRepository(remoteRoot);

Expand Down Expand Up @@ -107,6 +107,134 @@ public void syncWithIgnoreRules() throws Exception {
localRepoManagerRemote.close();
}

@Test
public void ignoreRulesExistBecomeDisabled() throws Exception {
createLocalAndRemoteRepo();
final LocalRepoManager localRepoManagerRemote = localRepoManagerFactory.createLocalRepoManagerForExistingRepository(remoteRoot);

final File child_1 = createDirectory(remoteRoot, "1");

final File child_1_a = createFileWithRandomContent(child_1, "a");
final File child_1_b = createFileWithRandomContent(child_1, "b");

localRepoManagerRemote.localSync(new NullProgressMonitor());
assertThatFilesInRepoAreCorrect(remoteRoot);

final RepoToRepoSync repoToRepoSync = RepoToRepoSync.create(getLocalRootWithPathPrefix(), remoteRootURLWithPathPrefix);
repoToRepoSync.sync(new LoggerProgressMonitor(logger));

assertThatFilesInRepoAreCorrect(remoteRoot);
assertDirectoriesAreEqualRecursively(getLocalRootWithPathPrefix(), getRemoteRootWithPathPrefix());


// Create and sync ignore rules.
Properties properties = new Properties();
properties.put("ignore[a].namePattern", "a"); // ignore a file that already exists and was already synced
properties.put("ignore[c].namePattern", "c"); // ignore a new file
properties.put("ignore[dir1].namePattern", "dir1"); // ignore a new directory
PropertiesUtil.store(createFile(remoteRoot, ".cloudstore.properties"), properties, null);

repoToRepoSync.sync(new LoggerProgressMonitor(logger));


// Modify some files and see whether the ignore-rules are respected.
try (OutputStream out = child_1_a.createOutputStream(true);) {
out.write(new byte[] { 1, 2, 3 });
}

try (OutputStream out = child_1_b.createOutputStream(true);) {
out.write(new byte[] { 4, 5, 6 });
}

final File child_1_c = createFileWithRandomContent(child_1, "c"); // new file!
final File src_dir1 = createDirectory(child_1, "dir1");
final File src_dir1_aaa = createFileWithRandomContent(src_dir1, "aaa");
byte[] src_dir1_aaa_data = readAll(src_dir1_aaa);

final File dest_child_1 = createFile(localRoot, "1");
final File dest_child_1_a = createFile(dest_child_1, "a");
final File dest_child_1_b = createFile(dest_child_1, "b");
final File dest_child_1_c = createFile(dest_child_1, "c");
final File dest_dir1 = createFile(dest_child_1, "dir1");
final File dest_dir1_aaa = createFile(dest_dir1, "aaa");


byte[] src_child_1_a_data = readAll(child_1_a);
byte[] dest_child_1_a_data = readAll(dest_child_1_a);
assertThat(src_child_1_a_data).isNotEqualTo(dest_child_1_a_data);

byte[] src_child_1_b_data = readAll(child_1_b);
byte[] dest_child_1_b_data = readAll(dest_child_1_b);
assertThat(src_child_1_b_data).isNotEqualTo(dest_child_1_b_data);

byte[] src_child_1_c_data = readAll(child_1_c);
assertThat(dest_child_1_c.getIoFile()).doesNotExist();


repoToRepoSync.sync(new LoggerProgressMonitor(logger));

// The file "/1/a" should *not* have been synced! It's ignored!
byte[] src_child_1_a_data2 = readAll(child_1_a);
byte[] dest_child_1_a_data2 = readAll(dest_child_1_a);
assertThat(src_child_1_a_data2).isEqualTo(src_child_1_a_data);
assertThat(dest_child_1_a_data2).isEqualTo(dest_child_1_a_data);

// The file "/1/b" should have been synced, because it's *not* ignored!
byte[] src_child_1_b_data2 = readAll(child_1_b);
byte[] dest_child_1_b_data2 = readAll(dest_child_1_b);
assertThat(src_child_1_b_data2).isEqualTo(src_child_1_b_data);
assertThat(dest_child_1_b_data2).isNotEqualTo(dest_child_1_b_data);
assertThat(dest_child_1_b_data2).isEqualTo(src_child_1_b_data);

// The file "/1/c" should *not* have been synced! It's ignored!
byte[] src_child_1_c_data2 = readAll(child_1_c);
assertThat(src_child_1_c_data2).isEqualTo(src_child_1_c_data);
assertThat(dest_child_1_c.getIoFile()).doesNotExist();

// The directory dir1 should *not* have been synced!
assertThat(src_dir1.getIoFile()).isDirectory();
assertThat(src_dir1_aaa.getIoFile()).isFile();
byte[] src_dir1_aaa_data1 = readAll(src_dir1_aaa);
assertThat(src_dir1_aaa_data1).isEqualTo(src_dir1_aaa_data);
assertThat(dest_dir1.getIoFile()).doesNotExist();
assertThat(dest_dir1_aaa.getIoFile()).doesNotExist();


// Disable ignore rules.
properties = new Properties();
properties.put("ignore[a].enabled", "false");
properties.put("ignore[c].enabled", "false");
properties.put("ignore[dir1].enabled", "false");
PropertiesUtil.store(createFile(child_1, ".cloudstore.properties"), properties, null);

repoToRepoSync.sync(new LoggerProgressMonitor(logger));

// Now, the previously ignored file "/1/a" should have been synced!
byte[] src_child_1_a_data3 = readAll(child_1_a);
byte[] dest_child_1_a_data3 = readAll(dest_child_1_a);
assertThat(dest_child_1_a_data3).isEqualTo(src_child_1_a_data3);

// And the previously ignoed file "/1/c" should have been synced, too!
byte[] src_child_1_c_data3 = readAll(child_1_c);
assertThat(src_child_1_c_data3).isEqualTo(src_child_1_c_data);
assertThat(dest_child_1_c.getIoFile()).exists();
byte[] dest_child_1_c_data3 = readAll(child_1_c);
assertThat(dest_child_1_c_data3).isEqualTo(src_child_1_c_data3);

// The previously ignored directory dir1 should have been synced!
assertThat(src_dir1.getIoFile()).isDirectory();
assertThat(src_dir1_aaa.getIoFile()).isFile();
byte[] src_dir1_aaa_data2 = readAll(src_dir1_aaa);
assertThat(src_dir1_aaa_data2).isEqualTo(src_dir1_aaa_data);
assertThat(dest_dir1.getIoFile()).isDirectory();
assertThat(dest_dir1_aaa.getIoFile()).isFile();
byte[] dest_dir1_aaa_data = readAll(dest_dir1_aaa);
assertThat(dest_dir1_aaa_data).isEqualTo(src_dir1_aaa_data);

repoToRepoSync.close();
localRepoManagerRemote.close();
}

private static byte[] readAll(File f) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (InputStream in = f.createInputStream()) {
Expand Down

0 comments on commit 70d4613

Please sign in to comment.