func compareTimestampsFromInfo(actualInfo, expectedInfo os.FileInfo) {
	actualAccessTime, err := atime.AccessTime(actualInfo)
	Expect(err).NotTo(HaveOccurred())

	expectedAccessTime, err := atime.AccessTime(expectedInfo)
	Expect(err).NotTo(HaveOccurred())

	Expect(actualInfo.ModTime().Unix()).To(Equal(expectedInfo.ModTime().Unix()))
	Expect(actualAccessTime.Unix()).To(Equal(expectedAccessTime.Unix()))
}
Exemple #2
0
func NewTimeMessage(fileInfo os.FileInfo) *TimeMessage {
	accessTime, err := atime.AccessTime(fileInfo)
	if err != nil {
		accessTime = time.Unix(0, 0)
	}

	return &TimeMessage{
		modificationTime: fileInfo.ModTime(),
		accessTime:       accessTime,
	}
}
		os.RemoveAll(tempDir)
	})

	Context("when creating a time message from file information", func() {
		var (
			timeMessage *scp.TimeMessage

			expectedModificationTime time.Time
			expectedAccessTime       time.Time
		)

		BeforeEach(func() {
			fileInfo, ferr := os.Stat(tempFile)
			Expect(ferr).NotTo(HaveOccurred())

			expectedAccessTime, ferr = atime.AccessTime(fileInfo)
			Expect(ferr).NotTo(HaveOccurred())

			expectedModificationTime = fileInfo.ModTime()

			timeMessage = scp.NewTimeMessage(fileInfo)
		})

		It("acquires the correct modification time", func() {
			Expect(timeMessage.ModificationTime()).To(Equal(expectedModificationTime))
		})

		It("acquires the correct access time", func() {
			Expect(timeMessage.AccessTime()).To(Equal(expectedAccessTime))
		})
	})
			stdin.WriteByte(0)

			tempFileInfo, err := os.Stat(tempFile)
			Expect(err).NotTo(HaveOccurred())
			timestamp := scp.NewTimeMessage(tempFileInfo)

			testCopier = newTestCopier(stdin, stdout, stderr, true)
			err = testCopier.ReceiveFile(tempDir, true, timestamp)
			Expect(err).NotTo(HaveOccurred())

			fileInfo, err := os.Stat(filepath.Join(tempDir, "hello.txt"))
			Expect(err).NotTo(HaveOccurred())

			Expect(fileInfo.ModTime()).To(Equal(tempFileInfo.ModTime()))

			fileAtime, err := atime.AccessTime(fileInfo)
			Expect(err).NotTo(HaveOccurred())

			tempAtime, err := atime.AccessTime(tempFileInfo)
			Expect(err).NotTo(HaveOccurred())

			Expect(fileInfo.ModTime()).To(Equal(tempFileInfo.ModTime()))
			Expect(fileAtime).To(Equal(tempAtime))
		})

		It("waits for a confirmation that the file has been sent", func() {
			stdin, pw := io.Pipe()
			stdout := &bytes.Buffer{}
			stderr := &bytes.Buffer{}

			errCh := make(chan error, 1)
				stdout := &bytes.Buffer{}
				stderr := &bytes.Buffer{}

				stdin.WriteString("D0755 0 empty-dir\n")
				stdin.WriteString("E\n")

				copier = newTestCopier(stdin, stdout, stderr, true)
				err := copier.ReceiveDirectory(tempDir, timeMessage)
				Expect(err).NotTo(HaveOccurred())

				Expect(filepath.Join(tempDir, "empty-dir")).To(BeADirectory())

				info, err := os.Stat(filepath.Join(tempDir, "empty-dir"))
				Expect(err).NotTo(HaveOccurred())

				accessTime, err := atime.AccessTime(info)
				Expect(err).NotTo(HaveOccurred())

				Expect(info.ModTime()).To(Equal(time.Unix(123456789, 0)))
				Expect(accessTime).To(Equal(time.Unix(987654321, 0)))
			})
		})

		Context("when the message is not a directory message", func() {
			It("raises an error", func() {
				stdin := &bytes.Buffer{}
				stdout := &bytes.Buffer{}
				stderr := &bytes.Buffer{}

				stdin.WriteString("C0755 0 empty-dir\n")
				stdin.WriteString("E\n")