locked
implement customer file sync provider overwrite from FullEnumerationSimpleSyncProvider(delete folder not work error) RRS feed

  • Question

  • Hi All,

    I got an error when I delete folder from source,

    public override void EnumerateItems(FullEnumerationContext context)
            {
                List<ItemFieldDictionary> items = new List<ItemFieldDictionary>();
    
                foreach (string fullFilePath in Directory.GetFileSystemEntries(_localStoragePath, "*", SearchOption.AllDirectories))
                {
                    if (Path.GetExtension(fullFilePath).ToLowerInvariant() != ".metadata")
                    {
                        items.Add(
                            ConstructDictionary(fullFilePath,
                                File.GetLastWriteTimeUtc(fullFilePath).Ticks));
                    }
                }
    
                context.ReportItems(items);
            }

    When I modified a file into the subfolder(in source) , then it will throw exception: the specified parmeter was out of valid

    Below code are my insert and update method:

      public override void InsertItem(object itemData,
                IEnumerable<SyncId> changeUnitsToCreate,
                RecoverableErrorReportingContext recoverableErrorReportingContext,
                out ItemFieldDictionary keyAndUpdatedVersion,
                out bool commitKnowledgeAfterThisItem)
            {
                IFileDataRetriever fileData = (IFileDataRetriever)itemData;
    
                string sourcePath = Path.Combine(_localPathName, fileData.FileData.RelativePath);
                string targetPath = Path.Combine(_localStoragePath, fileData.FileData.RelativePath);
    
                if (fileData.FileData.IsDirectory)
                {
                    if (!Directory.Exists(targetPath))
                    {
                        Directory.CreateDirectory(targetPath);
                    }
    
                    keyAndUpdatedVersion = ConstructDictionary(targetPath,
                                           Directory.GetLastWriteTimeUtc(targetPath).Ticks);
                    commitKnowledgeAfterThisItem = false;
                    return;
                }
    
                using (FileStream fs = File.OpenRead(sourcePath))
                {
                    if (File.Exists(targetPath))//this is an exception
                    {
                        recoverableErrorReportingContext.RecordConstraintError(
                            ConstructDictionary(targetPath));
                        keyAndUpdatedVersion = null;
                        commitKnowledgeAfterThisItem = false;
                        return;
                    }
                    string targetFolderPath = Path.Combine(_localStoragePath, fileData.RelativeDirectoryPath);
                    if (!Directory.Exists(targetFolderPath))
                    {
                        Directory.CreateDirectory(targetFolderPath);
                    }
                    File.Create(targetPath).Dispose();
    
                    keyAndUpdatedVersion = ConstructDictionary(targetPath,
                    File.GetLastWriteTimeUtc(targetPath).Ticks);
                    UploadFile(fileData.FileData.RelativePath, myContainer, fileData.FileStream);
                    commitKnowledgeAfterThisItem = false;
                }
            }
    
      public override void UpdateItem(object itemData,
                IEnumerable<SyncId> changeUnitsToUpdate,
                ItemFieldDictionary keyAndExpectedVersion,
                RecoverableErrorReportingContext recoverableErrorReportingContext,
                out ItemFieldDictionary keyAndUpdatedVersion,
                out bool commitKnowledgeAfterThisItem)
            {
                string oldTargetPath;
                long oldLMT;
                ParseDictionary(keyAndExpectedVersion, out oldTargetPath, out oldLMT);
                IFileDataRetriever fileData = (IFileDataRetriever)itemData;
                string sourceLocalPath = Path.Combine(_localPathName, fileData.FileData.RelativePath);
                string newTargetPath = Path.Combine(_localStoragePath, fileData.FileData.RelativePath);
                string oldRelativePath = oldTargetPath.Substring(_localStoragePath.Length + 1, oldTargetPath.Length - _localStoragePath.Length - 1);
    
                //if (fileData.FileData.IsDirectory)
                //{
                //    if (String.Compare(newTargetPath, oldTargetPath, StringComparison.CurrentCultureIgnoreCase) != 0)
                //    {
                //        Directory.Move(oldTargetPath, newTargetPath);
                //    }
    
                //    keyAndUpdatedVersion = ConstructDictionary(
                //      newTargetPath,
                //       Directory.GetLastWriteTimeUtc(newTargetPath).Ticks);
    
                //    commitKnowledgeAfterThisItem = false;
    
                //    return;
                //}
    
                using (FileStream fs = File.OpenRead(sourceLocalPath))
                {
                    string targetFolder = Path.Combine(_localStoragePath, fileData.RelativeDirectoryPath);
                    DateTime parentFolderTicks = File.GetLastWriteTimeUtc(targetFolder);
                    if (String.Compare(newTargetPath, oldTargetPath, StringComparison.CurrentCultureIgnoreCase) != 0)
                    {
                        if (!Directory.Exists(targetFolder))
                        {
                            Directory.CreateDirectory(targetFolder);
                        }
    
                        File.Move(oldTargetPath, newTargetPath);
                        UploadFile(fileData.FileData.RelativePath, myContainer, fileData.FileStream);
                        DeleteFile(oldRelativePath, myContainer);
                    }
                    else
                    {
                        File.SetLastWriteTime(newTargetPath, DateTime.Now);
                        //Directory.SetLastWriteTimeUtc(targetFolder, DateTime.Now);
                        UploadFile(fileData.FileData.RelativePath, myContainer, fileData.FileStream);
                    }
                    if (!targetFolder.Equals(_localStoragePath))
                    {
                        try
                        {
                            Directory.SetLastWriteTimeUtc(targetFolder, parentFolderTicks);
                        }
                        catch
                        {
                        }
                    }
                    keyAndUpdatedVersion = ConstructDictionary(
                       newTargetPath,
                        File.GetLastWriteTimeUtc(newTargetPath).Ticks);
    
                    commitKnowledgeAfterThisItem = false;
                }
            }

    Any help was appreciated

    Best regards

    Qiucu


    • Edited by liqiucu Sunday, December 22, 2013 7:31 AM
    Sunday, December 22, 2013 7:30 AM

All replies

  • what's the actual exception? can you post the stack trace?
    Monday, December 23, 2013 1:52 AM