/* 	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;
        }
        public UserFile FetchFile(
            string clientId,
            string filename,
            string fileowner)
        {
            Logger.Debug("Fetching file : " + filename + " for client id :" + clientId +
                         " with  owner " + fileowner
                         );

            UserFile       file = null;
            UserFileSystem fs   = getUserFSFromMapSynchronized(fileowner);

            //Now there no need for taking any more locks of this class. Used
            // synchronized methods of the file system class

            if (fs != null)
            {
                file = fs.getFileCloneSynchronized(filename);                  //this is the cloned copy of file, do whatever you want

                //this is the case when file is not present in the map
                if (file == null)
                {
                    throw new FileNotFoundException("File with name :" + filename +
                                                    " not found for owner : " + fileowner
                                                    );
                }
                //this is the case when file is present in the map but marked for deletion
                if (file.filemetadata.markedForDeletion == true)
                {
                    Logger.Debug("File, present but marked for deletion");
                    throw new FileNotFoundException("File with name :" + filename +
                                                    " marked for deletion for owner : " + fileowner
                                                    );
                }
            }
            else                 //file system is not present for the user
            {
                throw new UserNotLoadedInMemoryException("Client not found in memory : " + clientId);
            }

            if (!fileowner.Equals(clientId))
            {
                bool access = file.checkUserAccessSynchronized(clientId);
                if (!access)
                {
                    throw new AccessViolationException("File : " + filename + " owned by " +
                                                       fileowner + "is not shared with " + clientId
                                                       );
                }
            }

            if (file.filecontent == null || file.filecontent.Length == 0)
            {
                UserFile diskFile = this.persistentstoreinteraction.fetchFileFromDisk(fileowner, filename);
                fs.filemap [filename].filecontent = diskFile.filecontent;                 //setting this in fs as well
                file.filecontent = diskFile.filecontent;
                fs.metadata.totalFileSystemSizeBytes += file.filecontent.Length;
            }
            return(file);
        }
Esempio n. 3
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;
     }
 }
Esempio n. 4
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.filepath);

            UserFile existingFile = getFileSynchronized(file.filepath);

            bool retval = true;

            if (existingFile != null)
            {
                if (file.versionNumber > existingFile.versionNumber)
                {
                    addFileToMapSynchronized(file);
                }
                else
                {
                    Logger.Debug("Existing file with higher version number found for file : " + file.filepath +
                                 " , skipping updation");
                    retval = false;
                }
            }
            else
            {
                addFileToMapSynchronized(file);
            }

            return(retval);
        }
Esempio n. 5
0
        public bool removeSharedUserFromFileSynchronized(string filename, string sharedUser)
        {
            Logger.Debug("Removing shared user from file : " + filename + " " + sharedUser);

            UserFile f = getFileSynchronized(filename);

            return(f.deleteSharedUserSynchronized(sharedUser));
        }
Esempio n. 6
0
 public void invalidateAllContents()
 {
     foreach (KeyValuePair <string, UserFile> entry in filemap)
     {
         UserFile file = entry.Value;
         file.SetFileContent(new byte[0], file.getFileVersionNumberSynchronized());
     }
 }
Esempio n. 7
0
        /* Internal synchronized method to get file from the user file system
         *      Use this to read from the map. Returns null if the file is not present in the memory
         */
        public UserFile getFileSynchronized(string filename)
        {
            UserFile existingFile = null;

            lock (this.privateLock) {
                if (this.filemap.ContainsKey(filename))
                {
                    existingFile = this.filemap [filename];
                }
            }
            return(existingFile);
        }
 public void flushToDisk(UserFile file)
 {
     try {
         logger.Debug("Flushing file to disk with filename and owner : " + file.filemetadata.filepath + " " + file.filemetadata.owner);
         JsonServiceClient client = new JsonServiceClient(PERSISTENT_STORAGE_SERVICE_ENDPOINT);
         client.Post <Object> ("/flushfile", new FlushFile {
             file = file
         });
     } catch (Exception e) {
         logger.Warn(e);
     }
 }
        /* Synchronized method to add file for given client id
           Throws exception if the user for present
         */
        public bool addFileSynchronized(string clientid, UserFile file)
        {
            Logger.Debug("Adding file : " + file.filemetadata.filepath + " for client : " + clientid);
            UserFileSystem fs = getUserFSFromMapSynchronized (clientid);
            if (fs != null) {
                return fs.addFileSynchronized(file); //this can possible throw a version conflict exception

            } else {
                throw new UserNotLoadedInMemoryException("Add file failed for user :"******" and file name :" + file.filemetadata.filepath);
            }
        }
Esempio n. 10
0
        /*
         *      This Function is called when a New File Needs to be Added into the memory
         */
        public bool sendsynchaddFileToMemory(string UserName, UserFile file, OOBHandler oobhandle, Group group, List <Address> where)
        {
            try {
                Logger.Debug("File Operations Synch - sendsynchaddFileToMemory >> BEGIN");
                bool             operationResult = false;
                MemoryMappedFile transferFile    = null;

                string      fileName = FileServerComm.getInstance().transManager.generateTransactionId(UserName + "_" + file.filemetadata.filepath);
                Transaction trans    = new Transaction(fileName);

                if (true == FileServerComm.getInstance().transManager.insertTransaction(trans))
                {
                    try {
                        int writtenBytesLength = 0;
                        transferFile = oobhandle.serializeIntoMemoryMappedFile(fileName, file, ref writtenBytesLength);

                        oobhandle.sendOOBData(group, transferFile, fileName, where);

                        trans.waitTillSignalled();
                        Logger.Debug("File Operations Synch - sendsynchaddFileToMemory >> OOB Transfer of Data Complete for : "
                                     + file.filemetadata.filepath);

                        operationResult = !trans.isTimedOut;

                        OOBTransaction oobtrabs = new OOBTransaction(fileName, IsisSystem.GetMyAddress(), writtenBytesLength);

                        if (operationResult)
                        {
                            group.OrderedSend(FileServerComm.SaveFileToMemory, oobtrabs);
                            trans.waitTillSignalled();
                            Logger.Debug("File Operations Synch - sendsynchaddFileToMemory >> Ordered Send Complete Complete for : "
                                         + file.filemetadata.filepath);
                            operationResult = !trans.isTimedOut;
                        }
                        FileServerComm.getInstance().transManager.removeAndGetTransaction(fileName);
                        operationResult = !trans.isTimedOut;
                    } catch (Exception e) {
                        Logger.Debug("Exception during File Operations Synch - sendsynchaddFileToMemory" + e.ToString());
                        FileServerComm.getInstance().transManager.removeAndGetTransaction(fileName);
                        operationResult = false;
                    }
                }
                else
                {
                    Logger.Debug("File Operations Synch - sendsynchaddFileToMemory >> Generation of Transaction ID Failed: " + fileName);
                }
                return(operationResult);
            } catch (Exception e) {
                Logger.Debug("Caught Exception " + e.ToString());
            }
            return(false);
        }
Esempio n. 11
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;
        }
Esempio n. 12
0
        public UserFile FetchFile(
            string clientId,
            string filename,
            string fileowner)
        {
            Logger.Debug("Fetching file : " + filename + " for client id :" + clientId +
                         " with  owner " + fileowner);

            UserFile       file = null;
            UserFileSystem fs   = getUserFSFromMapSynchronized(clientId);

            //Now there no need for taking any more locks of this class. Used
            // synchronized methods of the file system class

            if (fs != null)
            {
                if (this.clientToFileSystemMap [fileowner].filemap.ContainsKey(filename))
                {
                    file = fs.filemap [filename];
                }
                else
                {
                    throw new FileNotFoundException("File with name :" + filename +
                                                    " not found for owner : " + fileowner);
                }
            }
            else
            {
                throw new UserNotLoadedInMemoryException("Client not found in memory : " + clientId);
            }

            if (!fileowner.Equals(clientId))
            {
                bool access = false;
                foreach (string shareduser in file.sharedwithclients)
                {
                    if (shareduser.Equals(clientId))
                    {
                        access = true;
                        break;
                    }
                }
                if (!access)
                {
                    throw new AccessViolationException("File : " + filename + " owned by " +
                                                       fileowner + "is not shared with " + clientId
                                                       );
                }
            }
            return(file);
        }
        public UserFile fetchFileFromDisk(string username, string filename)
        {
            try {
                logger.Debug("Fetching from disk for username and filename : " + username + " " + filename);
                JsonServiceClient client = new JsonServiceClient(PERSISTENT_STORAGE_SERVICE_ENDPOINT);
                UserFile          f      = client.Get <UserFile> ("/fetchfile/" + username + "/" + filename);

                logger.Debug("Received from persistent disk file " + f);
                return(f);
            } catch (Exception e) {
                logger.Warn(e);
                return(null);
            }
        }
Esempio n. 14
0
        /* Synchronized method to add file for given client id
         * Throws exception if the user for present
         */
        public bool addFileSynchronized(string clientid, UserFile file)
        {
            Logger.Debug("Adding file :" + file.filepath + " for client : " + clientid);
            UserFileSystem fs = getUserFSFromMapSynchronized(clientid);

            if (fs != null)
            {
                return(fs.addFileSynchronized(file));
            }
            else
            {
                throw new UserNotLoadedInMemoryException("Add file failed for user :"******" and file name :" + file.filepath);
            }
        }
Esempio n. 15
0
        public object Get(GetFile request)
        {
            if (!filesystem.AuthenticateUser(request.clientId, request.password))
            {
                throw new AuthenticationException("Authentication failed");
            }

            try {
                UserFile file =
                    filesystem.FetchFile(request.clientId, request.filename, request.fileowner);
                return(file);
            } catch (Exception e) {
                logger.Debug("Exception occured while doing getfile for client : " + request.clientId
                             + " for filename :" + request.filename, e);
                throw e;
            }
        }
Esempio n. 16
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);
        }
Esempio n. 17
0
        public UserFile getFileCloneSynchronized(string filename)
        {
            UserFile existingFile = null;

            lock (this.privateLock) {
                if (this.filemap.ContainsKey(filename))
                {
                    existingFile = this.filemap [filename];
                }
            }
            if (existingFile == null)
            {
                return(null);
            }
            else
            {
                return(existingFile.getFileCloneSynchronized());
            }
        }
Esempio n. 18
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);
        }
Esempio n. 19
0
        public object Get(GetFile request)
        {
            logger.Info("****Request received for getting file : " + request.filename + " client id : " +
                        request.clientId + " and fileowner : " + request.fileowner
                        );

            if (!filesystem.AuthenticateUser(request.clientId, request.password))
            {
                throw new AuthenticationException("Authentication failed");
            }

            try {
                UserFile file =
                    filesystem.FetchFile(request.clientId, request.filename, request.fileowner);
                logger.Debug("Returning file with size : " + file.filemetadata.filesize);
                return(file);
            } catch (Exception e) {
                logger.Debug("Exception occured while doing getfile for client : " + request.clientId
                             + " for filename :" + request.filename, e);
                throw e;
            }
        }
Esempio n. 20
0
        /*
         * Handler Function which Adds a File into Memory
         */
        public void handleAddFileToMemory(object arg)
        {
            try {
                OOBTransaction request     = (OOBTransaction)arg;
                UserFile       userfilesys = null;
                Logger.Debug("Update addFileToMemory Data Synchronized - Begin");

                Group      group     = FileServerComm.getInstance().getFileServerGroup();
                OOBHandler oobhandle = FileServerComm.getInstance().getOOBHandler();

                MemoryMappedFile transferredFile = group.OOBFetch(request.transactionID);

                if (null != transferredFile)
                {
                    Logger.Debug("getUserFileInfo OOB Fetch Success :)");
                    int index = 0;
                    userfilesys = oobhandle.deserializeFromMemoryMappedFile(transferredFile, ref index, request.fileLength) as UserFile;
                }
                else
                {
                    Logger.Debug("getUserFileInfo Failed Reason: OOB Fetch Failed:)");
                }

                if (!request.initiatedSystemId.Equals(IsisSystem.GetMyAddress()) &&
                    null != userfilesys)
                {
                    filesystem.addFileSynchronized(userfilesys.filemetadata.owner, userfilesys);
                }
                group.OOBDelete(request.transactionID);

                TryReleaseLock(request.initiatedSystemId, request.transactionID);
                Logger.Debug("Update addFileToMemory Data Synchronized - End");
            } catch (Exception e) {
                Logger.Debug("Update addFileToMemory Data Synchronized encountered an exception " + e.ToString());
            }
        }
Esempio n. 21
0
        public FileMetaData getFileMetaDataCloneSynchronized(string filename)
        {
            UserFile file = getFileSynchronized(filename);

            return(file.getFileMetaDataCloneSynchronized());
        }
Esempio n. 22
0
 public long SetFileContentSynchronized(UserFile newfile)
 {
     return SetFileContentSynchronized( newfile.filecontent, newfile.filemetadata.versionNumber);
 }
Esempio n. 23
0
        /*
            This Function is called when a New File Needs to be Added into the memory
         */
        public bool sendsynchaddFileToMemory(string UserName, UserFile file, OOBHandler oobhandle, Group group, List<Address> where)
        {
            try {
                Logger.Debug ("File Operations Synch - sendsynchaddFileToMemory >> BEGIN");
                bool operationResult = false;
                MemoryMappedFile transferFile = null;

                string fileName = FileServerComm.getInstance ().transManager.generateTransactionId (UserName + "_" + file.filemetadata.filepath);
                Transaction trans = new Transaction (fileName);

                if (true == FileServerComm.getInstance ().transManager.insertTransaction (trans)) {
                    try {

                        int writtenBytesLength = 0;
                        transferFile = oobhandle.serializeIntoMemoryMappedFile (fileName, file,ref writtenBytesLength);

                        oobhandle.sendOOBData (group, transferFile, fileName, where);

                        trans.waitTillSignalled ();
                        Logger.Debug ("File Operations Synch - sendsynchaddFileToMemory >> OOB Transfer of Data Complete for : "
                                      + file.filemetadata.filepath);

                        operationResult = !trans.isTimedOut;

                        OOBTransaction oobtrabs = new OOBTransaction (fileName, IsisSystem.GetMyAddress (), writtenBytesLength);

                        if (operationResult) {
                            group.OrderedSend (FileServerComm.SaveFileToMemory, oobtrabs);
                            trans.waitTillSignalled ();
                            Logger.Debug ("File Operations Synch - sendsynchaddFileToMemory >> Ordered Send Complete Complete for : "
                                          + file.filemetadata.filepath);
                            operationResult = !trans.isTimedOut;
                        }
                        FileServerComm.getInstance ().transManager.removeAndGetTransaction (fileName);
                        operationResult = !trans.isTimedOut;
                    } catch (Exception e) {
                        Logger.Debug ("Exception during File Operations Synch - sendsynchaddFileToMemory" + e.ToString ());
                        FileServerComm.getInstance ().transManager.removeAndGetTransaction (fileName);
                        operationResult = false;
                    }
                } else {
                    Logger.Debug ("File Operations Synch - sendsynchaddFileToMemory >> Generation of Transaction ID Failed: " + fileName);
                }
                return operationResult;
            } catch (Exception e) {
                Logger.Debug("Caught Exception " + e.ToString());
            }
            return false;
        }
Esempio n. 24
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;
     }
 }
Esempio n. 25
0
 public long SetFileContentSynchronized(UserFile newfile)
 {
     return(SetFileContentSynchronized(newfile.filecontent, newfile.filemetadata.versionNumber));
 }
Esempio n. 26
0
 public bool SetFileContentSynchronized(UserFile newfile)
 {
     return(SetFileContentSynchronized(newfile.filecontent, newfile.versionNumber));
 }
Esempio n. 27
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);
                        }
                    }
                }
            }

            return(oldfs);
        }
Esempio n. 28
0
 /* Internal synchronized method to add file to the class map*/
 private void addFileToMapSynchronized(UserFile file)
 {
     lock (this.privateLock) {
         this.filemap.Add(file.filepath, file);
     }
 }