GetModTime() public static method

Get the data modification time of a local file
public static GetModTime ( String fullPath ) : long
fullPath String
return long
示例#1
0
        private void createDirectoryOnServer(String name)
        {
            writeMessage("Telling server to create directory " + name);

            try {
                sendCommandToServer(Signal.createDirectoryOnServer);
                Common.SendString(socket, name);
                // TODO: wait for reply to know that it was created on server before updating index
                fileIndex.Update(new MyFile(name, 'd', Common.GetModTime(dataDir + name), 0, "0"));
            } catch (Exception e) {
                writeMessage("error requesting server directory create: " + e.Message);
            }
        }
示例#2
0
文件: Common.cs 项目: jonocodes/mybox
        public static MyFile SendFile(String relPath, Socket socket, String baseDir)
        {
            MyFile myFile = null;

            try {
                String fullPath = baseDir + relPath;

                byte[] fileName    = Encoding.UTF8.GetBytes(relPath);                 //file name
                byte[] fileNameLen = BitConverter.GetBytes((Int16)(fileName.Length)); //length of file name
                byte[] fileData    = File.ReadAllBytes(fullPath);                     //file
                byte[] fileDataLen = BitConverter.GetBytes(fileData.Length);          // file length

                // TODO: make sure "file length" matches actual file size

                long   modtime   = Common.GetModTime(fullPath);    // TODO: make timestamps into int
                byte[] timestamp = BitConverter.GetBytes(modtime); // assume long = int64 = 8 bytes

                // temporarially calc checksum here, though should be done higher up
                byte[] checksum       = FileChecksumToBytes(fullPath);
                String checksumString = BitConverter.ToString(checksum).Replace("-", String.Empty).ToLower();

                Console.WriteLine("Sending file " + relPath + " " + modtime);

                socket.Send(fileNameLen);//2
                socket.Send(fileName);

                socket.Send(timestamp);   //8

                socket.Send(checksum);    //16 bytes, or 32 characters?
                socket.Send(fileDataLen); //4, TODO: set this to 8 bits for files larger then 4GB ?
                socket.Send(fileData);

                myFile = new MyFile(relPath, 'f', modtime, fileData.Length, checksumString);
            }
            catch (Exception e) {
                Console.WriteLine("Operation failed: " + e.Message);
            }

            return(myFile);
        }
示例#3
0
        /// <summary>
        /// Handle input signals from the client
        /// </summary>
        /// <param name="signal"></param>
        private void handleInput(Signal signal)
        {
            Console.WriteLine("Handling input for signal " + signal);

            switch (signal)
            {
            case Signal.c2s:

                MyFile newFile = Common.ReceiveFile(socket, dataDir);

                if (newFile != null)
                {
                    server.serverDB.UpdateFile(User, newFile);
                }

                server.SpanCatchupOperation(handle, User.id, signal, newFile.name);
                break;

            //case Signal.clientWantsToSend:
            //  String relPath = Common.ReceiveString(socket);
            //  long timestamp = Common.ReceiveTimestamp(socket);

            //  sendCommandToClient(Signal.clientWantsToSend_response);

            //  Common.SendString(socket, relPath);

            //  // reply 'yes' if it refers to a file that does not exist or if the times do not match
            //  if (File.Exists(dataDir + relPath) && Common.GetModTime(dataDir + relPath) == timestamp) {
            //    Common.SendString(socket, "no");
            //  }
            //  else {
            //    Common.SendString(socket, "yes");
            //  }
            //  break;

            case Signal.clientWants:
                String relPath = Common.ReceiveString(socket);
                if (File.Exists(dataDir + relPath))
                {
                    outQueue.Enqueue(relPath);
                    processOutQueue();
                }
                break;

            case Signal.deleteOnServer:
                relPath = Common.ReceiveString(socket);

                if (Common.DeleteLocal(dataDir + relPath))
                {
                    server.serverDB.RemoveFile(User, relPath);
                }
//            index.Remove(relPath);  // TODO: check return value

                server.SpanCatchupOperation(handle, User.id, signal, relPath);
                break;

            case Signal.createDirectoryOnServer:
                relPath = Common.ReceiveString(socket);

                if (Common.CreateLocalDirectory(dataDir + relPath))
                {
                    server.serverDB.UpdateFile(User, new MyFile(relPath, 'd', Common.GetModTime(dataDir + relPath)
                                                                , 0, "0"));
                }

                server.SpanCatchupOperation(handle, User.id, signal, relPath);
                break;

            case Signal.requestServerFileList:

                List <List <string> > fileListToSerialize = server.serverDB.GetFileListSerializable(User);

                String jsonOutStringFiles = JsonConvert.SerializeObject(fileListToSerialize);

                Console.WriteLine("sending json file list: " + jsonOutStringFiles);

                try {
                    sendCommandToClient(Signal.requestServerFileList_response);
                    Common.SendString(socket, jsonOutStringFiles);
                }
                catch (Exception e) {
                    Console.WriteLine("Error during " + Signal.requestServerFileList_response + e.Message);
                    Common.ExitError();
                }

                break;

            case Signal.attachaccount:

                String args = Common.ReceiveString(socket);

                Console.WriteLine("received " + args);

                List <string> attachInput = JsonConvert.DeserializeObject <List <string> >(args);

                String userName = attachInput[0];
                String password = attachInput[1];

                Dictionary <string, string> jsonOut = new Dictionary <string, string>();
                jsonOut.Add("serverMyboxVersion", Common.AppVersion);

                if (attachUser(userName, password))
                {
                    jsonOut.Add("status", "success");
                    //jsonOut.Add("quota", Account.quota.ToString());
                    //jsonOut.Add("salt", Account.salt);

                    server.AddToMultiMap(User.id, handle);
                }
                else
                {
                    jsonOut.Add("status", "failed");
                    jsonOut.Add("error", "login invalid");

                    close();
                    // TODO: disconnect the client here
                }

                String jsonOutString = JsonConvert.SerializeObject(jsonOut);

                try {
                    sendCommandToClient(Signal.attachaccount_response);
                    Common.SendString(socket, jsonOutString);
                }
                catch (Exception e) {
                    Console.WriteLine("Error during " + Signal.attachaccount_response + e.Message);
                    Common.ExitError();
                }

                Console.WriteLine("attachaccount_response: " + jsonOutString);

                break;

            default:
                Console.WriteLine("Unknown command");
                break;
            }
        }
示例#4
0
        /// <summary>
        /// Compares the client to the client index to the server
        /// </summary>
        private void fullSync()
        {
            writeMessage("disabling listener");
            disableDirListener(); // hack while incoming set gets figured out

            setStatus(ClientStatus.SYNCING);

            writeMessage("fullSync started  " + DateTime.Now);

            // TODO: update all time comparisons to respect server/client time differences

            // populate S
            // file list according to server
            bool listReturned = serverDiscussion(Signal.requestServerFileList, Signal.requestServerFileList_response, null);

            if (!listReturned)
            {
                throw new Exception("requestServerFileList did not return in time");
            }

            // file list according to client filesystem
            Dictionary <String, MyFile> C = getLocalFileList();

            // file list according to client index database
            Dictionary <String, MyFile> I = fileIndex.GetFiles();

            // holds the name=>action according to the I vs C vs S comparison
            Dictionary <String, Signal> fileActionList = new Dictionary <string, Signal>();

            // here we make the assumption that fullSync() is only called once and right after initial connection
            long lastSync = -1;

            if (fileIndex.FoundAtInit)
            {
                lastSync = fileIndex.LastUpdate;
            }

            writeMessage("fullSync comparing C=" + C.Count + " to I=" + I.Count + " to S=" + S.Count
                         + "   lastSync=" + lastSync);


            foreach (KeyValuePair <String, MyFile> file in C)
            {
                String name = file.Key;
                MyFile c    = file.Value;

                if (I.ContainsKey(name))
                {
                    // TODO: handle conflicts where a file and directory have the same name

                    MyFile i = I[name];

                    // if it is a file
                    if (!Directory.Exists(dataDir + name))
                    {
                        if (c.modtime != i.modtime) // if times differ
                        {
                            writeMessage(name + " " + " c.modtime=" + c.modtime + " i.modtime=" + i.modtime);
                            writeMessage(name + " = transfer from client to server since file changed");

                            fileActionList.Add(name, Signal.c2s);
                        }
                    }

                    I.Remove(name);

                    // if it is a directory, do nothing because the files will already be coppied one by one
                }
                else // if it exists in C and not in index, push to server

                {
                    if (Directory.Exists(dataDir + name))
                    {
                        writeMessage(name + " = create directory on server since new directory");
                        fileActionList.Add(name, Signal.createDirectoryOnServer);
                    }
                    else
                    {
                        writeMessage(name + " = transfer from client to server since new file");
                        fileActionList.Add(name, Signal.c2s);
                    }
                }
            }

            // now process items that are in the index and not on the client

            foreach (KeyValuePair <String, MyFile> file in I)
            {
                // delete file or directory on server
                writeMessage(file.Key + " = remove from server since it is in I but not C");
                fileActionList.Add(file.Key, Signal.deleteOnServer);
            }

            writeMessage("finished addressing C vs I");


            // TODO: handle case where there is the same name item but it is a file on the client and dir on server

            foreach (KeyValuePair <String, MyFile> file in C)
            {
                String name = file.Key;
                MyFile c    = file.Value;

                if (S.ContainsKey(name))
                {
                    MyFile s = S[name];

                    // if it is not a directory and the times are different, compare times
                    if (!Directory.Exists(dataDir + name) && c.modtime != s.modtime)
                    {
                        writeMessage(name + " " + lastSync + " c.modtime=" + c.modtime + " s.modtime=" + s.modtime);

                        if (lastSync == -1)
                        {
                            writeMessage(name + " = conflict, since index is gone the newest file cannot be determined");
                        }
                        else if (c.modtime > lastSync)
                        {
                            if (s.modtime > lastSync)
                            {
                                writeMessage(name + " = conflict (both client and server file are new)");
                            }
                            else
                            {
                                writeMessage(name + " = transfer from client to server 1");

                                if (fileActionList.ContainsKey(name)) // can this be turned into a nested function perhaps?
                                {
                                    if (fileActionList[name] != Signal.c2s)
                                    {
                                        // conflict
                                    }
                                }
                                else
                                {
                                    fileActionList.Add(name, Signal.c2s);
                                }
                            }
                        }
                        else
                        {
                            if (s.modtime > c.modtime)
                            {
                                writeMessage(name + " = transfer from server to client 1");

                                if (fileActionList.ContainsKey(name))
                                {
                                    if (fileActionList[name] != Signal.clientWants)
                                    {
                                        // conflict
                                    }
                                }
                                else
                                {
                                    fileActionList.Add(name, Signal.clientWants);
                                }
                                // TODO: set overlay icon
                            }
                            else
                            {
                                writeMessage(name + " = conflict (both client and server file are old)");
                            }
                        }
                    }
                    S.Remove(name);
                }
                else
                {
                    if (c.modtime > lastSync) // will occur if index is missing since lastSync will be -1, thus performing a merge

                    {
                        if (Directory.Exists(dataDir + name))
                        {
                            writeMessage(name + " = create directory on server");

                            if (fileActionList.ContainsKey(name))
                            {
                                if (fileActionList[name] != Signal.createDirectoryOnServer)
                                {
                                    // conflict
                                }
                            }
                            else
                            {
                                fileActionList.Add(name, Signal.createDirectoryOnServer);
                            }
                        }
                        else
                        {
                            writeMessage(name + " = transfer from client to server 2");

                            if (fileActionList.ContainsKey(name))
                            {
                                if (fileActionList[name] != Signal.c2s)
                                {
                                    // conflict
                                }
                            }
                            else
                            {
                                fileActionList.Add(name, Signal.c2s);
                            }
                        }
                    }
                    else
                    {
                        writeMessage(name + " = remove on client");

                        if (fileActionList.ContainsKey(name))
                        {
                            if (fileActionList[name] != Signal.deleteOnClient)
                            {
                                // conflict
                            }
                        }
                        else
                        {
                            fileActionList.Add(name, Signal.deleteOnClient);
                        }
                    }
                }
            }

            foreach (KeyValuePair <String, MyFile> file in S)
            {
                String name = file.Key;
                MyFile s    = file.Value;

                if (s.modtime > lastSync) // will occur if index is missing since lastSync will be -1, thus performing a merge

                {
                    if (s.type == 'd')
                    {
                        writeMessage(name + " = create local directory on client");

                        if (fileActionList.ContainsKey(name))
                        {
                            if (fileActionList[name] != Signal.createDirectoryOnClient)
                            {
                                // conflict
                            }
                        }
                        else
                        {
                            fileActionList.Add(name, Signal.createDirectoryOnClient);
                        }
                    }
                    else
                    {
                        writeMessage(name + " = transfer from server to client 2");

                        if (fileActionList.ContainsKey(name))
                        {
                            if (fileActionList[name] != Signal.clientWants)
                            {
                                // conflict
                            }
                        }
                        else
                        {
                            fileActionList.Add(name, Signal.clientWants);
                        }
                        // TODO: set overlay icon
                    }
                }
                else
                {
                    writeMessage(name + " = remove from server"); // file or directory

                    if (fileActionList.ContainsKey(name))
                    {
                        if (fileActionList[name] != Signal.deleteOnServer)
                        {
                            // conflict
                        }
                    }
                    else
                    {
                        fileActionList.Add(name, Signal.deleteOnServer);
                    }
                }
            }

            // now process the fileLists

            writeMessage("Processing " + fileActionList.Count + " items on action list...");

            foreach (KeyValuePair <String, Signal> signalItem in fileActionList)
            {
                writeMessage(" " + signalItem.Key + " => " + signalItem.Value);

                switch (signalItem.Value)
                {
                case Signal.c2s:
                    outQueue.Enqueue(signalItem.Key);
                    break;

                case Signal.createDirectoryOnServer:
                    createDirectoryOnServer(signalItem.Key);
                    break;

                case Signal.deleteOnServer:
                    deleteOnServer(signalItem.Key);
                    break;

                case Signal.clientWants:
                    requestFile(signalItem.Key);
                    break;

                case Signal.deleteOnClient:
                    if (Common.DeleteLocal(dataDir + signalItem.Key))
                    {
                        fileIndex.Remove(signalItem.Key);
                    }
                    break;

                case Signal.createDirectoryOnClient:
                    if (Common.CreateLocalDirectory(dataDir + signalItem.Key))
                    {
                        fileIndex.Update(new MyFile(signalItem.Key, 'd', Common.GetModTime(dataDir + signalItem.Key),
                                                    0, "0"));
                    }
                    break;

                default:
                    throw new Exception("Unhandled signal in action list");
                }
            }

            processOutQueue();

            writeMessage("enableing listener since sync is done");
            enableDirListener();

            writeMessage("Sync finished " + DateTime.Now);

            if (incommingFiles.Count == 0)
            {
                setStatus(ClientStatus.READY);
            }


            Thread.Sleep(2000);
            checkSync();
        }
示例#5
0
        /// <summary>
        /// Deal with incoming signal from server
        /// </summary>
        /// <param name="signal"></param>
        private void handleInput(Signal signal)
        {
            if (paused)
            {
                return;
            }

            writeMessage("Handling input for signal " + signal);

            // TODO: make sure these all update the index

            setStatus(ClientStatus.SYNCING);

            switch (signal)
            {
            case Signal.s2c:
                MyFile newFile = Common.ReceiveFile(socket, dataDir);
                if (newFile != null)
                {
                    fileIndex.Update(newFile);
                    incommingFiles.Remove(newFile.name);
                    setOverlay(true);
                }
                break;

            case Signal.deleteOnClient:
                // catchup operation
                String relPath = Common.ReceiveString(socket);
                if (Common.DeleteLocal(dataDir + relPath))
                {
                    fileIndex.Remove(relPath);
                }
                break;

            case Signal.createDirectoryOnClient:
                // catchup operation
                relPath = Common.ReceiveString(socket);
                if (Common.CreateLocalDirectory(dataDir + relPath))
                {
                    fileIndex.Update(new MyFile(relPath, 'd', Common.GetModTime(dataDir + relPath), 0, "0"));
                }

                break;

            case Signal.requestServerFileList_response:

                String jsonStringFiles = Common.ReceiveString(socket);

                List <List <string> > fileDict =
                    JsonConvert.DeserializeObject <List <List <string> > >(jsonStringFiles);

                S.Clear();

                foreach (List <string> fileItem in fileDict)
                {
                    // TODO: try, catch for parse errors etc
                    S.Add(fileItem[0], new MyFile(fileItem[0], char.Parse(fileItem[1]), long.Parse(fileItem[2]),
                                                  long.Parse(fileItem[3]), fileItem[4]));
                }

                break;

            case Signal.attachaccount_response:

                // TODO: replace JSON parser with simple text parsing so we dont have to lug around the dependency

                Dictionary <string, string> jsonMap =
                    JsonConvert.DeserializeObject <Dictionary <string, string> >(Common.ReceiveString(socket));

                if (jsonMap["status"] != "success")// TODO: change to signal
                {
                    writeMessage("Unable to attach account. Server response: " + jsonMap["error"]);
                    // TODO: catch these exceptions above somewhere
                    //throw new Exception("Unable to attach account. Server response: " + jsonMap["error"]);
                    //socket.Close();
                    Stop();
                }
                else
                {
                    //writeMessage("set account salt to: " + account.Salt);

                    if (Common.AppVersion != jsonMap["serverMyboxVersion"])
                    {
                        writeMessage("Client and Server Mybox versions do not match");
                    }
                }

                break;

            default:
                writeMessage("Unknown command from server: " + signal);
                break;
            }

            lastReceivedOperation = signal;

            if (incommingFiles.Count == 0 && outQueue.Count == 0)
            {
                setStatus(ClientStatus.READY);
            }
        }