/* 	Synchronized method to add file to the file system.
            If the file is not present then the file is added. If the file
         	is already present it is overwritten if the new file has a
         	higher version number
        */
        public bool addFileSynchronized(UserFile file)
        {
            Logger.Debug ("Adding file with file name : " + file.filemetadata.filepath);

            UserFile existingFile = getFileSynchronized (file.filemetadata.filepath);

            bool add = true;
            long existingSize = 0;
            if (existingFile != null) {
                existingSize = existingFile.getFileSizeSynchronized (); // this is the size of the existing file

                if (existingFile.filemetadata.markedForDeletion == false) {
                    if (file.filemetadata.versionNumber <= existingFile.filemetadata.versionNumber) {
                        Logger.Debug ("Existing higher number file found, skipping updation");
                        add = false;
                        throw new VersionNumberConflictException ("Version number of passed file and the existing files are : " +
                            file.filemetadata.versionNumber + " " + existingFile.filemetadata.versionNumber
                        );
                    } else {
                        Logger.Debug ("Existing lower version number file exists");
                    }
                } else {
                    Logger.Debug ("Exiting file exists, but marked for deletion");
                }
            }

            if (add) {
                addFileToMapSynchronized (file);
                incrementTotalFileSystemSize (file.filemetadata.filesize - existingSize);
            }
            return add;
        }
Example #2
0
 /* Internal synchronized method to add file to the class map*/
 private void addFileToMapSynchronized(UserFile file)
 {
     lock (this.privateLock) {
         file.initializePrivateLock();                  // this is because the files sent over the network do not have their private locks initialized
         this.filemap[file.filemetadata.filepath] = file;
     }
 }
Example #3
0
        public UserFile RestoreUserFile(string userdir, string relativefilepath, bool restoreFileContent)
        {
            logger.Debug("Restoring user file for file path and flag :" + userdir + " " + relativefilepath + " " + restoreFileContent);
            string completefilepath = userdir + Path.DirectorySeparatorChar
                                      + "files" + Path.DirectorySeparatorChar + relativefilepath;
            string metadatafilepath = userdir + Path.DirectorySeparatorChar
                                      + "metadata" + Path.DirectorySeparatorChar + relativefilepath + ".dat";

            List <string> userfilemetadata = FileUtils.ReadLinesFromFile(metadatafilepath);

            if (userfilemetadata.Count < 2)
            {
                throw new DiskuserMetaDataCorrupt("File meta data corrupt for file  : " + relativefilepath);
            }

            UserFile file = GetFileFromFileMetaData(FileUtils.getMemoryPathFromDiskPath(relativefilepath),
                                                    userfilemetadata);

            //this makes sure that in case of checkpointing this stuff, we don't load the file content, I know it's pretty neat :)
            if (restoreFileContent)
            {
                file.filecontent = File.ReadAllBytes(completefilepath);
            }

            return(file);
        }
Example #4
0
        private UserFileSystem RestoreUserFileSystem(string userdir)
        {
            logger.Debug("Restoring user file system for user dir path :" + userdir);
            UserFileSystem userfilesystem       = new UserFileSystem();
            string         user                 = new DirectoryInfo(userdir).Name;
            string         usermetadatafilepath = userdir + Path.DirectorySeparatorChar + metadatafilename;
            List <String>  metadatafilecontent  = FileUtils.ReadLinesFromFile(usermetadatafilepath);

            if (metadatafilecontent.Count < 3)
            {
                throw new DiskuserMetaDataCorrupt("Disk meta data corrupt for user: " + user);
            }

            UserMetaData metadataobj = new UserMetaData(metadatafilecontent[0].Trim(),
                                                        metadatafilecontent[1].Trim(), int.Parse(metadatafilecontent[2].Trim()));

            userfilesystem.metadata = metadataobj;

            metadatafilecontent.RemoveRange(0, 3);              //remove the meta data rows and we are left with file names
            foreach (string filepath in metadatafilecontent)
            {
                UserFile file = RestoreUserFile(userdir, filepath.Trim());
                userfilesystem.filemap.Add(filepath.Trim(), file);
            }
            return(userfilesystem);
        }
Example #5
0
        public UserFile getFileCloneSynchronized(string filename)
        {
            UserFile existingFile = null;

            lock (this.privateLock) {
                if (this.filemap.ContainsKey(filename))
                {
                    existingFile = this.filemap [filename];
                }
            }
            return(existingFile.getFileCloneSynchronized());
        }
Example #6
0
        /* This add the user to the shareduserlist of the file*/
        public void addSharedUserToFileSynchronized(string filename, string user)
        {
            Logger.Debug("Adding shared user :"******" to file : " + filename);
            UserFile file = getFileSynchronized(filename);

            if (file == null)
            {
                throw new FileNotFoundException("File not found for user name and filename : " + user + " " + filename);
            }

            file.addSharedSynchronized(user);
            return;
        }
Example #7
0
        private UserFile RestoreUserFile(string userdir, string relativefilepath)
        {
            logger.Debug("Restoring user file for file path :" + relativefilepath);
            string completefilepath = userdir + Path.DirectorySeparatorChar
                                      + "files" + Path.DirectorySeparatorChar + relativefilepath;
            string metadatafilepath = userdir + Path.DirectorySeparatorChar
                                      + "metadata" + Path.DirectorySeparatorChar + relativefilepath + ".dat";

            List <string> userfilemetadata = FileUtils.ReadLinesFromFile(metadatafilepath);
            UserFile      file             = GetFileFromFileMetaData(relativefilepath, userfilemetadata);

            file.filecontent = File.ReadAllBytes(completefilepath);
            return(file);
        }
Example #8
0
        //mark for deletion, reset the content, update the user file system size
        public bool deleteFileSynchronized(string filename)
        {
            UserFile file = getFileSynchronized(filename);

            if (file == null)
            {
                throw new FileNotFoundException("File not found :" + filename);
            }

            bool delete = file.markForDeletionSynchronized();

            //reset the content. this will also increment the version number. Don't do it later
            long sizeDiff = file.SetFileContentSynchronized(new byte[0], file.getFileVersionNumberSynchronized() + 1);

            incrementTotalFileSystemSize(sizeDiff);
            return(delete);
        }
Example #9
0
        private UserFile GetFileFromFileMetaData(string filepath, List <string> metadata)
        {
            UserFile file = new UserFile(filepath, metadata [0].Trim());

            file.filepath      = filepath;
            file.filesize      = int.Parse(metadata [1].Trim());
            file.versionNumber = int.Parse(metadata [2].Trim());
            string [] clients = metadata [3].Trim().Split(',');
            foreach (string client in clients)
            {
                if (client.Trim().Length > 0)
                {
                    file.sharedwithclients.Add(client.Trim());
                }
            }
            return(file);
        }
Example #10
0
        public void FlushFile(UserFile file)
        {
            logger.Debug("Flushing file name for user : "******" " + file.filemetadata.filepath);

            try {
                string        lastcheckpointfilepath    = CheckpointLogic.pathprefix + CheckpointLogic.lastcheckpointfilename;
                List <string> lastcheckpointfilecontent = FileUtils.ReadLinesFromFile(lastcheckpointfilepath);
                logger.Debug("Last check point time stamp read :" + lastcheckpointfilecontent [0].Trim());
                DateTime lastcheckpointtime = DateUtils.getDatefromString(lastcheckpointfilecontent [0].Trim());

                logger.Debug("Read last checkpoint time as :" + lastcheckpointtime);

                if (lastcheckpointfilecontent.Count < 2)
                {
                    throw new DiskuserMetaDataCorrupt("Something wrong with the last check point file path, check!!");
                }

                string latestcheckpointfolderpath = lastcheckpointfilecontent [1];
                logger.Debug("Last check point path read as :" + latestcheckpointfolderpath);
                latestcheckpointfolderpath = latestcheckpointfolderpath.Trim();
                string userpath = latestcheckpointfolderpath + file.filemetadata.owner + Path.DirectorySeparatorChar;
                userpath = FileUtils.getDiskPathFromMemoryPath(userpath);
                logger.Debug("User path constructed is :" + userpath);

                string filepath     = userpath + "files" + Path.DirectorySeparatorChar;
                string metadatapath = userpath + "metadata" + Path.DirectorySeparatorChar;
                logger.Debug("User path constructed is :" + filepath);

                string completefilepath         = filepath + FileUtils.getDiskPathFromMemoryPath(file.filemetadata.filepath);
                string completemetadatafilepath = metadatapath + FileUtils.getDiskPathFromMemoryPath(file.filemetadata.filepath)
                                                  + ".dat";

                logger.Debug("Final file path  : " + completefilepath);
                logger.Debug("Final metadata file path : " + completemetadatafilepath);

                System.IO.File.WriteAllText(completemetadatafilepath, file.GenerateMetaDataStringFromFile());
                File.WriteAllBytes(completefilepath, file.ReadFileContent());
            } catch (Exception e) {
                //logger.Warn ("Exception occured while restoring the file system : " + e);
                throw e;
            }
        }
        public void FlushFile(UserFile file)
        {
            logger.Debug ("Flushing file name for user : "******" " + file.filemetadata.filepath);

            try {
                string lastcheckpointfilepath = CheckpointLogic.pathprefix + CheckpointLogic.lastcheckpointfilename;
                List<string> lastcheckpointfilecontent = FileUtils.ReadLinesFromFile (lastcheckpointfilepath);
                logger.Debug ("Last check point time stamp read :" + lastcheckpointfilecontent [0].Trim ());
                DateTime lastcheckpointtime = DateUtils.getDatefromString (lastcheckpointfilecontent [0].Trim ());

                logger.Debug ("Read last checkpoint time as :" + lastcheckpointtime);

                if (lastcheckpointfilecontent.Count < 2)
                    throw new DiskuserMetaDataCorrupt ("Something wrong with the last check point file path, check!!");

                string latestcheckpointfolderpath = lastcheckpointfilecontent [1];
                logger.Debug ("Last check point path read as :" + latestcheckpointfolderpath);
                latestcheckpointfolderpath = latestcheckpointfolderpath.Trim ();
                string userpath = latestcheckpointfolderpath + file.filemetadata.owner + Path.DirectorySeparatorChar;
                userpath = FileUtils.getDiskPathFromMemoryPath (userpath);
                logger.Debug ("User path constructed is :" + userpath);

                string filepath = userpath + "files" + Path.DirectorySeparatorChar;
                string metadatapath = userpath + "metadata" + Path.DirectorySeparatorChar;
                logger.Debug ("User path constructed is :" + filepath);

                string completefilepath = filepath + FileUtils.getDiskPathFromMemoryPath (file.filemetadata.filepath);
                string completemetadatafilepath = metadatapath + FileUtils.getDiskPathFromMemoryPath (file.filemetadata.filepath)
                    + ".dat";

                logger.Debug ("Final file path  : " + completefilepath);
                logger.Debug ("Final metadata file path : " + completemetadatafilepath);

                System.IO.File.WriteAllText (completemetadatafilepath, file.GenerateMetaDataStringFromFile ());
                File.WriteAllBytes (completefilepath, file.ReadFileContent ());

            } catch (Exception e) {
                //logger.Warn ("Exception occured while restoring the file system : " + e);
                throw e;
            }
        }
Example #12
0
        /*  Synchronized method to add file to the file system.
         *      If the file is not present then the file is added. If the file
         *      is already present it is overwritten if the new file has a
         *      higher version number
         */
        public bool addFileSynchronized(UserFile file)
        {
            Logger.Debug("Adding file with file name : " + file.filemetadata.filepath);

            UserFile existingFile = getFileSynchronized(file.filemetadata.filepath);

            bool add          = true;
            long existingSize = 0;

            if (existingFile != null)
            {
                existingSize = existingFile.getFileSizeSynchronized();                  // this is the size of the existing file

                if (existingFile.filemetadata.markedForDeletion == false)
                {
                    if (file.filemetadata.versionNumber <= existingFile.filemetadata.versionNumber)
                    {
                        Logger.Debug("Existing higher number file found, skipping updation");
                        add = false;
                        throw new VersionNumberConflictException("Version number of passed file and the existing files are : " +
                                                                 file.filemetadata.versionNumber + " " + existingFile.filemetadata.versionNumber
                                                                 );
                    }
                    else
                    {
                        Logger.Debug("Existing lower version number file exists");
                    }
                }
                else
                {
                    Logger.Debug("Exiting file exists, but marked for deletion");
                }
            }

            if (add)
            {
                addFileToMapSynchronized(file);
                incrementTotalFileSystemSize(file.filemetadata.filesize - existingSize);
            }
            return(add);
        }
Example #13
0
        private UserFile GetFileFromFileMetaData(string filepath, List <string> metadata)
        {
            UserFile file = new UserFile(filepath, metadata [0].Trim());

            file.filemetadata.filepath = filepath;

            file.filemetadata.filesize      = int.Parse(metadata [1].Trim());
            file.filemetadata.versionNumber = int.Parse(metadata [2].Trim());

            if (metadata.Count > 3)               //this is because this file might be shared with no one
            {
                string [] clients = metadata [3].Trim().Split(',');

                foreach (string client in clients)
                {
                    if (client.Trim().Length > 0)
                    {
                        file.filemetadata.sharedwithclients.Add(client.Trim());
                    }
                }
            }
            logger.Debug("Recovered metadata as : " + file.filemetadata);
            return(file);
        }
Example #14
0
 public long SetFileContentSynchronized(UserFile newfile)
 {
     return(SetFileContentSynchronized(newfile.filecontent, newfile.filemetadata.versionNumber));
 }
Example #15
0
 public void addFile(UserFile file)
 {
     this.filemap.Add(file.filepath, file);
 }
Example #16
0
 public long SetFileContentSynchronized(UserFile newfile)
 {
     return SetFileContentSynchronized( newfile.filecontent, newfile.filemetadata.versionNumber);
 }
Example #17
0
        //Merge the user file system
        private UserFileSystem mergeUserFileSystems(UserFileSystem newfs, UserFileSystem oldfs)
        {
            logger.Debug("Merging user file systems for user id : " + newfs.metadata.clientId);

            /*
             * Rule of Fight Club ( UserFile system merge)
             *
             * 1) User meta data- The one with higher version number wins, although it will be mostly the
             * newer file system object since user meta data is always maintained in memory
             *
             * 2) SharedFileList - This will be picked from the newer file system object since we don't have versioning for it
             * and it is maintained in the memory always
             *
             * 3) File map -
             *      a) If there is a new file which is not present in the old file system
             *              If its marked for deletion  - Don't add it
             *              If its not marked for deletion  - Add it
             *      b) If there is file present in the new file system which is also present in the old file system
             *              If the version number of the new file is higher
             *                      If its marked for deletion in the new file system delete that file
             *                      If its not marked for deletion, replace the file
             *              If its version number is lower
             *                      TOO BAD
             *      c) If there are files which are not present in the new file system which are present in old file system
             *              Ideally this should not happen since the all file names will always remain in memory. In any case, take the file on disk.
             *
             *
             */

            if (newfs.metadata.versionNumber > oldfs.metadata.versionNumber)
            {
                oldfs.metadata = newfs.metadata;                 //replace
            }
            else
            {
                logger.Warn("The version number for the new user metadata is lower than the old, FIX ME FIX ME");
            }

            oldfs.sharedFiles = newfs.sharedFiles;             //replace the shared file list

            //now iterate over the file map, don't f**k up man
            foreach (KeyValuePair <string, UserFile> entry in newfs.filemap)
            {
                UserFile newfile  = entry.Value;
                UserFile oldfile  = null;
                string   filename = entry.Key;

                if (oldfs.filemap.ContainsKey(filename))
                {
                    oldfile = oldfs.filemap [filename];
                }

                if (oldfile == null)                    //case when there is new file and NO old file

                {
                    if (newfile.filemetadata.markedForDeletion == false)
                    {
                        oldfs.addFileSynchronized(newfile);
                    }
                    else
                    {
                        logger.Debug("File found marked for deleting, skipping it : " + filename);
                    }
                }
                else                     // case where there is old file and new file

                {
                    if (newfile.filemetadata.versionNumber > oldfile.filemetadata.versionNumber)      //lets roll
                    {
                        if (newfile.filemetadata.markedForDeletion == true)                           //remove this file now
                        {
                            logger.Debug("File marked for deletion, removing : " + filename);
                            oldfs.removeFromMap(filename);                              //this will decrement the size
                        }
                        else
                        {
                            //long sizediff = newfile.filemetadata.filesize - oldfile.filemetadata.filesize;
                            oldfs.filemap [filename] = newfile;
                            //oldfs.incrementTotalFileSystemSize (sizediff);
                        }
                    }
                    else
                    {
                        logger.Debug("Version number of new content is not greater, skipping : " + newfile.filemetadata.versionNumber + " "
                                     + oldfile.filemetadata.versionNumber);
                    }
                }
            }

            return(oldfs);
        }
Example #18
0
        // Method used for restoring the user file system
        private UserFileSystem RestoreUserFileSystem(string userdir, bool restoreFileContent)
        {
            logger.Debug("Restoring user file system for user dir path and restore file content :" + userdir + " " + restoreFileContent);
            UserFileSystem userfilesystem = new UserFileSystem();            //this is what we return from this method
            string         user           = new DirectoryInfo(userdir).Name; //fetch the user name from directory name

            string userinfofilepath       = userdir + Path.DirectorySeparatorChar + CheckpointLogic.USERINFOFILENAME;
            string fileinfofileapth       = userdir + Path.DirectorySeparatorChar + CheckpointLogic.FILEINFOFILENAME;
            string sharedfileinfofilepath = userdir + Path.DirectorySeparatorChar + CheckpointLogic.SHAREDFILEINFONAME;

            //First restore the user info
            List <String> metadatafilecontent = FileUtils.ReadLinesFromFile(userinfofilepath);

            if (metadatafilecontent.Count < 4)
            {
                throw new DiskuserMetaDataCorrupt("Disk meta data corrupt for user: "******"SEE : " + long.Parse (metadatafilecontent [3].Trim ()));
            //logger.Fatal ("SEE : " + userfilesystem.metadata.ToString());

            //now restore the shared files
            List <string> sharedFileNames = FileUtils.ReadLinesFromFile(sharedfileinfofilepath);

            logger.Debug("# shared file : " + sharedFileNames.Count);
            foreach (string sharefilecontent in sharedFileNames)
            {
                string[] contentlist = sharefilecontent.Split(' ');
                if (contentlist.Length > 1)
                {
                    userfilesystem.sharedFiles.Add(new SharedFile(contentlist [0].Trim(), contentlist [1].Trim()));
                }
                else
                {
                    logger.Warn("File content for shared-info.txt is corrupt, check what happened " + sharefilecontent);
                }
            }

            //now restore the normal files
            List <string> userfilenames = FileUtils.ReadLinesFromFile(fileinfofileapth);

            logger.Debug("# Owned file : " + userfilenames.Count);

            foreach (string filepath in userfilenames)
            {
                String relativeDiskFilePath = filepath.Trim();
                if (relativeDiskFilePath.Length > 0)
                {
                    if (relativeDiskFilePath [0] == '_')
                    {
                        relativeDiskFilePath = relativeDiskFilePath.Substring(1);
                    }
                    relativeDiskFilePath = FileUtils.getDiskPathFromMemoryPath(relativeDiskFilePath);
                    UserFile file = RestoreUserFile(userdir, relativeDiskFilePath, restoreFileContent);
                    userfilesystem.filemap.Add(FileUtils.getMemoryPathFromDiskPath(filepath.Trim()), file);
                }
            }
            return(userfilesystem);
        }
Example #19
0
 public bool SetFileContentSynchronized(UserFile newfile)
 {
     return(SetFileContentSynchronized(newfile.filecontent, newfile.versionNumber));
 }
        private UserFile GetFileFromFileMetaData(string filepath, List<string> metadata)
        {
            UserFile file = new UserFile (filepath, metadata [0].Trim ());
            file.filemetadata.filepath = filepath;

            file.filemetadata.filesize = int.Parse (metadata [1].Trim ());
            file.filemetadata.versionNumber = int.Parse (metadata [2].Trim ());

            if (metadata.Count > 3) { //this is because this file might be shared with no one
                string [] clients = metadata [3].Trim ().Split (',');

                foreach (string client in clients) {
                    if (client.Trim ().Length > 0) {
                        file.filemetadata.sharedwithclients.Add (client.Trim ());
                    }
                }
            }
            logger.Debug ("Recovered metadata as : " + file.filemetadata);
            return file;
        }
Example #21
0
        public FileMetaData getFileMetaDataCloneSynchronized(string filename)
        {
            UserFile file = getFileSynchronized(filename);

            return(file.getFileMetaDataCloneSynchronized());
        }
 /* Internal synchronized method to add file to the class map*/
 private void addFileToMapSynchronized(UserFile file)
 {
     lock (this.privateLock) {
         file.initializePrivateLock (); // this is because the files sent over the network do not have their private locks initialized
         this.filemap[file.filemetadata.filepath] =  file;
     }
 }